i would seek assistance on the following batch script on set and goto - batch-file

is there a way for me to set a batch script after user input , it set a variable and then use go to. for example
SET INPUTLocation=""
SET /P INPUTLocation=Please select a number:
echo you enter %INPUTlocation%
IF /I '%INPUTlocation%'=='1'set router="router1" GOTO testnow`$`
IF /I '%INPUTlocation%'=='2' set router="router2" GOTO testnow'$
IF /I '%INPUTlocation%'=='3' set router="router3" GOTO testnow'$'

You mean executing multiple commands? You can do that by putting them in parentheses:
SET INPUTLocation=""
SET /P INPUTLocation=Please select a number:
echo you enter %INPUTlocation%
IF /I '%INPUTlocation%'=='1' (
set router="router1"
GOTO testnow`$`
)
IF /I '%INPUTlocation%'=='2' (
set router="router2"
GOTO testnow'$
)
IF /I '%INPUTlocation%'=='3' (
set router="router3"
GOTO testnow'$'
)
Note, if you want to input a single character, you may also use the CHOICE command. It lets you select a single character from a specified set of characters, and it doesn't require you to press enter afterwards. For these kinds of menu-selections, it is more convenient than SET.

Related

Batch Script not Executing filaname + Parameters

My batch script asks the user if he wants to run the program LegoScript.exe
If the user hits 'N' or 'n' then it terminates the program, however if the user types 'Y' or 'y' then it should echo the string "legoscript.exe" and then place the cursor right after it on the same line, so that the user can type additional arguments, and then run the program with the provided arguments, however rather than running the program with the provided arguments, it just shows me a blinking caret indefinitely
Here is the batch script:
#echo off
setlocal enabledelayedexpansion
set "base_path=%userprofile%\\desktop\\LegoScript\\"
for /d %%d in ("%base_path%\V*") do (
set "latest=%%d"
)
set "LATEST=!latest!"
echo Latest folder is: %LATEST%
"%base_path%\\MINGW\\Bin\\gcc.exe" "%latest%\\LegoScript.c" -o "%latest%\\LegoScript.exe"
set /p run=Do you want to run LegoScript.exe? (y/n):
if /i "%run%"=="y" (
#cmd /k ^"cmd /c "echo|set /p="LegoScript.exe "&set /p "arg1= "&& set /p "arg2= "&& set /p "arg3= "&& set /p "arg4= "&& start "" "%latest%\\LegoScript.exe" %arg1% %arg2% %arg3% %arg4%
) else if /i "%run%"=="n" (
exit
) else (
echo Invalid input. Please enter y or n.
pause >nul
call %0
)
Anyone care to figure out how to fix this?
I tried adding extra double quotes and escape characters ('^') but no luck.

How to set a value from a string

I got a command/program that returns a text in standard output as a string, which is "YES" or "NO", how can I set a variable to this string to use it elsewhere?
I want it to behave like
Echo Yes << this is from the command, I can't change it
Yes << How it is shown in the CMD window
IF "Yes" is defined goto yes
IF "No" is defined goto no
:yes
echo yes & exit
:no
echo no & exit
To capture the output of a command/program, you need to use a For /F Loop to parse and take action on the output you are looking to capture.
If you ONLY need to make a choice, using FIND or FINDStr to make a decision can often be a simpler solution.
So if you want to simply make a choice, and the only output is yes or no, and those are the only two options the output might take you could easily just check for one and assume the other if the 1st condition isn't found:
ECHO Yes | FIND /I "Yes" && (
GOTO :yes
) || (
GOTO :NO
)
If you only want to choose and you're calling a label with the same characters as the output and only such outputs will be produced you could make a select statement with a for-loop which is very nice:
FOR /F "Tokens=*" %%_ IN ('
ECHO.Yes
') DO (
GOTO :%%_
)
If you might have output that is not a valid choice there are some ways to make the above work by searching the script to validate the output and choose a default option if the label isn't found:
FOR /F "Tokens=*" %%_ IN ('
ECHO.Yes
') DO (
TYPE "%~f0" | FIND /I ":%%_" && (
GOTO :%%_
) || (
GOTO :Default
)
)
:Default
Echo. Invalid Result
GOTO :EOF
If you want to capture the info and make a choice so you can use the info in another part of the program too, and you again need a FOR /F Loop:
FOR /F "Tokens=*" %%_ IN ('
ECHO.Yes
') DO (
SET "_Response=%%_"
GOTO :%%_
)
:Yes
ECHO.The result was "%_Response%"!
GOTO :EOF

batch file display prompt not correct

There are two issues with the batch below. The first is, when batch file when opened prompts the user with a "y/n" question:
Question 1
Has the check been done
If the answer to this is "y" then another "y/n" question is displayed
Question 2
Do you want to send the DOSE report
If the answer to question 1 is "n" the check function is called and another question is displayed. However, currently the line in bold is being displayed then it is going to the second part (the goodbye function). What am I doing wrong? Thank you :).
Current batch
#ECHO OFF
:: ask user
:choice
set /P c=Has the check been done [y/n]
if /i %c%==y (
set /P c=Do you want to send the DOSE report[y/n]?
) else (
if /i %c%==n goto check
)
if /i %c%==y (
"L:\NGS\HLA LAB\total quality management\QC & QA\DOSE reports\DOSE reporting form.xlsm"
) else (
if /i %c%==n goto goodbye
)
:check
set /P c=Do you want to perform the check [y/n]
if /i %c%==y (
echo "perform check and hit enter when complete"
pause goto choice
) else (
if /i %c%==n goto goodbye
:: count loop
set var1=0
:loop
set /a var1=%var1%+1
echo %var1%
if %var1% EQU 1 (
goto end
) else (
goto loop
)
:end
echo "the DOSE report has already been sent by %USERNAME% on %DATE% at %TIME%"
:goodbye
echo "goodbye"
TIMEOUT 2 /nobreak
exit
First off, the if-else statement in :choice is missing its closing )
Heres an important note on user input, never trust it. Try putting something other than y/n in the first
:choice
set /p c=Has the check been done [y/n]
if /i %c%==y (
set /p c=Do you want to send the DOSE report[y/n]?
) else (
if /i %c%==n goto check
)
If you input something invalid into the first if-else , say somebody tries typing no instead of n, it will fail to return them to :choice as it only checks for n or y
And end up running through script. In your case it fails the if statements before :check and starts :check's proccess, but in check the same issue arises, and it will run through to ::count loop and the following commands where it can mess up your data.
After each if-else statement, its VERY safe practice to add a default action, such as;
:choice
set /p c=Has the check been done [y/n]
if /i %c%==y (
set /p c=Do you want to send the DOSE report[y/n]?
) else (
if /i %c%==n goto check
)
:[ If both if statements are false, it will reach this line: ]
cls
echo Error: "%c%" is not y/n.
pause
goto :choice
Another thing to note, if nothing is input, errors will occur. You can fix this by checking if the variable is defined right after set /p c=Has the check been done [y/n] using:
if not defined c (
cls
echo Error: Input cannot be empty!
pause
goto :choice
)
So a proper way to do the first check would be:
:choice
set /p c=Has the check been done [y/n]
:[Empty check ]
if not defined c cls & echo Error: Input cannot be empty! & pause & goto :choice
if /i "%c%==y" (
set /p c=Do you want to send the DOSE report[y/n]?
) else (
if /i "%c%==n" goto check
)
:[ Invalid input ]
cls & echo Error: "%c%" is not y/n. & pause & goto :choice
To answer your question directly... well, I'm not sure what you're asking to be honest. I'm having trouble deciphering "I am trying to have the below batch file increment the counter %var1 only if the "y" and not able to get the syntax correct."
I don't see any outright syntax errors, but you do have quite a few logic issues with your script. Indeed, the value of %var1% is being incremented. Put an echo Var1 incremented: %var1% after the set /a var1+=1 and see for yourself. To fix your script, you need to address what statements get executed under what conditions.
Use setlocal. It's just good practice. Using setlocal helps you avoid contaminating the outer console thread with variables that are only relevant to the runtime of this script.
Don't trust that the user will always answer "y" or "n". What happens if they answer "w"? As your script is written, it will proceed to sections you probably didn't intend. I have two suggestions for this.
a. Don't explicitly check for an answer of "n". If it's not "y", assume it's "n". If that is not appropriate, then have a final else goto label for whatever label immediately precedes the conditional.
b. Alternatively, instead of set /P, consider using choice. Example:
choice /c YN /m "Do you want to perform the check? "
if errorlevel 2 (
rem // user chose "N"
) else (
rem // user chose "Y"
)
In your :choice section of code, what is the difference between an answer of Y then N, versus an answer of N? As written, there's no difference. Either way, the script proceeds to :check. Examine your logic here.
Instead of exit, consider using either exit /b or goto :EOF. When running your script from a cmd prompt (as opposed to double-clicking it), you should avoid exiting the parent thread.
For goodness sake, indent your code and add some line breaks! That big blob of left-justified commands has no flow, no rhythm. It's tedious to read and troubleshoot.
Before:
#ECHO OFF
:: ask user
:choice
set /P c=Has the check been done [y/n]
if /i %c%==y (
set /P c=Do you want to send the DOSE report[y/n]?
) else (
if /i %c%==n goto check
)
:check
set /P c=Do you want to perform the check [y/n]
if /i %c%==y (
set /P c=please complete the check and click enter
goto file
) else (
if /i %c%==n goto goodbye
)
It's like a slab, difficult to see at a glance which statements are ancestors and which are descendents or where the section breaks occur.
After:
#ECHO OFF
setlocal
:choice // ask user
choice /c YN /m "Has the check been done? "
if not errorlevel 2 (
choice /c YN /m "Do you want to send the DOSE report? "
if errorlevel 2 (
goto goodbye
)
)
:check
choice /c YN /m "Do you want to perform the check? "
if errorlevel 2 goto goodbye
set /P "c=Please complete the check and click enter: "
See? It's much more pleasant to read that way. By the way, you might've noticed that I combined the "ask user" comment with the :choice label. That's often done when defining functions to explain expected parameters, or just explain what's going on. You don't really need the double slashes. It just makes Stack Overflow's Ted Turner Technicolor parser demonstrate that that's a comment.

Need assistance with batch. ( was unexpected error

so I'm writing batch and come up with the ( was unexpected error after setting the first variable. I want a batch file to start with just setting the color. (I named it edmond)
this is my code
#echo off
SETLOCAL
title Edmond
goto :Check
:Prompt
set /p action=What shall I do for you master?
:Check
if not defined action goto :Prompt
echo %action%
pause
if /i %%action == color (
echo stuff
:cl
set /p BC=What Should the background color be?
set /p FC=and the foreground color?
if %BC%==Black set BC1=0
if %BC%==Blue set BC1=1
if %BC%==Green set BC1=2
if %BC%==Aqua set BC1=3
if %BC%==Red set BC1=4
if %BC%==Purple set BC1=5
if %BC%==Yellow set BC1=6
if %BC%==White set BC1=7
if %BC%==Gray set BC1=8
if %BC%==LBlue set BC1=9
if %BC%==LGreen set BC1=a
if %BC%==LAqua set BC1=b
if %BC%==LRed set BC1=c
if %BC%==LPurple set BC1=d
if %BC%==LYellow set BC1=e
if %BC%==LWhite (
set BC1=f
) else (
echo I'm sorry, I didn't exactly understand that.
echo By any chance could you say it again?
goto cl
)
if %FC%==Black set FC1=0
if %FC%==Blue set FC1=1
if %FC%==Green set FC1=2
if %FC%==Aqua set FC1=3
if %FC%==Red set FC1=4
if %FC%==Purple set FC1=5
if %FC%==Yellow set FC1=6
if %FC%==White set FC1=7
if %FC%==Gray set FC1=8
if %FC%==LBlue set FC1=9
if %FC%==LGreen set FC1=a
if %FC%==LAqua set FC1=b
if %FC%==LRed set FC1=c
if %FC%==LPurple set FC1=d
if %FC%==LYellow set FC1=e
if %FC%==LWhite (
set FC1=f
) else (
echo I'm sorry, I didn't exactly understand that.
)
echo Applying changes.
ping localhost -n 2 >nul
color %FC%%BC%
) else (
echo I'm sorry, I didn't exactly understand that.
)
pause
exit
after the set /p action=what shall I do for you master? it says ( was unexpected and then shuts down (I got the ( was unexpected by printing screen before it closes) can someone help? thanks (in advance)
if /i %%action == color (
should be
if /i %action% == color (
or better
if /i "%action%"=="color" (
since you have uncontrolled input that may contain spaces or other separators.
Your upcoming problems are many-fold.
You can't use a label in a (block statement - a series of parenthesised statements)
You need to
setlocal enabledelayedexpansion
then use !var! in place of %var% withing a block statement to access the value of any ordinary environment variable changed or established within the block.
Very much truncated revised structure:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:Prompt
set /p action=What shall I do for you master?
:Check
if not defined action goto :Prompt
echo %action%
if /i "%action%"=="color" GOTO setcolor
echo I'm sorry, I didn't exactly understand that.
goto Prompt
:setcolor
echo stuff
:cl
set /p BC=What Should the background color be?
set /p FC=and the foreground color?
:: This forces BC1 & FC1 to be undefined
SET "BC1="
SET "FC1="
if /i "%BC%"=="Black" set BC1=0
if /i "%BC%"=="Blue" set BC1=1
:: Your job to fill in the rest
IF NOT DEFINED BC1 (
echo I'm sorry, I didn't exactly understand that.
echo By any chance could you say it again?
goto cl
)
if /i "%FC%"=="Yellow" set FC1=6
:: Your job to fill in the rest
IF NOT DEFINED BC1 (
echo I'm sorry, I didn't exactly understand that.
rem I suppose you really want to re-enter at this point.
rem note that you need to use REM within a block, not ::-style comments.
goto cl
)
echo Applying changes.
ping localhost -n 2 >NUL
:: Best to use FC1 and BC1 here, else you'll try to execute "color YellowBlack"
:: And it's likely you have them reversed.
color %FC1%%BC1%
GOTO :EOF

Problems with BAT file: IF/ELSE not working as expected

I'm trying to make a short BAT file and I'm having trouble with one of its functions. I've tried a number of different ways to do this and none of them seem to work, but being a beginner at this I can't figure out the problem. Basically, the script, as it runs, is supposed to check if a certain .BAT file exists, and if it does, the script asks if the user wants to run it. If the user indicates Y, the other BAT is called and then the original script proceeds. If the user indicates N, the script is supposed to proceed without calling the other BAT. So far the script always notices and asks about the file, but choosing Y at the prompt never works. I'm sure the solution is obvious, but it's escaping me. Here's the code:
SET /P kmname=Enter database name:
:kmstart
IF EXIST C:\Visual\area\%kmname%\%kmname%.flt (
ECHO %kmname%.flt found, will now create %kmname%.ive.
CD C:\Visual\area\%kmname%\
IF EXIST Preprocess.bat (
SET /P kmpreproc=Found Preprocess.bat. Do you want to run it now?
IF /I "%kmpreproc%" EQU "Y" (
GOTO PREPROC
) ELSE (
GOTO CONTINUE
)
)
GOTO CONTINUE
) ELSE (
ECHO C:\Visual\area\%kmname%\%kmname%.flt does not exist. Try again.
SET /P kmname=Enter database name:
GOTO kmstart
)
:PREPROC
ECHO Running Preprocess.bat.
:CONTINUE
ECHO Continuing process.
PAUSE
The problem is your variables are being evaluated before they enter the if's, which means cmd won't see any changes until they have ended.
This is causing problems for you as your variables kmpreproc and, depending on the first if result, kmname change within the if blocks.
The fix (presuming the rest of your code is working) is to enable delayed expansion and use delayed expansion instead of normal expansion, by changing the %'s to ! on your variables.
setlocal enabledelayedexpansion
SET /P kmname=Enter database name:
:kmstart
IF EXIST C:\Visual\area\!kmname!\!kmname!.flt (
ECHO !kmname!.flt found, will now create !kmname!.ive.
CD C:\Visual\area\!kmname!\
IF EXIST Preprocess.bat (
SET /P kmpreproc=Found Preprocess.bat. Do you want to run it now?
IF /I "!kmpreproc!" EQU "Y" (
GOTO PREPROC
) ELSE (
GOTO CONTINUE
)
)
GOTO CONTINUE
) ELSE (
ECHO C:\Visual\area\!kmname!\!kmname!.flt does not exist. Try again.
SET /P kmname=Enter database name:
GOTO kmstart
)
:PREPROC
ECHO Running Preprocess.bat.
:CONTINUE
ECHO Continuing process.
PAUSE
Here is below my corrected and tided up code :
#echo off
SET /P kmname=Enter database name:
:kmstart
IF EXIST C:\Visual\area\%kmname%\%kmname%.flt (
ECHO %kmname%.flt found, will now create %kmname%.ive.
CD C:\Visual\area\%kmname%\
IF EXIST Preprocess.bat (
SET /P kmpreproc=Found Preprocess.bat. Do you want to run it now?
IF /I "%kmpreproc%"=="Y" (
GOTO PREPROC
)
GOTO CONTINUE
)
)
ECHO C:\Visual\area\%kmname%\%kmname%.flt does not exist. Try again.
SET /P kmname=Enter database name:
GOTO kmstart
:PREPROC
ECHO Running Preprocess.bat.
call "cmd /c start Preprocess.bat"
pause
:CONTINUE
ECHO Continuing process.
PAUSE
If it doesn't work try writting setlocal EnableDelayedExpansion under #echo off and replacing all % with ! .

Resources