set /a in If statement condition unable to work - batch-file

Below are my simple calculator batch i trying to do, however on the set /a unable to do the job. What is the problem and my mistake?
#echo off
Title Calculator
:start
echo Press 1 for "+"
echo Press 2 for Exit
echo.
set /p input="Please choose your option: "
if %input%==1 (
echo.
set /p num1="Please enter first number: "
set /p num2="Please enter second number: "
set /a ans=%num1%+%num2%
echo Your answer is %ans%
pause
cls
goto start
)
if %input%==2 (
echo.
echo Thanks!
pause
exit
) else echo Invalid input!
pause
goto start
When i first run the batch is will return me the Missing operand. When i continue again the error disappear without giving me the answer, when the third time i continue, it return me the answer of the number that i wanted to add up.
For example:
1. 10+10 result is Missing operand
2. 1+1 result is empty
3. 2+2 result is 20 (which is the 2 number i enter at first time)
Please help what my error is. Thanks.

Here is your batch code with using delayed expansion and indents as wisely suggested by rojo:
#echo off
title Calculator
setlocal EnableExtensions EnableDelayedExpansion
:Begin
echo Press 1 for "+"
echo Press 2 for Exit
echo/
set "input="
set /P "input=Please choose your option: "
if "!input!"=="1" (
echo/
set /P "num1=Please enter first number: "
set /P "num2=Please enter second number: "
set /A ans=num1+num2
echo Your answer is !ans!
pause
cls
goto Begin
)
if "!input!"=="2" (
echo/
echo Thanks^^!
echo/
pause
exit /B
)
echo Invalid input^^!
echo/
pause
echo/
goto Begin
Variable input is always cleared before user is asked because otherwise the user could just hit key RETURN or ENTER to keep current value of variable input.
Delayed expansion is used on checking user input against 1 or 2 in case of user enters a character which would result in a syntax error on execution with not using delayed expansion. Better would be nevertheless the usage of command choice for first user prompt.
For an expression evaluated on runtime by using set /A the environment variables can be specified directly without being expanded at all. So instead of using
set /A ans=%num1%+%num2%
or
set /A ans=!num1!+!num2!
it is enough to write
set /A ans=num1+num2
because with parameter /A command set interprets num1 and num2 automatically as names of variables.
Delayed expansion is nevertheless needed to print the result value stored in variable ans because command processor expands otherwise %ans% by nothing respectively the value of previous run on parsing the entire block starting with ( and ending with ). This can be seen on running your batch file from within a command prompt window with first line changed to #echo on for debugging.
For more details run in a command prompt window set /? or help set and read the output help pages.
start is the name of a command. Therefore it is not good to use this word as name of a label although possible.
By the way: Always use set "variable=value" and never set variable="value" as this makes a big difference, see the answers on
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
How to set environment variables with spaces?

Related

How to show what user typed in this command?

please help! I was looking for the answer all over the internet.
Here's my code:
#echo off
title var test
:question
set a1=This
set a2=Is
set a3=a
set a4=Var
set a5=Test
choice /c 12345 /m "press a number"
if errorlevel=5 set num=5&goto answer
if errorlevel=4 set num=4&goto answer
if errorlevel=3 set num=3&goto answer
if errorlevel=2 set num=2&goto answer
if errorlevel=1 set num=1&goto answer
:answer
echo now change the answer.
set /p a%num%=
FOR /F "tokens=1-6" %%1 IN ("%a1% %a2% %a3% %a4% %a5% a%num%") DO echo %%1 %%2 %%4 %%5.&echo You typed=%%6
pause
goto question
As you can see I made the user select a number between 1 and 5 to change the specific word. But when I try same kind of code to show what he typed doesn't work :(
Environment variables should never begin with a digit and using a digits for loop variables should be also avoided. Run in a command prompt window call /? and output is the help for this command explaining how batch file arguments can be referenced with %0, %1, %2, ... which explains why environment variables with digit as first character and loop variables with a digit are not good in general even on being
a%num% in set of FOR does not reference the value of environment variable a1 or a2 or a3 or a4 or a5. It is just the name of the environment variable. The for loop is not necessary at all.
#echo off
title var test
:question
set "a1=This"
set "a2=Is"
set "a3=a"
set "a4=Var"
set "a5=Test"
%SystemRoot%\System32\choice.exe /C 12345E /N /M "Press a number in range 1-5 or E for exit: "
if errorlevel 6 goto :EOF
set "num=%ERRORLEVEL%"
set /P "a%num%=Now change the answer: "
echo %a1% %a2% %a3% %a4% %a5%.
call echo You typed: %%a%num%%%
pause
goto question
The command line call echo You typed: %%a%num%%% is parsed by Windows command processor before execution of the command line on number 3 entered to call echo You typed: %a3%. This command line is parsed a second time because of command call resulting in replacing %a3% by the value of environment variable a3 and so echo outputs the expected string.
It would be also possible to replace call echo You typed: %%a%num%%% by
setlocal EnableDelayedExpansion
echo You typed: !a%num%!
endlocal
The usage of delayed environment variable expansion results also in double parsing the command line before execution of command echo. For more details see How does the Windows Command Interpreter (CMD.EXE) parse scripts?
Please read also this answer for details about the commands SETLOCAL and ENDLOCAL.
The two lines below in batch code above are also not really good taking into account that the user can really enter anything.
echo %a1% %a2% %a3% %a4% %a5%.
call echo You typed: %%a%num%%%
For example if the user enters number 1 and on next prompt enters:
Your user name is:& setlocal EnableDelayedExpansion & echo !UserName!& endlocal & rem
Then the batch file does something completely different than designed for and outputs the user's account name.
Secure would be the batch code:
#echo off
title var test
setlocal EnableExtensions DisableDelayedExpansion
:question
set "a1=This"
set "a2=Is"
set "a3=a"
set "a4=Var"
set "a5=Test"
%SystemRoot%\System32\choice.exe /C 12345E /N /M "Press a number in range 1-5 or E for exit: "
if errorlevel 6 goto :EOF
set "num=%ERRORLEVEL%"
set /P "a%num%=Now change the answer: "
setlocal EnableDelayedExpansion
echo !a1! !a2! !a3! !a4! !a5!.
echo You typed: !a%num%!
endlocal
pause
goto question
Now the user input string cannot modify anymore the command lines executed by Windows command processor.
A solution with the useless FOR loop would be:
setlocal EnableDelayedExpansion
for /F tokens^=1-6^ eol^= %%A in ("!a1! !a2! !a3! !a4! !a5! !a%num%!") do echo %%A %%B %%C %%D %%E.&echo You typed: %%F
endlocal
eol= is necessary to output everything correct also if use enters number 1 and next a string starting with a semicolon. The FOR options string cannot be enclosed in double quotes in this case like "tokens=1-6 eol=" because of this would define " as end of line character and nothing is output if user enters number 1 and enters next a string starting with ". The equal sign and the space must be escaped with ^ to be interpreted as literal characters by cmd.exe on double parsing the entire for command line before execution of command for.
Note: The FOR loop solution does not work correct on user enters for first variable value the special command line string as posted above. So it is also not really secure.

Batch counter program shutting down

So I am making a counter and I am not sure how to make it work.. I have this right now with some other functions for customization purposes:
set /a current_value=current_value+incremental_value
but it does not work unfortunately..
The whole purpose is to use the pause >nul function so when ever the user presses a key then the screen will show a number go up by the incremental value chosen earlier..
This is the whole script:
#echo off
cls
title Counter
:Incremental_Value
cls
echo./----------------------------------------------\
echo.I Set the Incremental Value then press Enter I
echo.\----------------------------------------------/
echo.
set /p %incremental_value%= [
:Starter_Value
cls
set current_value=%starter_value%
echo./------------------------------------------\
echo.I Set the Starter Value then press Enter I
echo.\------------------------------------------/
echo.
set /p %starter_value%= [
goto Counter
:Counter
cls
echo./-------------------\
echo.I %current_value% I
echo.\-------------------/
echo.
pause >nul
set /a current_value=current_value+incremental_value
goto Counter
Edit: I fixed the shutting down problem, but when you first get to the Counter screen the number does not appear. Once you hit a key it becomes zero (if you set the starting value to zero) then it wont add the incremental value if you continue to press the key.
A very simple issue you had was the improper use of the set /p command. When using set /p, you do not specify the string as set /p %String%= but rather set /p String=. For more information on the set command try typing set /? into a command prompt.
Another issue, not problem is that you have :Incremental_Value & :Starter_Value but you never call or goto them in the script. The only place you properly did this was with goto Counter. Unless you are going to individually goto/call them later, just remove them; or use goto :Starter_Value - exc.
In the future, try using echo( instead of echo. to call a blank space in the window.
Counter.bat
#echo off
title Counter With Incremental Progression
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo º Set the Starter Value then press Enter º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo(
set /p starter_value=Value:
cls
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo º Set the Incremental Value then press Enter º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo(
set /p incremental_value=Value:
Set "current_value=%starter_value%"
:Counter
cls
echo Current Number: %current_value%
echo(
pause >nul
set /a "current_value=current_value+incremental_value"
goto Counter
PS: Switch the file encoding to ANSI for fun boxes - :-)

Searching for specific characters within a string using a for loop to check time format in Batch

I'm writing a script where a user has to put in a time. The issue I'm running into is that the user can enter anything and batch script will accept it. My idea to check for this was to extract where the colon should be in the time inputted and check if it's there with this:
SET check=%utime:2,1%
IF %check%==: [continue with script]
So that it'd skip the first two characters in say 01:00 and extract the semi colon, set it to check and then compare check to see if it was a colon or not. This works great but then I realized that a user could input anything with a colon in the third space and get past. For a first step my idea to check for this was to have a FOR loop check all the five possible spaces to see if they were empty or not with this code:
SET conf=1
FOR /L %%i IN (1,1,5) DO (
echo %%i
SET check=%utime:%%i,1%
IF [%check%] == [] SET conf=0
)
conf would be a true or false variable (0 or 1) that I could then evaluate with an IF statement to then see if it's a valid format. But what ended up happening is that conf was set to 0 but it was not correctly checking the characters within the time variable. Leaving the echo on I could see that check was not correctly being set meaning conf was always set to 0 no matter what was inputted from the user. Here is the code in question in context just in case (This portion is only meant to check for 5 characters being present):
:Start
SET /p utime=Please insert the time (hh:mm):
CALL :CheckTime
IF conf EQU 1 goto :Valid
ECHO Please enter a valid time!
GOTO :Start
:Valid
ECHO Thank you for entering a valid time!
PAUSE
EXIT
:CheckTime
SET conf=1
FOR /L %%i IN (1,1,5) DO (
echo %%i
SET check=%utime:%%i,1%
IF [%check%] == [] SET conf=0
)
EXIT /B
A little bit different approach then Compo.
:: Q:\Test\2018\06\14\SO_50860883.cmd
#Echo off
:Start
Set "MyTime="
SET /p MyTime=Please insert the time (hh:mm):
If not defined MYTime Exit /B 0
CALL :CheckTime "%MyTime%"|| (ECHO Please enter a valid time! &GOTO :Start)
ECHO Thank you for entering a valid time %MyTime% !
ECHO=
goto :Start
:CheckTime passed value not a fixed var
Echo=%~1|Findstr "^[0-9]:[0-5][0-9]$ ^[0-1][0-9]:[0-5][0-9]$ ^2[0-3]:[0-5][0-9]$" 2>&1>Nul || Exit /B 1
To not allow the input of a single digit hour change the last line to :
Echo=%~1|Findstr "^[0-1][0-9]:[0-5][0-9]$ ^2[0-3]:[0-5][0-9]$" 2>&1>Nul || Exit /B 1
Perhaps using FindStr would be suitable:
:Start
Set "uTime="
Set /P "uTime=Please insert the time (hh:mm): "
Set "uTime=%uTime:~,5%"
If Not Defined uTime GoTo :Start
Echo %uTime% |FindStr /R "[0-1][0-9]:[0-5][0-9] 2[0-3]:[0-5][0-9]">Nul||GoTo :Start
Echo Thank you for entering a valid time!
Pause

.Bat code only creates file for one set of data entered

I want to create a bat file asking for a user input which will ask for some choices:
#echo off
MKDIR D:\BatFiles\File
SET /P Output="D:\BatFiles\File"
ECHO Select Task
ECHO ==========
#echo off
title Task List Creator
:homescreen
ECHO
Echo 1.) Create Notepad Task File
Echo 2.) Exit
Echo.
set /p input=Type Choice :
if "%input%"=="1" goto getInfo
if "%input%"=="2" exit
Pause
:getInfo
set /p VarOne=Enter Type:
set /p VarTwo=Enter Number:
set /p VarThree=Enter Name:
echo Task Type=%VarOne% >> %Output%\test.txt
echo Task Number=%VarTwo% >> %Output%\test.txt
echo Task Name=%VarThree% >> %Output%\test.txt
echo Entry successfully written
Pause
goto finished
:finished
echo Do you want to create a new set of entry?
set /p response= Y or N?
if "%response%"=="Y" goto getInfo
if "%response%"=="N" goto homescreen
--The problem with this code is that I want to create more than 2 entries. This code only creates an output file if user has only one set of entries. If user creates 2 or more, the output file is not created and data entered appears only when user runs the bat file again and only enters one set of data. Sorry about the lame question, I'm just a batch file beginner here.
Look on this code:
#echo off
title Task List Creator
setlocal EnableExtensions EnableDelayedExpansion
set "OutputFolder=D:\BatFiles\File"
set FileNumber=0
:HomeScreen
cls
echo Select Task
echo ===========
echo.
echo 1 ... Create Notepad Task File
echo 2 ... Exit
echo.
set "Input=2"
set /P "Input=Your choice: "
if "!Input!"=="1" goto PrepareTaskFile
if "!Input!"=="2" endlocal & goto :EOF
goto HomeScreen
:PrepareTaskFile
set /A FileNumber+=1
set "OutputFile=%OutputFolder%\test%FileNumber%.txt"
if exist "%OutputFile%" del "%OutputFile%"
:GetInfo
echo.
set "VarOne="
set "VarTwo="
set "VarThree="
:EnterType
set /P "VarOne=Enter type: "
if not defined VarOne goto EnterType
:EnterNumber
set /P "VarTwo=Enter number: "
if not defined VarTwo goto EnterNumber
:EnterName
set /P "VarThree=Enter name: "
if not defined VarThree goto EnterName
if not exist "%OutputFolder%" mkdir "%OutputFolder%"
echo Task Type=!VarOne!>>"%OutputFile%"
echo Task Number=!VarTwo!>>"%OutputFile%"
echo Task Name=!VarThree!>>"%OutputFile%"
echo.
echo Entry successfully written.
echo.
pause
echo.
echo Do you want to create a new set of entry?
echo.
set "Response=N"
set /P "Response=Y or N? "
if /I "!Response!"=="Y" goto GetInfo
goto HomeScreen
The environment variable on prompt keeps its current value if the user just hits RETURN or ENTER. Therefore it is advisable to define a default value or undefine a variable before prompting the user.
The entered strings assigned to the variables are referenced with usage of delayed expansion in case of user enters something not expected which could result in a syntax error and therefore exit of batch processing on referencing the entered strings with immediate expansion. For example a string comparison would fail with a syntax error if the user enters a string with a double quote.
See How to set environment variables with spaces? why using double quotes as it can be seen here on set "variable=string value" and set /P "variable=prompt text".
The space character left of redirection operator >> in code of question would be also written into the file. This should be avoided by removing it and reference the variables with delayed expansion in case of the variable value is a number with value 1, 2, 3, ... to avoid a wrong handle redirection, see the Microsoft article about Using command redirection operators.
On usage of set /P for a menu instead of command choice it must be always taken into account that the user enters something not suggested. So if the user enters on first prompt whether 1 nor 2, there must be code which defines the behavior on the invalid input.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cls /?
del /?
echo /?
endlocal /?
goto /?
if /?
mkdir /?
pause /?
set /?
setlocal /?
title /?
See also answer on Single line with multiple commands using Windows batch file for an explanation of & between endlocal and goto :EOF.

Batch file variable scope issue

I'm having a strange variable scope issue when trying to create a 'dos' (windows 7 command line) batch file which performs a bit of string manipulation to create new file paths. Can anyone see why the OUTPUT_FILENAME variable always ends up being null in the example below?
echo Enter the Data Input, S (Site) or U (User)
set /p DATA_TYPE=
echo.
echo Enter the Input File Name
set /p INPUT_FILENAME=
echo.
IF /I %DATA_TYPE%==u (
set OUTPUT_FILENAME=%INPUT_FILENAME:\users\=\Users\Outputs\%
set OUTPUT_FILENAME=%OUTPUT_FILENAME:xls=txt%
echo Output:
echo %OUTPUT_FILENAME%
)
IF /I %DATA_TYPE%==s (
set OUTPUT_FILENAME=%INPUT_FILENAME:\sites\=\Sites\Outputs\%
set OUTPUT_FILENAME=%OUTPUT_FILENAME:xls=txt%
echo Outputs:
echo %OUTPUT_FILENAME%
)
Thanks in advance for any assistance, this is driving me nuts!
You need to enable the delayed expansion:
setlocal EnableDelayedExpansion
echo Enter the Data Input, S (Site) or U (User)
set /p DATA_TYPE=
echo.
echo Enter the Input File Name
set /p INPUT_FILENAME=
echo.
SET OUTPUT_FILENAME=Empty
IF /I %DATA_TYPE%==u (
set OUTPUT_FILENAME=!INPUT_FILENAME:\users\=\Users\Outputs\!
set OUTPUT_FILENAME=!OUTPUT_FILENAME:xls=txt!
echo Output:
echo !OUTPUT_FILENAME!
)
IF /I %DATA_TYPE%==s (
set OUTPUT_FILENAME=!INPUT_FILENAME:\sites\=\Sites\Outputs\!
set OUTPUT_FILENAME=!OUTPUT_FILENAME:xls=txt!
echo Outputs:
echo !OUTPUT_FILENAME!
)
As the help for the SET command states:
Delayed environment variable expansion is useful for getting around the limitations of the current expansion which happens when a line of text is read, not when it is executed.
So, you need to use the delayed expansion to make sure that INPUT_FILENAME OUTPUT_FILENAME's value are expanded at execution time.
As Laf has correctly indicated, the code, as is, needs delayed expansion. In batch files, when a line or a block (all the lines enclosed in parenthesis) is reached, before being executed, it is parsed. In this parse phase, each variable read is replaced with the value the variable has before the execution starts.
If inside a block you change a variable, and want to access this changed value inside the same block, you need delayed expansion. The code in Laf answer reflect how to do it
Or, if it is possible, you can change your code to not need it
echo Enter the Data Input, S (Site) or U (User)
set /p DATA_TYPE=
echo.
echo Enter the Input File Name
set /p INPUT_FILENAME=
echo.
IF /I %DATA_TYPE%==u (
set OUTPUT_FILENAME=%INPUT_FILENAME:\users\=\Users\Outputs\%
)
IF /I %DATA_TYPE%==s (
set OUTPUT_FILENAME=%INPUT_FILENAME:\sites\=\Sites\Outputs\%
)
set OUTPUT_FILENAME=%OUTPUT_FILENAME:xls=txt%
echo Output:
echo %OUTPUT_FILENAME%
Now, there are variables changed inside blocks, but the values are then accessed out of the blocks.

Resources