How to use set and if statements in batch files - batch-file

So i'm trying to make a small batch program with some sets and ifs, seems easy enough right? Apparently, you can't use "set" and "if" like this.
#echo off
setlocal enabledelayedexpansion
cls
set variableTrue EQU 1
Then continue the code and later in the program do this.
If %variableTrue% EQU 1 goto next
Please note I've tried it with exclamation marks as well.
With the exclamation marks, it's like it completely ignores the statement, even if it's true it will continue as usual. With the percentage signs, it says very quickly before crashing
"1" was not expected at this time.
Or something like that, like I said it barely stayed for half a second.
I always thought you could do it like this as long as there are no conflicting variables.
:start
#echo off
setlocal enabledelayedexpansion
title test
color a
cls
:favnum
cls
echo What is your favorite number?
set /p fn=Favorite Number
If "!fn!" NEQ 13 goto thanks
If "!fn!" EQU 13 goto setvar
:setvar
set "coolestNum==1"
:thanks
cls
If "!coolestNum!"== 1 goto cool
echo Thanks
pause
goto :eof
:cool
echo cool
pause
goto :eof
That doesn't give an error, it just ignores the statement and keeps going as usual.
UPDATE:
After fixing errors this still doesn't work.
When I use exclamation marks it ignores the line, and when I use percentage signs it says:
"
"1 was not expected at this time"

One set of problems is your IF statements. For example, If "!coolestNum!"== 1 goto cool. The quotes are included in the comparison.
You need to be symmetric - either include quotes on both sides
If "!coolestNum!" == "1" goto cool
or neither:
If !coolestNum! == 1 goto cool
The same problem exists with If "!fn!" EQU 13 goto setvar, as well as the line before it.
The other problem you have is set "coolestNum==1" has an extra, unwanted =. The second = becomes part of the value. You only want two equal signs with IF comparisons.
Here is corrected code:
:start
#echo off
setlocal enabledelayedexpansion
title test
color a
cls
:favnum
cls
echo What is your favorite number?
set /p fn=Favorite Number
If "!fn!" NEQ "13" goto thanks
If "!fn!" EQU "13" goto setvar
:setvar
set "coolestNum=1"
:thanks
set coolest
cls
If "!coolestNum!"=="1" goto cool
echo Thanks
pause
goto :eof
:cool
echo cool
pause
goto :eof
But your logic is needlessly convoluted. The following produces the exact same result.
:start
#echo off
setlocal enabledelayedexpansion
title test
color a
cls
echo What is your favorite number?
set /p fn=Favorite Number
if !fn! equ 13 (
set "coolestNum=1"
echo cool
) else echo Thanks
pause
exit /b

Use
set variableTrue=1
rather than
set variableTrue EQU 1
This batch script:
#echo off
setlocal enabledelayedexpansion
cls
set variableTrue=1
echo A
if %variableTrue% EQU 1 goto next
echo B
goto :EOF
:next
echo C
outputs
A
C

Related

Batch: Show last user input

(This is my first post here, so bear with me)
Can you show the last user-input in a batch file? I'm gonna try to keep it simple here.
#echo off
:menu
echo Type 1 to proceed.
set /p example=
if "%example%" == "1" GOTO :proceed
GOTO :error
:proceed
pause
:error
cls
echo You wrote (last user input), that's not correct.
timeout 30
GOTO :menu
I know that I could replace the (last user input) with %example%, but then I'd have to make custom error messages for every category, and there are about 50 of them. It'd be easier with a last input command.
By the way, I've taught myself everything that I know about batch, so my example probably has major issues right now, but it works somehow.
You could centralize all user input into a function (user_input)
:menu1
echo Type 1 to proceed.
call :userInput example
if "%example%" == "1" GOTO :proceed
GOTO :error
:menu2
echo Type 42 to proceed.
call :userInput answer
if "%answer%" == "42" GOTO :proceed
GOTO :error
:userInput
set /p LAST_INPUT=
set "%1=%LAST_INPUT%"
exit /b
:proceed
pause
:error
cls
echo You wrote "%LAST_INPUT%", that's not correct.
timeout 30
GOTO :menu
I don't know how to do it without temp file. TO get the things written int the console you need the doskey /history (this will skip the running of the script itself):
#echo off
setlocal enableDelayedExpansion
set "last="
set "but_last="
doskey /history > log.txt
for /f "tokens=* delims=" %%# in (log.txt) do (
set "but_last=!last!"
set "last=%%#"
)
echo "%but_last%"
del /s /q log.txt >nul 2>nul

IF operator not working in batch

Why does this batch code not work?
I certainly don't know why...
Download: http://tufda.net/downloads/explore/StackOverflow%20files/ultraworld.bat
#echo on
cls
set game=UltraWorld
echo Welcome to %game%!
PAUSE
cls
echo What's your name?:
set /p playername=""
cls
echo Welcome to %game%, %playername%!
PAUSE
:titlescreen
cls
echo Commands:
echo new
echo load
echo credits
echo.
set /p command1=Enter your command here:
IF command1==new (
set /p gamesave1=What name will you give this save?
)
IF command1==load (
echo No.
PAUSE
goto titlescreen
)
IF command1=="credits" (echo Everything - tufda & PAUSE & goto titlescreen)
PAUSE
When you test the variable, you need to either surround it in percentage marks, or surround it with exclamation marks, if
setlocal enabledelayedexpansion
is used.
For example,
IF %command1%==load (
When using the IF command in a batch file, you need to follow the proper syntax.
The correct way to write an IF command is:
IF /I "%command1%" EQU "new" (
The Quotes arround each side will account for an empty value, while the EQU is the comarison type which is equivelant to ==. The comparisons are as follows:
EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal
I used a /I to indicate that the comparison is not case sensitive.

Goto not working in batch file, skips user input

I'm working on a program to backup Fallout 4 Saves, because using console commands can bork a save file this time around. Unfortuanly, while all of the parts are working independently, the little menu I have made isn't working!
For some reason the if %1m% == _ goto _ commands are doing nothing, and the program skips back to the label 1, a feature I put there in case of invalid input.
What's wrong here?
#echo off
title Fallout 4 Save Backup Utility
color 0a
:1
cls
setLocal EnableDelayedExpansion
pushd "C:\FalloutBackup\data\"
set /a count=0
for /d /r %%i in (*.*) do set /a count+=1
popd
echo %count% Backup(s^) currently exist.
echo.
echo.
set /a value=0
set /a sum=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI/1000000
set /a sum=!sum!+!value!
)
#echo Backups files using about: !sum! Mb
endlocal
echo.
echo.
echo Delete all but last backup? y/n?
set /p 1m=
if %1m% == y goto 3
if %1m% == Y goto 3
if %1m% == n goto 2
if %1m% == N goto 2
cls
goto 1
As Squash man Said:
Change your variable 1m to m1. CMD interpreter is not smart enough to know that you are trying to reference an environmental variable and not a argument passed to the batch file. –
I also recommend using quotes for the "%m1%"=="y". Also, the /I parameter after IF makes the answer not cap-sensitive, a big plus.
Another option would be to use CHOICE and ERRORLEVEL for option selection. The relevant code changes would be:
echo.
choice /c yn /m "Delete all but last backup? "
IF ERRORLEVEL 255 goto end
IF ERRORLEVEL 2 goto donotdelete
IF ERRORLEVEL 1 goto dodelete
IF ERRORLEVEL 0 goto end
goto begin
(255 and 0 are there for escapes.)
(You will need to obtain the choice COM file, which is readily available.)

Batch programming name display

#echo off
:start
set CTR=1
:loop
echo Lanz
set /a ctr=%CTR%+1
if ctr LEQ 5(
echo Lanz
) else goto loop
if ctr==5 goto finish
:finish
echo %CTR%
pause
cls
goto start
I just need help on this.
The instruction is that it needs to display the name five times in a loop statement form.
It's difficult because what my teacher gave me is a flowchart, I followed everything to the letter, it's not working. Help
Why not use a for loop?
#echo off
for /l %%a in (1,1,5) do (
echo Lanz
)
pause
If you want to stick with goto, it might look similar to this:
#echo off
set ctr=1
:loop
echo Lanz
set /a ctr=%ctr%+1
if %ctr% LEQ 5 goto loop
echo %ctr%
pause
There's no need for other labels and gotos: simple as do ... while

Why does my CMD/Batch game not work?

Well if you were to put this code onto a batch file:
it doesn't choose a random number (or if it does it always has the same result)
it does't seem to lose or win if you choose to fight
i've been teaching myself coding and this is my first time on a forum so please go easy on me :)
here is my code:
#echo off
title template
color 0F
pause
:menu
cls
echo 1.start
echo 2.instructions
echo 3.exit
set /p answer=Type the number of your option and press enter
if %answer%==1 goto start_1
if %answer%==2 goto instructions
if %answer%==3 goto exit
:exit
echo thanks for playing
pause
exit /b
:instructions
cls
echo instructions
echo.
echo This game is case-sensitive!
echo Just have fun with it!
pause
goto menu
:start_1
set /a s1=%random% * 3 / 32768 + 1
if %s1%==1 goto prefight_1
if %s1%==2 goto prefight_2
if %s1%==3 goto prefight_3
:prefight_1
cls
echo You have discovered 3 Turtles!
echo They dont see you!
set /p answer=would you like to (1)FIGHT or (2)RUN?
if %answer%==1 goto fight_1
if %answer%==2 goto run_1
:fight_1
set /a f1=%random% * 4 / 32768 + 1
if %f1%==1 goto lose_fight_1
if %f1%==2 goto win_fight_1
if %f1%==3 goto win_fight_1
if %f1%==4 goto win_fight_1
:prefight_2
cls
echo You have discovered 3 Turtles!
echo They see you!
set /p answer=would you like to (1)FIGHT or (2)RUN?
if %answer%==1 goto fight_2
if %answer%==2 goto run_1
:fight_2
set /a f2=%random% * 4 / 32768 + 1
if %f2% gtr 4 goto fight_2
if %f2% lss 1 goto fight_2
if %f2%==1 goto lose_fight_1
if %f2%==2 goto lose_fight_1
if %f2%==3 goto win_fight_1
if %f2%==4 goto win_fight_1
:prefight_3
cls
echo You have discovered 3 Turtles!
echo They see you!
echo They seem angry!
set /p answer=would you like to (1)FIGHT or (2)RUN?
if %answer%==1 goto fight_3
if %answer%==2 goto run_1
:fight_3
set /a f3=%random% * 4 / 32768 + 1
if %f3%==1 goto lose_fight_1
if %f3%==2 goto lose_fight_1
if %f3%==3 goto lose_fight_1
if %f3%==4 goto win_fight_1
:lose_fight_1
cls
echo Sorry,You LOST!
echo Thank you for playing!
echo made by: JEREMY
set /p answer==(1)continue or (2)quit?
if %answer%==1 goto start_1
if %answer%==2 goto menu
pause
:run_1
cls
echo You ran away
pause
goto start_1
First off it's better for humans to read when it's "equ" instead of "==".
The set /p VARIABLE= is easier to use when clear so use echo like so:
:Jelly_Attack_1
cls
echo AH! There's a swarm of angry jellyfish!
echo Act fast!
echo.
echo You can (1) fight or (2) RUN
set /p VARIABLE=
if %VARIABLE% equ 1 goto Fight_Jelly_1
if %VARIABLE% equ 2 goto Run
if %VARIABLE% neq 1 goto TEST
The fight part should look something like this:
:Fight_Jelly_1
cls
echo The jellyfish are more annoying than harmful!
echo You have a great advantage!
pause
set /a FIGHTNO=%random%
if %FIGHTNO% gtr 4 goto Fight_Jelly_1
if %FIGHTNO% lss 1 goto Fight_Jelly_1
if %FIGHTNO% equ 1 goto FAILED
if %FIGHTNO% equ 2 goto Fight_Jelly_1_Win
if %FIGHTNO% equ 3 goto Fight_Jelly_1_Win
if %FightNO% geq 4 goto Fight_Jelly_1_Win
Then when wanting to give gold or a reward on winning do this:
:Fight_Jelly_1_Win
cls
echo The fight was easy and you found some gold!
pause
set gold=0 (if you didn't have a gold variable)
set /a gold=gold+5 (to add gold)
goto FRAMENAME
I hope I helped!
I strongly suspect that you have set a variable random. If you've done that, then the value that you set in the environment overrides the magic variable.
You can clear it by set "random="
It's normal practice to use a setlocal after your #echo off. That way, any changes you make to the envirnment are backed out when the batch terminates. Without it, any changes made will remain until they are explicitly changed again.
Personally, I prefer
set /a value=%random% %% limit + 1
to generate a value 1..limit. If for no other reason, it's easier to type.
#echo off
setlocal
rem environment changes made after here will be backed-out when the batch finishes...
....whatever....
The setlocal command sets up a 'local' environment which exists only until the batch ends or an endlocal command is encountered.
see setlocal /? from the prompt for more info.
Your batch appears to work fine once I'd added a win_fight_1 routine.
Note however that batch will barf on set /p answer==(1)continue or (2)quit? but removing one of those = will fix it.
Beyond that, just watch whether you want to go to menu or start_1.
And the
if %f2% gtr 4 goto fight_2
if %f2% lss 1 goto fight_2
will be ineffective - certainly if you've used set /a f2=%random% %% 4 + 1
Fake random--
I confirmed that %random% is driven by system clock-- http://blogs.msdn.com/b/oldnewthing/archive/2010/06/17/10026183.aspx
use the clock MS to get a random number from 1 to 1000
here is one way to get milliseconds: https://answers.yahoo.com/question/index?qid=20100816021807AAz6eL3
Another possibility to introduce randomness is to start the game and then wait for user input i.e. "ready to start?" and when they start will introduce some randomness into the amount of time since cmd.exe started.

Resources