Batch if not error - batch-file

I have a problem using if not in batch. I'm creating a little game and I have added an inventory system, to carry things around. Basically, at the moment I have 10 inventory slots, and when I view my inventory I want to only see a slot in the inventory if it has an item in it. By default, I have all the inventory slots set to Empty (I know there is probably a simpler way to do this, if there is please let me know) and this block follows;
:inventory
cls
if not %slotone%==Empty echo %slotone%
if not %slottwo%==Empty echo %slottwo%
if not %slotthree%==Empty echo %slotthree%
if not %slotfour%==Empty echo %slotfour%
if not %slotfive%==Empty echo %slotfive%
if not %slotsix%==Empty echo %slotsix%
if not %slotseven%==Empty echo %slotseven%
if not %sloteight%==Empty echo %sloteight%
if not %slotnine%==Empty echo %slotnine%
if not %slotten%==Empty echo %slotten%
pause
goto %invback%
This returns an error, and I'm not sure why. Any help?

Here is a case insensitive way of comparing text using if not and /i and which protects against spaces, null strings, and poison characters.
if /i not "%slotone%"=="Empty" echo %slotone%

Related

Randomize batch variables

I am trying to make a Yahtzee game and I need to randomize number 1-6 in order to make it echo the specific dice face.
:gameplay
cls
pause
SET /A dice=%RANDOM% %%6+1
if %dice%==1
echo %dice1a%
echo %dice1b%
echo %dice1c%
echo %dice1d%
echo %dice1e%
echo %dice1g%
pause
This is the code I developed. Its not completed yet, this was just a test of the randomizer. Every time I run this, it gets to the first pause, then it quits the program. If you guys know what the problem is, can you tell me, and also I was wondering if it was possible to store multiline variable so I don't have to constantly echo multiple variables. If you guys know anything please tell me. Thanks
EDIT:
I tried what you suggested and did this:
:gameplay
cls
pause
SET /A dice=%RANDOM% %%6+1
if %dice%=1
echo Hi
pause
echo %dice1a%
echo %dice1b%
echo %dice1c%
echo %dice1d%
echo %dice1e%
echo %dice1g%
pause
It didn't follow through to the 2nd pause so I think the variable isn't set properly. I barely noticed this time that it says invalid command syntax right before it quits. Thanks for the suggestion though.
Thanks guys I figured it out, I just was missing the double and quadruple spaces as well as the parenthesis in the if then statement.
SET /A dice=%RANDOM% %%6 +1
if %dice%==1 (
echo %dice1a%
echo %dice1b%
echo %dice1c%
echo %dice1d%
echo %dice1e%
echo %dice1f%
echo %dice1g%
)
I really appreciate everyone who attempted to help me, even if it was just me being stupid, and I really appreciate yacc for helping me the whole way. If you want my game once its finished, I will copy it on to the about me section.

Batch. Trying to make text adventure, if else not working

i am trying to make a text adventure, but i'm kind of clueless how to get an if/else statement to work with a variable. probably a few mistakes in my code:
# echo off
color 0a
echo This is a text adventure! to play, read through the story and at certain points you get to make decisions.
echo Remember, selecting an invalid option will automatically be counted as option 2!
echo have fun
pause
cls
echo You wake up in a dimly lit room. You can't seem to remember anything. Anything about you or how you got here, that is.
echo You walk towards the door. It is locked.
echo 1)Force the door open.
echo 2) look around for another means of escape.
set /p escape=
if %escape% = 1
cls
echo you get the door open, but an orc comes in and smashes your face.
echo get rekt buddy, game over.
pause
exit
else
goto dungeon
:dungeon
cls
echo well done.
pause
exit
If your if statement has has an accompanying else statement, you need to use parentheses. The else must also be in the format ) else (, like this:
# echo off
color 0a
echo This is a text adventure! to play, read through the story and at certain points you get to make decisions.
echo Remember, selecting an invalid option will automatically be counted as option 2!
echo have fun
pause
cls
echo You wake up in a dimly lit room. You can't seem to remember anything. Anything about you or how you got here, that is.
echo You walk towards the door. It is locked.
echo 1)Force the door open.
echo 2) look around for another means of escape.
set /p escape=
if "%escape%"=="1" (
cls
echo you get the door open, but an orc comes in and smashes your face.
echo get rekt buddy, game over.
pause
exit
) else (
goto dungeon
)
exit /b
:dungeon
cls
echo well done.
pause
exit
I've removed the blanks lines for aesthetic purposes; you can put them back in if you really want them. I also added a exit /b because having a goto followed by the label you're going to is bad form. The quotes in the if statement are there to prevent the script from breaking if the user enters nothing.

Batch script closing itself

So, i'm trying to learn batch, and i ran into a problem. I was working on a basic text adventure to learn IF statements and such, and my code is crashing (without an error note) whenever i try to put in the first command, (The input in the ROOM01 label.) this is the beginning -> start of the second label.
#echo off
Title =The Temple=
:menu
set progress=0
color 5
echo ===The Temple===
echo
echo *By Dexter Gard, 2015
echo
echo *Public Domain
echo
echo Press any key
pause
echo
:main
echo ======The======
echo -----TEMPLE----
goto ROOM01
:ROOM01
echo you are in a dark room.
echo there is a DOOR to the EAST.
set/p input=Command?
if %input%==GO EAST goto ROOM02
if %input%==EXAMINE ROOM echo you can't see anything. goto ROOM01
if %input%==EXAMINE DOOR echo A large heavy wooden door. goto ROOM01
:ROOM02
set/p "input=Command?"
if /i "%input%"=="GO EAST" goto ROOM02
Batch regards a "quoted string containing separators like spaces" as a single string. The structure I've shown for the if statement is mandatory using strings which may contain spaces. The /i switch makes the comparison case-insensitive.
If you are entering a string with a set/p, then there's no saying that the data entered doesn't contain Spaces. The way to get over that is to "enclose the strings on both sides of the comparison operator in quotes" - that is, double-quotes 'not single quotes'
The syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned. set /a can safely be used "quoteless".
You have to use quotation marks both in your code and when inputting the command because they consist spaces, OR you can change GO EAST to GO_EASTand then you don't need quotation marks. The if clauses also needed some modification to work.
:ROOM01
echo you are in a dark room.
echo there is a DOOR to the EAST.
set/p input=Command?
if %input%=="GO EAST" (
goto ROOM02
)
if %input%=="EXAMINE ROOM" (
echo you can't see anything.
goto ROOM01
)
if %input%=="EXAMINE DOOR" (
echo A large heavy wooden door.
goto ROOM01
)
:ROOM02

BATCH program crashes after goto command

This code is part of a chat program that I am currently working on. The 'else' part of my program is the one that doesn't work. The program quits instead of going to :home
:join
cls
if not exist "C:/Users/Public/room.cmd" (
echo No room has been found.
echo.
set /p choiceretry=Do you want to retry? y/n
if "%choiceretry%"=="y" goto join
if "%choiceretry%"=="n" goto home
) else (
cls
"C:/Users/Public/room.cmd"
echo A room has been found.
pause >nul
echo Joining
set roomjoined=1
echo %roomjoined%
goto home
)
:home
echo this finally works
pause
I have tried changing the code several times starting from 'echo Joining'
Anyone know why cmd quits?...
:) :) :)
Thanks in advance
The problem is the way you run room.cmd; you must use call to return from it:
call "C:/Users/Public/room.cmd"
Otherwise, execution will not return from room.cmd to the original batch file that ran it.
Hint: Consider to use choice instead of set /P for Y/N decisions.
Firstly, please don't left justify your code blocks. It's much easier to read code that's properly indented.
Secondly, when retrieving values within a code block, you need delayed expansion. See setlocal /? in a cmd prompt for more information. This is the reason for the unexpected behavior. Your variables retrieved within the same parenthetical code block in which they were set won't contain the values you expect unless you retrieve them with delayed expansion syntax. As an alternative, you could use the choice command and if errorlevel, which would result in a bit nicer user experience I think.
Thirdly, when testing user input, you should use the /i switch in your if statements for case-insensitivity. This isn't relevant if using choice / if errorlevel though.
Fourthly, Windows paths use backslashes, not forward slashes.
I'd fix it this way:
#echo off
setlocal
:join
cls
if errorlevel 1 set /P "=Retrying... "<NUL
if not exist "C:\Users\Public\room.cmd" (
echo No room has been found.
echo.
choice /c yn /n /m "Do you want to retry? [y/n] "
if errorlevel 2 goto home
goto join
) else (
"C:\Users\Public\room.cmd"
echo A room has been found.
pause >nul
echo Joining
set roomjoined=1
)
:home
echo this finally works
pause

How to use parentheses in .bat files, set command and IF EXIST

Ok, I have batch file, just a simple one that hides and unhides folders.
I don't see why it cannot seem to execute accordingly;
Here is extended sample code:
#echo off
color a
title Folder/Drive hider Service
:jiki
echo Loading...
TIMEOUT /T 2 >nul
goto inputs
:inputs
echo Enabling security...
TIMEOUT /T 2 >nul
cls
goto menu
:menu
if EXIST "%~dp0\Encryption" (set status=Folder is locked.)
if EXIST "%~dp0\Logan_Documents" (set status=Folder is unlocked, to open it, enter open as your `action.)`
cls
echo.
echo STATUS: %status%
echo.
echo ----------------------------------------
echo FOLDER PROTECTOR by Logan
echo ----------------------------------------
echo.
echo Lock = Lock the folder(s)
echo Unlock = Unlock the folder(s)
echo Credits = For more info
echo V = Display your current version
echo Exit = Close the program
echo.
echo ----------------------------------------
echo For more info, just ask Logan!
echo ----------------------------------------
echo.
echo Select your action, %USERNAME%.
echo.
set /p "menu=>"
if /I %menu%== lock goto lock
if /I %menu%== unlock goto unlock
if /I %menu%== credits goto credits
if /I %menu%== v goto version
if /I %menu%== exit goto exit
goto invalid
and also a lot more, and every time I go to execute the script, it just leaves the status variable blank.
Here's what I've tried.
Reconfiguring all variables through a port, which then sorts based on if exist. doesn't work, just leaves status blank.
Using different variables. (Kinda stupid but I didn't want to think that I have all these errors because of a small typo.) Still left error blank.
Appreciate all efforts to resolve my problem and get this program working!
-Logan
if exist should work fine exactly as you use it. You don't strictly need the quotes, since the names don't include spaces. Also you don't need the parentheses since it is a single command.
But then again, it should work with them as well (I actually tested this), so the only thing I can imagine is that the files or folders are not found because the script is running in the wrong directory. After all you use just the names without any path, so the current directory should contain those files.
The 'current directory' isn't necessarily the directory in which the script is saved. If you are in 'C:\Foo' and you call 'C:\Bar\Script.bat', the current directory will still be 'C:\Foo'. The same goes for starting scripts through a shortcut.
To try this, you can use echo %CD% in your script to echo the current directory.
As a possible solution, you can use %~dp0 to use the actual directory in which the batch script is saved, so you always have a starting point to start from:
REM Check if 'Encryption' exists in the same folder as the batch file.
if EXIST "%~dp0\Encryption" (set status=Folder is locked.)
probably neither of the ifs are true, maybe because the active directory is not what you think it is. you can test this easily by inserting a set status=none above the ifs. or insert dir to see what the scrips actually sees at this point.

Resources