I have a batch-game where there's some questions for one person in particular, I always change the file when she(the person) isn't nearby so its kind of a surprise.
The thing is I keep on displaying questions of different types but sometimes the GOTO or IF statements just simply doesn't work properly, it just follow the line or exit the file.
#ECHO OFF
setlocal EnableDelayedExpansion
ECHO.
ECHO "i just skipped all this echo for you"
pause>nul
:choice
cls
set /p c=Question 1[y/n]
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice
:somewhere
cls
cd "-somewhere in my pc-"
start "-the file-"
cls
goto :somewhere1
:somewhere1
cls
echo.
echo "skip"
pause>NUL
goto :somewhere2
:somewhere2
cls
echo.
set /p c1=question 2
if /I "!c1!"== "option1" goto :correct
if not /I "!c1!"== "option1" goto :somewhere_else1
:correct
cls
echo.
echo "skip"
echo.
echo enter to exit
pause>nul
exit
:somewhere_else1
cls
echo.
echo bad answer
pause>nul
goto :somewhere2
Here are my problems:
When it ask's you the question
:choice
cls
set /p c=Question 1[y/n]
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice
it's just work properly, it wasn't working in the past but I magically fixed it with no idea what i'm doing.
but then I just wanted to make it bigger and add the next lines:
:somewhere2
cls
echo.
set /p c1=question 2
if /I "!c1!"== "option1" goto :correct
if not /I "!c1!"== "option1" goto :somewhere_else1
and now the file is executing perfectly the ":correct" part but no the ":somewhere_else1". in the case of ":somewhere_else1" its just exit the file. in the past was exiting the file the right one too but again I fixed it just writing it again.
The problem is your line if not /I "!c1!"== "option1" goto :somewhere_else1
The /I needs to be before not
if /I not "!c1!"== "option1" goto :somewhere_else1
Though I should also mention that your use of ! instead of % is unnecessary in this example.
You should have a read through this link to get some insight into how to fix these errors yourself in the future, and perhaps this link on good practice.
Related
When you get to the user input part, no matter what I type (desk, fire, door) it always goes to fireplace. Is there something wrong with my if syntax?
#echo off
color C
title RG Text Game
echo -------------------------------------------------
echo Welcome to the Game!
echo.
echo -------------------------------------------------
echo.
pause
echo.
echo Blah bah blah story story
echo What do you want to do?
echo Choices: fire/desk/door
set /p choice=
if %choice%=="fire" GOTO fireplace
if %choice%=="desk" GOTO desk
if %choice%=="door" GOTO door
:fireplace
echo.
echo You come to the fireplace.
echo.
pause
:desk
echo.
echo You go to the desk.
echo.
:door
echo.
echo You go to the door.
echo.
Doublequote %choice% or it will not be equal: desk is not equal as "desk".
And exit your Label block with a goto:eof or exit/b.
Use the /i switch with IF so you can also use DESK or DesK
if /i "%choice%"=="fire" GOTO fireplace
if /i "%choice%"=="desk" GOTO desk
if /i "%choice%"=="door" GOTO door
goto:error
:fireplace
echo.
echo You come to the fireplace.
echo.
pause
exit/b
:desk
echo.
echo You go to the desk.
echo.
exit/b
:door
echo.
echo You go to the door.
echo.
exit/b
You need to protect the entry points of each of your target blocks, otherwise when it's done executing the target block, it will "fall into" the next block.
Before any label line (e.g., :fireplace), you'll need a GOTO to make sure that the program flow doesn't "fall into" the next routine:
#echo off
color C
title RG Text Game
echo -------------------------------------------------
echo Welcome to the Game!
echo.
echo -------------------------------------------------
echo.
pause
echo.
echo Blah bah blah story story
echo What do you want to do?
echo Choices: fire/desk/door
set /p choice=
if /I "%choice%" EQU "fire" GOTO fireplace
if /I "%choice%" EQU "desk" GOTO desk
if /I "%choice%" EQU "door" GOTO door
GOTO END
:fireplace
echo.
echo You come to the fireplace.
echo.
pause
GOTO END
:desk
echo.
echo You go to the desk.
echo.
GOTO END
:door
echo.
echo You go to the door.
echo.
:END
Note also the changes to the IF statements. These allow you to handle the case of typing FIRE or Fire instead of just fire.
In addition to the tips from Jeff and SachaDee,
if you name all your labels equal to the choice you could use a loop
For %%A in (fire desk door) Do If /i "%choice%" equ "%%A" Goto %choice%
Or with a llimited number of choices you could use choice.exe and work with a single letter answer (no enter needed then) and evaluate the returned errorlevel.
I've spent a few days trying to get this batch script to work, but it just does not seem to work properly. It seems to just do whatever it wants after it prompts me to set a variable and i set it.
For example, I might enter n when it says that it doesn't seem to exist, and it will just end the script like it should. But if I re-open it, and it says the same thing as before, and I enter n again, it might just jump to :DeleteCalc, as if I typed y.
Here's my script:
#echo off
:Begin
color fc
title My script
cls
if not exist "C:\calc.exe" (
echo calc.exe doesn't seem to exist. Attempt deletion anyway? ^(Y/N^)
set "calcnotexist="
set /p "calcnotexist="
::This command checks to see if the user inputs a quotation mark. If they do, it echos that quotes cannot be inputted.
setlocal EnableDelayedExpansion
if not !calcnotexist!==!calcnotexist:^"=! set "calcnotexist="
endlocal & if "%calcnotexist%"=="" (
echo ERROR - Quotes cannot be entered.
pause
goto Begin
)
if /i "%calcnotexist%"=="Y" (
echo.
goto DeleteCalc
)
if /i "%calcnotexist%"=="Yes" (
echo.
goto DeleteCalc
)
if /i "%calcnotexist%"=="N" goto End
if /i "%calcnotexist%"=="No" goto End
echo ERROR - Unrecognized input
pause
goto Begin
)
:calcDoesExist
title My script
cls
echo calc.exe found. Delete? ^(Y/N^)
set "calcexist="
set /p "calcexist="
::This command checks to see if the user inputs a quotation mark. If they do, it echos that quotes cannot be inputted.
setlocal enabledelayedexpansion
if not !calcexist!==!calcexist:^"=! set "calcexist="
endlocal & if "%calcexist%"=="" (
echo ERROR - Quotes cannot be entered.
pause
goto calcDoesExist
)
if /i "%calcexist%"=="Y" goto DeleteCalc
if /i "%calcexist%"=="Yes" goto DeleteCalc
if /i "%calcexist%"=="N" goto End
if /i "%calcexist%"=="No" goto End
echo ERROR - Unrecognized input
pause
goto calcDoesExist
:DeleteCalc
cls
echo Deleting...
if not exist C:\calc.exe goto Success
del /f /q C:\calc.exe >nul 2>nul
if not exist C:\calc.exe goto Success
echo Fail!
echo.
echo calc.exe could not be deleted.
echo.
pause
goto End
:Success
echo Deleted!
echo.
echo calc.exe successfully deleted.
echo.
pause
goto End
:End
exit /b
What could I possibly be doing wrong?
Thanks
P.S. I tested this by opening CMD and running the batch script multiple times in there. (but it also doesn't work right when just double clicking it)
If you restructure your script there will be no need for the extended If blocks and therefore no necessity to EnableDelayedExpansion. Also if you use Choice you will not have to do all of the verification of responses.
Example:
#Echo Off
Title My script
Color FC
:Begin
If Exist "C:\calc.exe" GoTo calcDoesExist
Echo(calc.exe doesn't seem to exist.
Choice /M "Attempt deletion anyway"
If ErrorLevel 3 (ClS & GoTo Begin)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo Success
GoTo End
:calcDoesExist
Echo(calc.exe found.
Choice /M "Delete"
If ErrorLevel 3 (ClS & GoTo calcDoesExist)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo DeleteCalc
GoTo End
:DeleteCalc
ClS
Echo(Deleting...
Del /A /F "C:\calc.exe">Nul 2>&1
If Not Exist "C:\calc.exe" GoTo Success
Echo(Fail!
Echo(
Echo(calc.exe could not be deleted.
GoTo End
:Success
Echo(
Echo(Deleted!
Echo(
Echo(calc.exe does not exist.
:End
Echo(
Echo(Exiting...
Timeout 3 /NoBreak>Nul
Exit /B
I have problems with this code, it should only perform an action when the correct input is given.
enter code here
#echo off
echo I want to play a game. Do you?
set /P INPUT=[Y/N]: %=%
If %INPUT%=="Y" goto YES
If %INPUT%=="y" goto YES
If %INPUT%=="N" goto NO
If %INPUT%=="n" goto NO
:NO
echo FOOL!
goto end
:YES
echo Good, good...
goto end
:end
PAUSE
But the input i give doesnt make a difference, also letters like "h" will trigger an reaction. It will perform the "NO" echo everytime. This is because its the first code after the choise section. Does anybody have an idea how to fix this?
You missed the quotes:
If "%INPUT%"=="Y" goto YES
If "%INPUT%"=="y" goto YES
If "%INPUT%"=="N" goto NO
If "%INPUT%"=="n" goto NO
Else you compare Y with "Y".
After
If "%INPUT%"=="n" goto NO
you need to
goto somewhere
otherwise, cmd simply continues processing, line by line, until it reaches a goto or exit or end-of-file.
:somewhere should be just before ypu do you want to play prompt.
BTW, you can use
if /i ".....
to make the comparison case-insensitive, and
"%input:~0,1%"
will return the first character of the string (so a response like "yello" will return y)
Well in order to achieve what you want the following modifications are needed for that to happen.
Here is the original code you programmed:
#echo off
echo I want to play a game. Do you?
set /P INPUT=[Y/N]: %=%
If %INPUT%=="Y" goto YES
If %INPUT%=="y" goto YES
If %INPUT%=="N" goto NO
If %INPUT%=="n" goto NO
:NO
echo FOOL!
goto end
:YES
echo Good, good...
goto end
:end
PAUSE
Here is the modifications that I made to the code to make it run more smoothly:
echo off
goto :menu
cls
:menu
cls
echo I want to play a game. Do you?
echo[
set /P INPUT=[Y/N]: %=%
If %INPUT% equ Y goto :YES
If %INPUT% equ y goto :YES
If %INPUT% equ N goto :YES
If %INPUT% equ n goto :YES
else goto :NO
:NO
cls
echo FOOL!
pause
goto end
:YES
echo Good, good...
goto end
:end
I am assuming that based on the question that you asked that you want the following inputs (Y,y,N,n) to generate a result and anything else will go to :NO section of the code? Your question wasn't really detailed.
I am combining multiple Batch files I made into 1 Batch file.
I have a few questions.
I am going through thousands of pictures and when I come across 1 that I need I copy it to my documents to review later, that's what the "copy" in the script is for. I have it set up to loop so I can just keep entering files to send to my documents.
First. I would like to be able to jump back to the menu when I type Menu, so it would leave the loop and goto :MENU.
Second. I have no idea if this is possible... but going through so many pictures that I would like to be able to save where I left off. Thats where the GOTO :Set comes in to play. I would like to be able to type in the number of the picture and have the Batch file save it, so that when I enter GOTO :OPEN it will then open the picture I left off on.
Sorry if this sounds confusing, any help would be great. If you have any questions feel free to ask
Thank you
ECHO OFF
CLS
:MENU
CLS
ECHO.
ECHO ...............................................
ECHO Welcome to the sub-menu
ECHO ...............................................
ECHO.
ECHO 1 - Rename files in folder.
ECHO 2 - Copy files to My Documents.
ECHO 3 - Set file to you left off on.
ECHO 4 - Open file you left off on.
ECHO 5 - Exit.
ECHO.
SET /P M=Type 1, 2, 3, or 4 then press ENTER:
IF %M%==1 GOTO Rename
IF %M%==2 GOTO COPY
IF %M%==3 GOTO SET
IF %M%==4 GOTO OPEN
:Rename
setlocal EnableDelayedExpansion
set i=0
for %%a in (*.jpg) do (
set /a i+=1
ren "%%a" "!i!.new"
)
ren *.new *.jpg
GOTO MENU
:COPY
cls
SET /P filename=Enter the file which should be moved:
xcopy %filename%.* C:\Users\USERNAME\Documents
if not exist %filename%.* goto :Failure
if exist %filename%.* goto :data
GOTO MENU
:SET
GOTO MENU
:OPEN
GOTO Me
:Failure
echo Failure
pause
goto :COPY
:data
timeout /t 3
goto :COPY
I Figured out another way to leave the loop, now I just need to enter anything that does not match a name in the folder.
This is what I have now and is working Great.
ECHO OFF
CLS
:MENU
CLS
ECHO.
ECHO ...............................................
ECHO Welcome to the sub-menu
ECHO ...............................................
ECHO.
ECHO 1 - Rename files in folder.
ECHO 2 - Copy files to My Documents.
ECHO 3 - Set file to you left off on.
ECHO 4 - Open file you left off on.
ECHO 5 - Exit.
ECHO.
SET /P M=Type 1, 2, 3, or 4 then press ENTER:
IF %M%==1 GOTO Rename
IF %M%==2 GOTO COPY
IF %M%==3 GOTO SET
IF %M%==4 GOTO OPEN
:Rename
setlocal EnableDelayedExpansion
set /p i=Enter Starting Number:
for %%a in (*.JPG) do (
set /a i+=1
ren "%%a" "!i!.new"
)
ren *.new *.JPG
GOTO MENU
:COPY
cls
SET /P filename=Enter the file which should be moved:
xcopy %filename%.* C:\Users\USERNAME\Documents
if not exist %filename%.* goto :Failure
if exist %filename%.* goto :data
:Failure
goto :menu
:data
timeout /t 3
goto :copy
GOTO MENU
:SET
cls
del temp.txt
set INPUT=
set /P INPUT=Type input: %=%
echo Your input was: %INPUT%
pause
echo %INPUT%.JPG >>temp.txt
GOTO MENU
:OPEN
set /p texte=< temp.txt
echo %texte%
pause
set q=%texte%
start %texte%
GOTO menu
:Failure
echo Failure
pause
goto :COPY
:data
timeout /t 3
goto :COPY
So here's my ENTIRE code:
#echo off
cls
color fc
:Start
cls
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
set/p t=Try Again? (Y/N)
if %t%==Y goto Start
if %t%==y goto Start
if %t%==N goto Quit
if %t%==n goto Quit
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
set/p g=Choice:
if %g%== 1 goto KSPEnd
:KSPEnd
start "" "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\KSP.exe"
cls
goto Quit
:Quit
cls
echo Goodbye
Timeout 1
But the code opens the .exe AND a .txt file with exactly the same name. I can't rename the files. So basically i'm asking how to open a specific file type.
Thanks
Instead of starting C:\....\KSP.exe, first go to the right directory, then start KSP:
cd "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program"
KSP.exe
Ok I've got two things for you. Firstly I'll give you you desired solution.
Treat it like an operatable program
rem To start Kerbal Space Program:
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
Thats it. Really.
Secondly:
Use the Choice command
you keep on using set /p where choice would be much better.
Just to be convenient I redid you code with everything I would do. Have fun!
Code :
#echo off
cls
color fc
title -{GAMELOADER}-
:Start
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
set Er=Userid
goto :Crash
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
choice /c yn /m "Try Again?"
if %errorlevel%==1 goto Start
if %errorlevel%==2 goto Quit
set Er=Loop-End_of_stream
goto :Crash
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
set Er=Passid
goto :Crash
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
echo.
Choice /c 1 /m "Game: "
if %errorlevel%==1 goto KSPEnd
set Er=Gameid
goto :Crash
:KSPEnd
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
goto Quit
set Er=End_of_file___UNKNOWN
goto :Crash
:Quit
cls
echo Goodbye
Timeout 1
Exit
:Crash
Rem Always useful :)
Echo Program has crashed Error: %Er%
Pause
Exit
Hope that helped. Mona