I recently played the game "Dictator" and wanted to recreate it in a batch script. I just began creating it when I ran into an issue. I have made programs which allow the user to save their progress and load it. I copied this code from a different program and put it in mine. Saving does seem to work but when i edit the save file (.cmd) and edit one of the saved variables, once I load that file nothing seems to happen. For example I saved one file called key.cmd and edited it to make the "pol" variable equivalent to 100. Once I loaded that file it did say loaded but the pol variable was still set to the default (10). I dont understand as I have made a folder called save and key.cmd file is inside it. I have used my save and load code multiple times in the past and have never had any issues. Please help! Here is my code:
:Setup
#echo off
title Dictator
color 0a
:Save Variables
set pol=10
set bui=10
set low=10
set cor=10
set peo=10
set cri=10
:Main
cls
echo 1 - Save
echo 2 - Load
echo 3 - Police - %pol%
echo 4 - Buissnes Men - %bui%
echo 5 - Lower Government - %low%
echo 6 - Corruption - %cor%
echo 7 - People - %peo%
echo 8 - Criminals - %cri%
choice /c 12345678 /n /m ">>> "
if %errorlevel% equ 1 goto Save
if %errorlevel% equ 2 goto load
:Save
cls
set /p pin="PIN: "
(
echo set pol=%pol%
echo set bui=%bui%
echo set low=%low%
echo set cor=%cor%
echo set peo=%peo%
echo set cri=%cri%
) >> saves\%pin%.cmd
echo SAVED
pause >nul
goto main
:Load
cls
set /p pin="PIN: "
if exist saves\%pin%.cmd (
call saves\%pin%.cmd
echo %pol%
echo LOADED
pause >nul
) else (
echo INCORRECT PIN
pause >nul
)
goto main
At :Load you need to allow spaces in the file name. Most users won't know of this weakness, but in order to allow spaces (and a few other 'special' characters) to be read in the user input without breaking it, you need to add setlocal enableextensions enabledelayedexpansion to the beginning of your batch file and make the set /p pin="PIN: " statement into set /p "pin=PIN: " and replace the %'s with !'s where it calls for %pin%.
In the end, your code should look like:
:Setup
setlocal enableextensions enabledelayedexpansion
#echo off
title Dictator
color 0a
:Save Variables
set pol=10
set bui=10
set low=10
set cor=10
set peo=10
set cri=10
:Main
cls
echo 1 - Save
echo 2 - Load
echo 3 - Police - %pol%
echo 4 - Buissnes Men - %bui%
echo 5 - Lower Government - %low%
echo 6 - Corruption - %cor%
echo 7 - People - %peo%
echo 8 - Criminals - %cri%
choice /c 12345678 /n /m ">>> "
if %errorlevel% equ 1 goto Save
if %errorlevel% equ 2 goto load
:Save
cls
set /p "pin=PIN: "
(
echo set pol=%pol%
echo set bui=%bui%
echo set low=%low%
echo set cor=%cor%
echo set peo=%peo%
echo set cri=%cri%
) >> saves\!pin!.cmd
echo SAVED
pause >nul
goto main
:Load
cls
set /p "pin=PIN: "
if exist saves\!pin!.cmd (
call saves\!pin!.cmd
echo %pol%
echo LOADED
pause >nul
) else (
echo INCORRECT PIN
pause >nul
)
goto main
Related
Trying to obtain an element in a list by its index, using batch script. Here is the code:
#Echo off
setlocal EnableDelayedExpansion
set acc[0]=default
set acc[1]=Account_2
set acc[2]=Account_3
set acc[3]=Account_4
set acc[4]=Account_5
if exist interator.txt (
set /p i=<interator.txt
echo "read: !i!"
echo "!acc[%i%]!"
REM start cmd /c setx AWS_PROFILE !acc[%i%]!
REM start cmd /k python script.py
set /A i=i+1
(echo !i!)>interator.txt
echo "write: !i!"
) else (
(echo 0)>interator.txt
)
Output Received:
"read: 0"
""
"write: 1"
As setx requires the CMD session to be closed, for affect to take place. I am trying a different approach to automate some regular stuff.
Expected Output:
"read: 0"
"default"
"write: 1"
#Echo off
setlocal EnableDelayedExpansion
set "acc[0]=default
set "acc[1]=Account_2"
set "acc[2]=Account_3"
set "acc[3]=Account_4"
set "acc[4]=Account_5"
if exist q65771965.txt (
set /p i=<q65771965.txt
echo "read: !i!"
FOR %%a IN (acc[!i!]) DO (
ECHO "!%%a!"
echo start cmd /c setx AWS_PROFILE "!%%a!"
echo start cmd /k python script.py
)
set /A i=i+1
(echo !i!)
echo "write: !i!"
) else (
(echo 0)
)
GOTO :EOF
OK - small changes to allow this to work on my test environment:
Changed name of file from interator.txt to q65771965.txt (suits my environment)
Removed updating of data file so the modifications are shown on-screen.
Replaced REM start with ECHO start to show the start commands on-screen.
Subtle syntax-oriented change : Use set "var1=data" for setting values - this avoids problems caused by trailing spaces.
Significant change : insert a for loop to transfer indirect values to a metavariable (%%a) and use these.
Possibly-required : I don't use setx much, but I've some memory of the argument's needing to be "quoted"
The problem is, you used echo "%acc[!i!]%" within a codeblock. You need another layer of parsing, like call echo "%%acc[!i!]%%"
As an alternative, restructure your code, so the critical part isn't in a code block:
#Echo off
setlocal EnableDelayedExpansion
set acc[0]=default
set acc[1]=Account_2
set acc[2]=Account_3
set acc[3]=Account_4
set acc[4]=Account_5
if not exist interator.txt (
(echo 0)>interator.txt
goto :eof
)
set /p i=<interator.txt
echo "read: !i!"
echo "%acc[!i!]%"
set /A i=i+1
(echo !i!)>interator.txt
echo "write: !i!"
(this code is functionally identically to yours, just structured in another way)
(btw: it should probably iterator, not interator - but that's only spelling)
New to batch/sql files. Thanks for you help.
What im trying to do:
user picks option 1, runs my sql file.
user picks 2, exits program.
user enters nothing or invalid option, ECHO "invalid option".
Problem: anything I type will exit my program. What am I doing wrong?
ECHO 1 - Show Report
ECHO 2 - Exit
SET choice=""
SET /P input=Your choice:
IF "%input%"=="1" (
GOTO :sql file
) ELSE (
IF "%input%"=="2" (
GOTO :Exit
)
) ELSE (
IF "%input%"=="" (
ECHO Invalid option chosen.
)
)
PAUSE
Just use the choice command, its instructions are available by entering choice /? at the Command Prompt.
#ECHO OFF
ECHO 1 - Show Report
ECHO 2 - Exit
CHOICE /C 12 /M "Your choice"
IF ERRORLEVEL 2 GOTO :EOF
REM This line is your SQL report code
PAUSE
If you still wanted to use, what in your case I believe is the wrong Set /P input method, then something like this should be relatively robust, given that your end user is free to enter whatever they like as input, mischievous or harmful intent included:
#ECHO OFF
ECHO 1 - Show Report
ECHO 2 - Exit
:GETInput
SET "input="
SET /P "input=Your choice: "
2>&1 SET input | FINDSTR /XR "input=[12]" 1>NUL || (
ECHO Invalid option chosen
GOTO GETInput
)
IF %input% EQU 2 GOTO :EOF
REM This line is your SQL report code
PAUSE
There is one extract closing bracket [Line #12] inside nested if..else. Also, as far as i know, your label :sql file should not contain space and you must declare it inside the batch file (Same for label :Exit)
I did few changes in your code to make it work
#echo off
:Main
cls
ECHO 1 - Show Report
ECHO 2 - Exit
SET choice=""
SET /P input=Your choice:
IF "%input%"=="1" (
GOTO :SQLReport
) ELSE IF "%input%"=="2" (
GOTO :EOF
) ELSE (
ECHO.
ECHO Invalid option chosen.
pause
GOTO :Main
)
:SQLReport
ECHO.
ECHO Running SQL Report...
GOTO :EOF
:EOF
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This program stops automatically! When I enter correct name and password (i.e it is not executing from 8th line). Please check if I made any mistakes.
#echo off
:s
set t=0
set /p a= enter your name
set /p b= enter your password
if %a%==andrew(set /a t=%t%+1)
if %b%==123 (set /a t=%t%+2 )
if %t%==3 ( echo welcome andrew
echo ---------------------------------------------------------------------------
:r
set /p c=want to change your settings -yes -no
if %c%==no (echo ok
goto e
)
if %c%==yes (set /p p= what you want to change -name(1) -password(2)
if %p%==1 (set /p a=enter name to change)
if %p%==2 (set /p a=enter password to change)
)
)
if %t%==2 echo %a% is not authorised
if %t%==1 echo enter correct password
if %t%==0 echo Both name and id are wrong enter again
goto r
goto s
:e
pause
Set #echo on and you may see something like this when the script is executed:
--> set t=0
--> set /p a= enter your name
enter your nameandrew
--> set /p b= enter your password
enter your password123
--> if andrew == andrew(set /a t=0+1)
--> if 123 == 123 (set /a t=0+2 )
(echo was unexpected at this time.
--> if ==no (echo ok
The script stopped with error:
(echo was unexpected at this time.
Line 7 showed OK:
if 123 == 123 (set /a t=0+2 )
though, next command output seen is:
if ==no (echo ok
which means %c% is undefined as you can see a value missing left of the == that caused the error.
This could be happening as to the opening parentheses in line 8:
if %t%==3 ( echo welcome andrew
The parser will see the opening parentheses and will keep reading the lines until it reaches a closing parentheses. It will substitute the percentage enclosed variables before execution. This makes %c% undefined as %c% is not defined before the opening parentheses on line 8 began the read.
So it seems the parser read:
if %t%==3 ( echo welcome andrew
echo ---------------------------------------------------------------------------
:r
set /p c=want to change your settings -yes -no
if %c%==no (echo ok
goto e
)
And does variable substitution of known values of t=3 and c=:
if 3==3 ( echo welcome andrew
echo ---------------------------------------------------------------------------
:r
set /p c=want to change your settings -yes -no
if ==no (echo ok
goto e
)
The error of if ==no (echo ok is shown.
Your question in the title:
why i cant get output from this batch script?
It is the opening parentheses in line 8. So one should ask thy self... What is that opening parentheses doing there in line 8 when it will include a label :r and a set /p that sets a variable c that can cause an undefined variable?
If the opening parentheses is meant to be there, view set /? that may show an example like:
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "%VAR%" == "after" #echo If you see this, it worked
)
The example code and the details in set /? informs you how set variables work.
The label between the parentheses would still be a concern as it can cause error in a read block of code.
There is a couple of things. Do not set single character variables or label names. rather use set vara than using set a but making things recognisable like using variables that means something, makes things less confusing.
when matching variables with a value using == double quote each side. i.e if "%var" == "123"
For selection, use choice instead of set /p
Also, when using set /a the better way to add is set /a vart+=1 instead of set /a %vart%+1
This should do more or less what you want.
#echo off
:start
set vart=0
set /p "name=Enter your name: "
set /p "pass=Enter your password: "
if /i "%name%" == "andrew" set /a vart+=1
if "%pass%" == "123" set /a vart+=2
if %vart% equ 3 echo welcome andrew
echo ---------------------------------------------------------------------------
:settings
choice /c YN /m "Change your settings?"
if %errorlevel% equ 0 goto opt0
goto opt%errorlevel%
:opt0
echo You Pressed CTRL+C and selected "N"
exit /b 1
:opt2
pause
goto :eof
:opt1
choice /c UP /m "Change your User or Password?"
if %errorlevel% equ 0 goto opt0
goto chng%errorlevel%
:chng2
set /p "name=Enter name to change: "
goto :eof
:chng1
set /p "pass=Enter password to change: "
if %vart% equ 2 echo %a% is not authorised
if %vart% equ 1 echo enter correct password
if %vart% equ 0 echo Both name and id are wrong enter again
goto settings
goto start
Please read the syntax of the commands, such as if and set before posting.
Addressing some problems in your code:
First,
if %a%==andrew(set /a t=%t%+1)
is intercepted by cmd.exe as:
if %a% is andrew(set, run a command called /a t=%t%+1. Also, please double quote all set /p ...= in your script, even though it can't handle poison characters.
Second, you have unescaped closing parenthesis ) in your if, which can cause interception errors.
The script prompts the user to choose which calculation to perform (call). After I choose an option and calculate the numbers I get random results. Such as 4 + 5 = 27 and 4 * 4 = 0.
#echo off
goto :main
:addition
set /a FNUM1=%~1
set /a FNUM2=%~2
set /a RESULT=FNUM + FNUM2
echo RESULT: %RESULT%
goto :eof
:subtraction
set /a FNUM1=%~1
set /a FNUM2=%~2
set /a RESULT=FNUM - FNUM2
echo RESULT: %RESULT%
goto :eof
:multiplication
set /a FNUM1=%~1
set /a FNUM2=%~2
set /a RESULT=FNUM * FNUM2
echo RESULT: %RESULT%
goto :eof
:division
set /a FNUM1=%~1
set /a FNUM2=%~2
set /a RESULT=FNUM / FNUM2
echo RESULT: %RESULT%
goto :eof
:main
echo Welcome to the calculator
echo Please chose one of the following options:
echo 1 - Addition
echo 2 - Subtraction
echo 3 - Multiplication
echo 4 - Division
echo 5 - Quit
set /p CHOICE=
if "%CHOICE%"=="1" (
echo YOU CHOSE ADDITION!
echo Enter first number:
set /p NUM1=
echo Added by:
set /p NUM2=
call :addition %NUM1% %NUM2%
)
if "%CHOICE%"=="2" (
echo YOU CHOSE SUBTRACTION!
echo Enter first number:
set /p NUM1=
echo Subtracted by:
set /p NUM2=
call :subtraction %NUM1% %NUM2%
)
if "%CHOICE%"=="3" (
echo YOU CHOSE MULTIPLICATION!
echo Enter first number:
set /p NUM1=
echo Multiplied by:
set /p NUM2=
call :multiplication %NUM1% %NUM2%
)
if "%CHOICE%"=="4" (
echo YOU CHOSE DIVISION!
echo Enter first number:
set /p NUM1=
echo Divided by:
set /p NUM2=
call :division %NUM1% %NUM2%
)
if "%CHOICE%"=="5" (
echo Good Bye!
)
goto :eof
I tried to use "/a" for setting FNUM1 and FNUM2, but I still get random results.
Thanks for any advice.
set FNUM1=4
set FNUM2=4
set /a RESULT=%FNUM1%*%FNUM2%
echo RESULT: %RESULT%
RESULT: 16
set /a "RESULT = FNUM1 * FNUM2"
echo RESULT: %RESULT%
RESULT: 16
:main
echo Welcome to the calculator
echo Please chose one of the following options:
echo 1 - Addition
echo 2 - Subtraction
echo 3 - Multiplication
echo 4 - Division
echo 5 - Quit
set "operation="
set "choice="
set /p CHOICE=
if "%CHOICE%"=="1" set "operation=ADDITION"
if "%CHOICE%"=="2" set "operation=SUBTRACTION"
if "%CHOICE%"=="3" set "operation=MULTIPLICATION"
if "%CHOICE%"=="4" set "operation=DIVISION"
if "%CHOICE%"=="5" (
echo Good Bye!
)
if not defined operation echo illegal choice&goto main
echo YOU CHOSE %operation%!
echo Enter first number:
set /p NUM1=
echo Second number:
set /p NUM2=
call :%operation% %NUM1% %NUM2%
goto main
goto :eof
Your primary issue is can be explained by reading some SO items about delayed expansion. In short, within a block (a parenthesised series of lines) any %var% is replaced by the value of var at the time the block is encountered, so changing num? within the block does not alter the values passed to the subroutine - it is num? as it was when the execution of the block began.
Mods: set choice to nothing before executing a set/p because if simply Enter is pushed, the variable will not become empty, but unchanged
establish a new variable, operation and set it to nothing
depending on the choice made (perhaps you should try choice /? from the prompt for a better method) set operation to the appropriate value.
Since each entry produced (nearly) the same text, enter the values, then perform the calculation.
Perhaps you may want to put something like
echo %1 ? %2=%result%
in each subroutine, OR change operation within the subroutine to +, - etc. and in the main routine, after the call :%operation%... (which chooses the subroutine to run) try
echo %num1% %operation% %num2%=%result%
which should fill in operation as set by the routine ":oldvalueofoperation"
Here is my code, I don't want the user to have to press enter to make a choice. I tried a couple of things but nothing I tried worked.
#echo off
:debut mon menu a moi
cls
echo 1.Calculatrice
echo 2.Task manager
echo 3.msconfig
echo 4.Notepad
echo 5.ipconfig
echo 6.quit
echo.
set /p /c choix=" Choisir votre application : "
if "%choix%"=="1" goto menu1
if "%choix%"=="2" goto menu2
if "%choix%"=="3" goto menu3
if "%choix%"=="4" goto menu4
if "%choix%"=="5" goto menu5
if "%choix%"=="6" goto menu6
I didn't include the menu since it's not relevant
#echo off
cls
echo 1.Calculatrice
echo 2.Task manager
echo 3.msconfig
echo 4.Notepad
echo 5.ipconfig
echo 6.quit
echo.
choice /C 123456 /M "Choisir votre application : "
goto menu%errorlevel%
:menu1
echo Calculatrice
. . .
:menu6
:menu0
echo Quit
The choice command returns an errorlevel value between 1 and 6 (or 0 if the user press Ctrl-C), so goto menu%errorlevel% command direclty transfer to the desired label without need of any additional checking. For further details, type: choice /?
#echo off
setlocal enableextensions disabledelayedexpansion
rem OPTION 1 - Use choice command (if available)
echo ---------------------------------------------------
rem Ask for the options
choice /n /c 1234560 /m "Choisir votre application : "
rem Each option sets a errorlevel in the same order they are included.
rem It is necessary to test in decreasing order
if errorlevel 7 (
echo selected 0
) else if errorlevel 6 (
echo selected 6
) else if errorlevel 5 (
echo selected 5
) else if errorlevel 4 (
echo selected 4
) else if errorlevel 3 (
echo selected 3
) else if errorlevel 2 (
echo selected 2
) else if errorlevel 1 (
echo selected 1
)
rem Alternatively, this sintax can be used
rem In this case the order in the tests is not relevant
if "%errorlevel%"=="1" echo selected 1
if "%errorlevel%"=="2" echo selected 2
if "%errorlevel%"=="3" echo selected 3
if "%errorlevel%"=="4" echo selected 4
if "%errorlevel%"=="5" echo selected 5
if "%errorlevel%"=="6" echo selected 6
if "%errorlevel%"=="7" echo selected 0
rem OPTION 2 - Use xcopy to retrieve the key press
echo ---------------------------------------------------
rem Show the prompt
<nul set /p ".=Choisir votre application : "
rem Retrieve the user input
set "choix=" & for /f "delims=" %%a in ('xcopy /l /w "%~f0" "%~f0" 2^>nul') do if not defined choix set "choix=%%a"
set "choix=%choix:~-1%"
rem Echo user input
echo %choix%