I have come across a big problem while testing my code. And this problem is the annoying message ... "Maximum recursion depth exceeded". Look how it works:
#echo off
REM ---------- MAIN MENU ----------
:Main
echo 1.Supermarket
echo 2.Exit Batch
setlocal
for /f %%A in ('"prompt $H & echo on & for %%B in (1) do rem"') do set "BS=%%A"
set /p Menu=%BS% Type in your option {1,2} followed by ENTER:
if not '%Menu%'=='' set Menu=%Menu:~0,1%
if %Menu% EQU 1 ENDLOCAL & goto Super
if %Menu% EQU 2 goto EOF
REM ---------- SECONDARY MENU ----------
:Super
echo 1.Supermarket
echo a.Apple
echo b.Banana
setlocal
for /f %%A in ('"prompt $H & echo on & for %%B in (1) do rem"') do set "BS=%%A"
set /p Menu=%BS% Type in your option {1,a,b} followed by ENTER:
if not '%Menu%'=='' set Menu=%Menu:~0,1%
if %Menu% EQU 1 ENDLOCAL & goto Main
if %Menu% EQU a ENDLOCAL & goto App
if %Menu% EQU b ENDLOCAL & goto Ban
:App
setlocal enabledelayedexpansion
set /a cnt=0
for /f "tokens=* delims= " %%a in (*.apple) do (set /a cnt+=1)
if %cnt% EQU 0 (
echo There are %cnt% apples & pause>nul & ENDLOCAL & goto Main
) else (
echo There are %cnt% apples
[... do many things and after 200 lines]
echo The total number of apples was %cnt% & pause>nul & ENDLOCAL & goto Main
:Ban
echo This is Banana & pause>nul & goto Main
:EOF
Let's suppose that the user follows this sequence in a row :
1 --> a (First time) --> 1 --> a (second time) --> 1 --> a (third time) etc etc etc
1 --> a (eighth time) --> 1 --> a (ninith time) and, at this moment, you will get the "Maximum recursion depth exceeded". message. If you just keep ignoring this message, the internal calculations of your code will start to behave strangely, even tough it "apparently" seems to have been yielding good results
In fact, the order does not matter. The same thing happens if you keep varying between letter "a" and "b" or more "a" than "b" as:
1 --> b (First time) --> 1 --> a (second time) --> 1 --> a (third time) ...
1 --> a (eighth time) --> 1 --> b (ninth time). Again the "Maximum recursion depth exceeded". message
In addition, if the user types in the option 1 relentlessly (invariably staying at MAIN MENU only) the error message pops out after the twelfth trial.
How to keep the same code structure as above but avoid this error? I am simplifying here but I am talking about a batch of several hundred lines of code and several secondary, tertiary, quaternary etc submenus.
Thanks,
Maleck
I don't see any recursion in the code you posted, and not surprisingly, I can't get the code to fail.
You must have recursion in code that you are not showing that is causing the problem.
I am aware of two types of fatal recursion errors with batch files:
1) SETLOCAL is limited to a maximum of 32 levels. Attempting to use SETLOCAL a 33rd time without issuing an ENDLOCAL will result in the following fatal error message:
Maximum setlocal recursion level reached.
I suspect this is the recursion limit you are reaching.
UPDATE: Actually, the limit is 32 SETLOCAL per CALL level. For more information, read the entire thread at http://ss64.org/viewtopic.php?id=1778
2) CALL recursion can fail if you issue too many CALLs before any of them return. Batch returns from a CALL whenever it reaches the end of the script, EXIT /B, or GOTO :EOF. The maximum number of recursive CALLs is machine dependent. (Perhaps code dependent?) The error message will look something like this:
****** B A T C H R E C U R S I O N exceeds STACK limits ******
Recursion Count=600, Stack Usage=90 percent
****** B A T C H PROCESSING IS A B O R T E D ******
There may be some system configuration that could increase the number of recursive CALLs you can make, but there will always be some upper limit.
If you are reaching a recursion limit, then you really don't have much choice other than to restructure your code. But unless you show your recursive code, it is difficult (nigh impossible as far as I'm concerned) for us to suggest a way for you to work around the problem.
EDIT in response to updated question and comment
Here is a general rule that you need to follow: If you have a code loop then every SETLOCAL must be paired with an ENDLOCAL. The loop could be created by GOTO, or it could be a FOR loop.
You are making heavy use of GOTO loops. You successfully identified that after issuing SETLOCAL you must issue ENDLOCAL before you execute a GOTO that loops back. It looks to me like you solved that problem.
One last EDIT - Use CALL to simplify the logic
It is a pain to keep track of exactly where and how many times you must issue ENDLOCAL when you loop with a GOTO. The logic is much simpler if you use CALL instead, and the code is more structured as well.
All SETLOCAL statements that were issued during execution of a CALLed subroutine are implicitly ended once the routine ends. There is no need to explicitly use ENDLOCAL when using CALL.
Below I've shown how your code could be restructured to use CALL. Study all of the code - I've made a number of subtle changes that I think simplify and/or improve other aspects of your logic.
#echo off
setlocal
:: Define the BS variable just once at the beginning
for /f %%A in ('"prompt $H & echo on & for %%B in (1) do rem"') do set "BS=%%A"
:Main ---------- MAIN MENU ----------
:: ECHO( echoes a blank line
echo(
echo Main Menu
echo 1.Supermarket
echo 2.Exit Batch
:: If user hits ENTER without entering anything then current value remains.
:: So clear the value to make sure we don't accidently take action based
:: on prior value.
set "Menu="
set /p Menu=%BS% Type in your option {1,2} followed by ENTER:
if defined Menu set "Menu=%Menu:~0,1%"
if "%Menu%" EQU "1" call :Super
if "%Menu%" EQU "2" exit /b
goto :Main
:Super ---------- SECONDARY MENU ----------
setlocal
echo(
echo Supermarket
echo a.Apple
echo b.Banana
echo 1.Retutn to Main
set "Menu="
set /p Menu=%BS% Type in your option {1,a,b} followed by ENTER:
if defined Menu set "Menu=%Menu:~0,1%"
:: Use IF /I option so case does not matter
if /i "%Menu%" EQU "1" exit /b
:: Note that :App and :Ban are still logially part of this subroutine
:: because I used GOTO instead of CALL. When either :App or :Ban ends, then
:: control will return to the Main menu
if /i "%Menu%" EQU "a" goto :App
if /i "%Menu%" EQU "b" goto :Ban
goto :Super
:App
setlocal enableDelayedExpansion
set /a cnt=0
for /f "tokens=* delims= " %%a in (*.apple) do (set /a cnt+=1)
echo There are %cnt% apples
if %cnt% GTR 0 (
REM ... do many things and after 200 lines
echo The total number of apples was %cnt%
)
pause>nul
exit /b
:: The EXIT /B above returns to the Main menu (ends the CALL to :Super)
:: Note that the SETLOCAL at the start of :Super and the one at the
:: start of :App are implicitly ended. No need to explictly issue
:: ENDLOCAL.
:Ban
echo This is Banana
pause>nul
exit /b
:: The EXIT /B above returns to the Main menu (ends the CALL to :Super)
:: Note that the SETLOCAL at the start of :Super is implicitly ended.
:: No need to explictly issue ENDLOCAL.
Related
I'm creating a D&D style RPG game using batch files and I saw another person using a login feature that creates a batch file that returns you to where you left off. so I tried to replicate it. Almost everything works except for the fact that it won't create the batch file to allow you to go back. When you go through the process of creating a login it goes through it like normal and on the next screen under start even displays that your username is what you set it as but it won't create the batch file. Some of the code might be scattered because I'm still learning and some of it unfinished but this is what I have so far.
title Lost Mine of Phandelvor
#echo off
:entergame
cls
echo.
echo Welcome to Lost Mine of Phandelvor
echo -------------------
echo.
echo 1. Create Account
echo 2. Login
echo 3. Exit
echo.
set /p input=
if %input% EQU 1 goto createuser
if %input% EQU 2 goto login
if %input% EQU 3 exit
if %input% GEQ 4 goto entergame
:createuser
cls
echo.
echo What would you like your Username to be?
set /p username1=
set v1f=0
goto checkforspaces
:checkforspaces
set x=!v1f!
set Letter%v1f%=!username1:~%x%,1!
if "!Letter%v1f%!" EQU " " (
echo.
echo.
echo Sorry you can't use spaces in your Username.
pause>nul
goto entergame
)
if NOT "!Letter%v1f%!" EQU "" (
set /a v1f=%v1f%+1
)
echo.
echo What would you like your Password to be?
set /p password1=
goto DATA_VALUES
:login
cls
set /p name=Username:
if not exist "%name%.bat" (
echo That is not a valid Username.
pause>nul
goto entergame
)
set /p pass1=Password:
call %name1%.bat
if not %password1% EQU %pass1% (
echo That is not a valid Password.
pause>nul
goto entergame
)
goto create
:DATA_FILES
set lvl1=1
set exp1=0
set expmax1=300
set gp1=10
set hp1=12
set ac1=15
set profbonus1=2
set str1=4
set dex1=1
set con1=2
set int1=0
set wis1=2
set cha1=2
set destination=SAVE_GAME_FILES
set destination2=SAVE_GAME_FILES
goto SAVE_GAME_FILES
:SAVE_GAME_FILES
(
echo set username1=%username1%
echo set password1=%password1%
:DATA_VALUES
echo set lvl1=%lvl1%
echo set exp1=%exp1%
echo set expmax1=%expmax1%
echo set gp1=%gp1%
echo set hp1=%hp1%
echo set ac1=%ac1%
echo set profbonus1=%profbonus1%
echo set str1=%str1%
echo set dex1=%dex1%
echo set con1=%con1%
echo set int1=%int1%
echo set wis1=%wis1%
echo set cha1=%cha1%
echo set destination=%destination%
echo set destination2=%destination2%
)>%username1%.bat
goto start
:start
cls
echo.
echo Currently logged in as %username1%
echo.
echo Welcome to my fantasy style role playing game.
echo You will need to create a character.
echo.
echo Enjoy!
echo.
echo 1. Continue to Character Selection
echo 2. Exit
echo.
set /p input=
if %input% EQU 1 goto create
if %input% EQU 2 exit
if %input% GEQ 3 goto start
:create
cls
echo.
echo Welcome to Character Creation
echo.
echo Pick your race!
echo.
echo 1. Human
echo 2. Dwarf
echo 3. Elf
echo 4. Dragonborn
echo 5. Tiefling
echo.
set /p input=Choice:
if %input%==1 goto createHuman
if %input%==2 goto createDwarf
if %input%==3 goto createElf
if %input%==4 goto createDragonborn
if %input%==5 goto createTiefling
goto create
:createHuman
cls
echo.
echo You have chosen Human as your race!
echo.
echo Choose your class
echo.
echo 1. Fighter
echo 2. Ranger
echo 3. Rogue
echo 4. Wizard
echo.
set /p input=Choise
if %input%==1 goto humanFighter
if %input%==2 goto humanRanger
if %input%==3 goto humanRogue
if %input%==4 goto humanWizard
goto createHuman
:humanFighter
cls
echo.
echo You have chosen Fighter as your class!
echo.
echo Choose your weapon
echo.
echo 1. Battleaxe 1D8 Slashing
echo 2. Longsword 1D8 Slashing
echo 3. Rapier 1D8 Piercing
echo.
set /p input=choise
if %input%==1 goto humanFighter1
if %input%==2 goto humanFighter2
if %input%==3 goto humanFighter3
goto humanFighter
:humanfighter1
set lvl1=1
set exp1=0
set expmax1=300
set gp1=10
set hp1=12
set ac1=15
set profbonus1=2
set str1=4
set dex1=1
set con1=2
set int1=0
set wis1=2
set cha1=2
cls
echo.
echo To find your stats open your character sheet labeled humanfighter1_cs.txt
echo.
echo What's your name?
echo.
set /p name1=Enter:
goto hf1main
:hf1main
cls
echo.
echo %name1% Human Fighter
echo Lvl: %lvl1% Money:%gp1%
echo Hit Points: %hp1%/12
echo Armor: Breastplate Armor Class: %ac1%
echo Exp: %exp1%/%expmax1%
echo Weapon: Battleaxe 1D8 Slashing
echo Stat Modifiers:
echo Strength: +%str1%
echo Dexterity: +%dex1%
echo Constitution: +%con1%
echo Intelligence: +%int1%
echo Wisdom: +%wis1%
echo Charisma: +%cha1%
echo -------------------------------------
echo 1) Continue
echo 2) Exit
echo.
set /p input=Enter:
if %input%==1 goto hf1continue
if %input%==2 exit
I expect it to create the batch file after creating an account so that you can log in later.
For your menus, I would like to introduce you to choice.exe as an alternative to Set /P. It is far better to use it when input must be any one of a small set of known values. Set /P allows the end user to enter nothing or anything, and in order to maintain control you need to build in some input verification mechanism. To find out how choice.exe works, open a cmd.exe window and enter choice /? at the prompt.
Here is a rewritten example snippet of your script, (lines 1-43) to hopefully explain why:
#Echo Off
Title Lost Mine of Phandelvor
:EnterGame
ClS
Echo(
Echo Welcome to Lost Mine of Phandelvor
Echo ----------------------------------
Echo(
Echo 1. Create Account
Echo 2. Login
Echo 3. Exit
Echo(
Choice /C 123
If ErrorLevel 3 Exit /B
If ErrorLevel 2 GoTo Login
:CreateUser
ClS
Echo(
Set "username1="
Set /P "username1=What would you like your Username to be? "
If Not Defined username1 GoTo CreateUser
:CheckForSpaces
If Not "%username1: =%"=="%username1%" (
Echo Sorry you can not use spaces in your Username.
Choice /M "Is %username1: =%" okay"
If ErrorLevel 2 GoTo CreateUser
)
If Exist "%username1%.bat" GoTo :Login
Echo(
Set "password1="
Set /P "password1=What would you like your Password to be? "
However, before you continue with your script, based upon what I said about using Set /P. The end user can currently enter anything they want as username1. We've already checked for no entry and for spaces, but you're also saving the name as a filename too, %name%.bat. Along with the decimal character codes 0 through 31, Windows filenames cannot contain any of the following characters, \/:*?"<>|, so you'd need to incorporate further verification of username1. This verification procedure would need to be implemented before line 31 of the code above.
Before you do so however, you may want to consider changing your methodology and not saving the Username to the name of a file. The reason for this is that you'd also be wise not to use filenames ending with a ., or any named CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9 either. So you would further need to include those verification checks of the input too.
Don't forget, you'll probably want to include a verification method for input entries to the %password1% prompt too!
Given the complexity involved with user input and Set /P you can see why I have recommended choice.exe as a control measure for your menus.
Ooh - so many errors!
First, add an extra line setlocal enabledelayedexpansion directly after the #echo off line. This has two effects - first, when your batch ends, your environment is restored to its original condition so that you don't get confused with variables that have been established by prior runs. second, it turns on delayed expansion mode - which is where the !variable! syntax you are using is activated, otherwise ! is simply an ordinary character.
Next, having accepted input from the ketboard with a set /p, you have no idea whether the user entered. In your validation if statements, use if "%var%"=="value" which overcomes most input problems.
Next, 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.
And a little tip for games that need a save/restore feature:
If you reserve a character as a prefix for variables-you-want-to-save (eg all variables I want to save/reload start with #) then all you need to save a game is
set #>"mygamefile.txt"
and all you need to reload a game is
for /f "usebackqdelims=" %%a in ("mygamefile.txt") do set "%%a"
To zap all # variables (useful before reloading a game) use
for /f "delims==" %%a in ('set # 2^>nul') do set "%%a="
You should look at the thousands of code examples here on SO to find out how to use subroutines - you appear to be using "spaghetti code".
This code may help with your password-checking:
for /f "tokens=1,2" %%a in (passwordfilename) do if "%%a"=="%playername%" set "playerpass=%%b"
if "%playerpass%"=="%enteredpass%" goto gameon
This uses a single password file of the form
player1 password1
player2 password2
If player1 changes password, then append the new data to the file like this:
player1 password1
player2 password2
player1 newpassword
and the above for /f construct will then set playerpass to the last occurrence of player1 found.
This should provide you with a start and obviate your need for a subsidiary batch file. You'll have enough on your plate implementing these suggestions, but you should be able then to see the wood for the trees.
And implement changes one at a time, then test and debug them. Small steps will make the process a lot easier.
I'm using the same thing in my game. You need 2 functions, 1 to load and 1 to save. The save function should look something like this:
:: My save thing
:save
(
echo %var1%
echo %var2%
) > save.sav
and the loading portion should look something like this:
< save.sav (
set /p var1=
set /p var2=
)
also, one major recommendation, put all your scripts into separate files and folders. Like a normal game, example:
- MainGame
- Launcher.bat
- Resources
- Scripts
- Save.bat
- Load.bat
- Fight.bat
- Assets
- Title.txt
this is a similar structure to what I used in my game.
Main_Control is the parent batch.
It calls batches named DEFAULT, SETOPTIONS AND USEROPTIONS.
DEFAULT sets a variable %def_set% to either 1 or 2. %def_set% is used in USEROPITONS to set some other variables which are used in SETOPITONS. After DEFAULT, in the MAIN_CONTROL, a test of %def_set% returns error "'1' (or '2') is not recognized as an internal or external command, program or batch" for both %def_set%=1 or 2. It doesn't seem to pass the variable correctly. What have I done wrong?
------------------------CODE-------------------------
"Main_Control" = batch1
#ECHO off
CLS
MODE CON:cols=130 lines=75
REM This particular file is the control script which CALLs the subroutines to perform the back or restore of settings.
:skipintro
REM 1. Get backup location and subroutine location/names.
REM 2. Clear screen
REM 3. Display the notes text file in the window (must be paused).
REM 4. Pause to read notes
REM 4. Get user name
REM 6. "START1" label. Start point if the user wants to rerun backup/restore in same session.
REM 7. Ask if user wants to backup or restore
REM 8. Ask what settings to backup/restore
REM 9. Ask if the user wants to use default settings
REM 10. If user wants to change default settings, provide options to change.
REM 11. Set the options to use throughout the backup/restore.
CALL "%~DP0"_BackupLocation_Subroutines.bat
CLS
TYPE %NOTES%
Timeout /t 30
CALL %GETUSERNAME%
:START1
CALL %B_R_OPTION%
CALL %MENU%
CALL %DEFAULTS%
rem test1
echo before & %def_set% & pause
rem END TEST1
CALL %USEROPTIONS%
REM TEST2
echo after & %def_set% & pause
REM END TEST2
CALL %SETOPTIONS%
Echo You are back in %~n0.
Echo You chose action: %ACTION%
Echo You chose to %ACTION%: %SELECTION%
Timeout /T 5
:: Create (for backups) or verify existence (for restores) the Main Backup and Registry Folders.
SETLOCAL ENABLEDELAYEDEXPANSION
SET FOLDERS= ^
%BACKUP_FOLDER% ^
%REGISTRY_FOLDER%
IF %ACTION%==Backup CALL %CREATE%
IF %ACTION%==Restore CALL %VERIFY%
ETC ETC ETC
%DEF_set% has to pass from DEFAULTS to USEROPTIONS and SETOPTIONS. But it is not.
DEFAULTS batch
#echo off
REM Name: DEFAULTS
REM Asks user to use defaults and have no more prompts or to be prompted for inputs.
:def_input
CLS
ECHO Would you like to use default values?
ECHO Backup location: %Backup_Folder% (cannot modify at this time)
ECHO Overwrite previous backup: No
ECHO Rename previous backup with date/time: Yes
ECHO Prompt to overwrite files: No (no option to change)
ECHO Prompt to delete files: No
ECHO Generate a log: Yes
ECHO Overwrite or append to an existing log: Overwrite
ECHO Log file located in backup location: Yes (no option to change)
ECHO.
ECHO 1. Yes
ECHO 2. No
ECHO.
Set /P Def_SET= Choose:
ECHO.
FOR %%w in (1 2) DO IF #%Def_SET%==#%%w (
ECHO You selected: %Def_SET%
Timeout /T 5
Exit /b
)
REM USER ERROR
REM If the user types something other than 1 or 2, the FOR/DO/IF statement won't run indicating user error.
REM The routine will continue here, letting the user know there is an error.
ECHO Error
ECHO Select 1 or 2 & GOTO :def_input
After this, TEST1 in Main_control batch returns this error "'1' is not recognized as an internal or external command, program or batch".
USEROPTIONS batch
#echo off
REM Name: USEROPTIONS
ECHO %~no & %def_set% & pause
IF %def_set%==1 GOTO :defopts
IF %def_set%==2 GOTO :askopts
:defopts
:: Overwrite previous backup
SET OW_BU=1
:: Generate a log
SET ONLog=1
:: Overwrite the log
SET OWLog=1
:: Prompt to delete files
SET Del_P=1
GOTO :EOF
:askopts
:: Questions to Users
:: Overwrite Backup Question
:: Rename the previous backup if user does not want it overwritten.
:OW_Backup
IF NOT EXIST %BACKUP_FOLDER%\ GOTO :skipOWQ
REM If previous backup does not exist, skip question to overwrite it.
ECHO.
ECHO Do you want to OVERWRITE the previous backup. Choose:
ECHO 1. Do not overwrite old backup,then rename it with its "created" date.
ECHO 2. Overwrite old backup.
ECHO.
SET /P OW_BU= Make a selection...
FOR %%u in (1 2) DO IF #%OW_BU%==#%%u (
ECHO You selected: %OW_BU%
GOTO SkipError1
)
REM Error
ECHO.
ECHO.
ECHO *********Error*************
ECHO Select 1 or 2 & GOTO :OW_Backup
:SkipError1
:skipOWQ
:: Generate Robocopy Log
:GenLog
ECHO.
ECHO Do you want to GENERATE a Robocopy log?
ECHO It will be located in %Backup_Folder%.
ECHO 1. Yes
ECHO 2. No
ECHO.
SET /P ONLog= Make a selection...
FOR %%v in (1 2) DO IF #%ONLog%==#%%v (
ECHO You selected: %ONLog%
GOTO SkipError2
)
REM Error
ECHO.
ECHO.
ECHO *********Error*************
ECHO Select 1 or 2 & GOTO :GenLog
:SkipError2
:: Overwrite or Append Robocopy Log
IF %ONLog%==2 GOTO :Del_Prompt
:OW_RLog
ECHO.
ECHO Do you want to OVERWRITE the Robocopy log?
ECHO.
ECHO 1. Yes - Overwrite
ECHO 2. No - Append to Log
ECHO.
SET /P ONLog= Make a selection...
FOR %%v in (1 2) DO IF #%ONLog%==#%%v (
ECHO You selected: %ONLog%
GOTO SkipError3
)
REM Error
ECHO.
ECHO.
ECHO *********Error*************
ECHO Select 1 or 2 & GOTO :OW_Rlog
:SkipError3
:: Prompt to Delete Decision
:Del_Prompt
ECHO.
ECHO.
ECHO The restoration will delete default files that are not in your backup in order to restore your VDI to the way you had it.
ECHO Do you want confirmations to delete files?
ECHO 1. No confirmations to delete files.
ECHO 2. Review and confirm to delete files.
ECHO.
SET /P Del_P= Make a selection...
ECHO.
FOR %%x in (1 2) DO IF #%Del_P%==#%%x (
ECHO You selected: %Del_P%
GOTO SkipError4
)
REM Error
ECHO.
ECHO.
ECHO *********Error*************
ECHO Select 1 or 2 & GOTO :Del_Prompt
:SkipError4
Exit /b
Back to Main_Control batch, then call SETOPTIONS
SETOPTIONS batch
#echo off
REM Name: SETOPTIONS
ECHO Currenlty running: %~nx0
pause
:: Set Options
ECHO Backup location is: %Backup_Folder%
REM Test
echo OnLog %Onlog%
echo Overwrite %Overwrite%
echo Del_P %del_P%
pause
::///////////////////////// Robocopy options /////////////////////////////
IF %ONLog%==1 (
IF %OWLog%==1 SET LOG=/LOG:%BACKUP_FOLDER%\Robocopy_Log.txt
IF %OWLog%==2 SET LOG=/LOG+:%BACKUP_FOLDER%\Robocopy_Log.txt
) else (
SET LOG=
)
SET R_OPT=/Z /MIR %LOG%
REM Removed /copyall
::///////////////////// Overwrite Backup Code ////////////////////////////////
IF NOT EXIST %BACKUP_FOLDER%\ GOTO :skipOWC
IF %Overwrite%== 1 (
ECHO.
ECHO %BACKUP_FOLDER% will be RENAMED.
TIMEOUT /T 2
Call %RENAME%
)
IF %Overwrite%== 2 (
Echo %BACKUP_FOLDER% will be OVERWRITTEN.
ECHO Close this window if you've changed your mind.
TIMEOUT /T 10
)
REM Nothing to do to overwrite old backup. It will do it on its own with /MIR option.
:skipOWC
::///////////////////// Prompt to Delete Code ////////////////////////////////
IF %Del_P%== 1 SET Del_Opt_Prompt=/Q
IF %Del_P%== 2 SET DEL_Opt_Prompt=/P
DEL_OPT=/F /S %Del_Opt_Prompt%
REM Test
echo OnLog %Onlog%
echo Overwrite %Overwrite%
echo Del_P %del_P%
pause
EXIT /b
And back to Main_Control batch.
Escape the & ampersand character (this one to be displayed) using a ^ caret as follows:
echo before ^& %def_set% & pause
rem END TEST1
CALL %USEROPTIONS%
REM TEST2
echo after ^& %def_set% & pause
The & ampersand is one of a few with special meaning in a Window CLI and/or batch.
Read also Syntax : Escape Characters, Delimiters and Quotes.
So many errors and typos.
In trying to figure out why the called batches weren't working, I added more errors in my attempts to test it.
The tests in Main_Control contained ampersands.
echo before & %def_set% & pause
USEROPTIONS crashed because of a typo - letter "o" instead of zero "0" in:
ECHO %~no & %def_set% & pause
SETOPTIONS crashed because I forgot what variables I was using. %overwrite% doesn't describe anything and doesn't exist in the line:
echo Overwrite %Overwrite%
It should have been :
echo Overwrite backup OW_BU = %OW_BU%
echo Overwrite log OWlog = %OWlog%
SET was ommitted from the beginning of the line:
DEL_OPT=/F /S %Del_Opt_Prompt%
And i think that solved all my problems. What a waste of yesterday staring at this all day. In short, the calls worked & the variables were being passed from subroutine to subroutine. The errors were all typos initially and made even worse when I introduced more typos into my attempts to tests and trap errors. I'm tired of this project.
I am trying to create a bat file that is in essence a database. I want to be able to enter information that is tied to a single record. When I enter a record, I want to be able to look up the record by the card number assigned to it. The code I have now doesn't really work due to the fact that the variables are not being stored properly.
This is my code:
Color 5F
#echo off
:start
cls
echo ==========================================
echo Gift Card
echo ==========================================
echo.
echo What would you like to do?
echo.
echo 1 Add Card
echo 2 Check Information
echo 3 Edit Card Balance
echo 4 Delete Card
echo.
set /p choice=Please enter choice:
if /I %choice%==1 goto 1
if /I %choice%==2 goto 2
:1
echo.
set /p var=Enter Card Number:
set /p val=Enter Amount:
set /p fname=Enter First Name:
set /p lname=Enter Last Name:
set /p cbal=Enter Current Balance:
set /p diss=Enter Date issued:
#echo set %var%=%val%=%fname%=%lname%=%cbal%=%diss% > %var%.bat
echo.
echo The data has been stored!
pause
goto start
:2
echo.
set /p var=Please enter card number:
setlocal enabledelayedexpansion
call %var%.bat
echo !%fname%! !%lname%!'s !%var%! card has $!%cbal%! on it as of !%diss%!!
pause > nul
goto start
I have tried to send the variables separately and altogether and none have worked. I am thinking it is because I do not have the delayed expansion sytax correct.
Any help is very appreciated!
Your problem seems to lie where you generate the batch file to fill the variables... You generate only one line, which does not actually assign anything to any of the variables you need.
Try changing
#echo set %var%=%val%=%fname%=%lname%=%cbal%=%diss% > %var%.bat
to be
#echo set var=%var% > %var%.bat
#echo set val=%val% >> %var%.bat
#echo set fname=%fname% >> %var%.bat
#echo set lname=%lname% >> %var%.bat
#echo set cbal=%cbal% >> %var%.bat
#echo set diss=%diss% >> %var%.bat
This should allow your variables to be loaded back properly.
Also, change
echo !%fname%! !%lname%!'s !%var%! card has $!%cbal%! on it as of !%diss%!!
to read
echo %fname% %lname%'s %var% card has $%cbal% on it as of %diss%!
You should never use both ! and % to surround variables in a batch file, only one or the other. % should be used in most cases; ! should be used when you need to read a variable inside a multi-line "code block" (for example, the result of an if statement or the body of a for loop) which is surrounded by parentheses.
Some more advice:
You can put setlocal delayedexpansion just once in the beginning of the file, right after #echo off. However, you are not doing anything in this program (yet, at least) to need delayed expansion. Delayed expansion is used to enable accessing variables with the ! symbol and is only useful inside multi-line statement bodies surrounded by parentheses. Because of that, you should get rid of it completely unless/until you actually need it, as it can cause other problems.
There is no need for the "#" symbol in any command after you call #echo off (but it won't break anything). The # symbol simply suppresses echoing of the command which it precedes, but it is redundant because all commands are silenced by default after you call echo off. For this reason, it is important to only use it on the first line when calling #echo off, so that the user does not see that command echoed.
These are not working for me.
Any help to definitelly corret the four examples below ?
The EXAMPLE01 just echoes "continue", even if I have three CMD.exe opened.
---------- EXAMPLE 01 ------------
#echo off
wmic process where name="cmd.exe" | find "cmd.exe" /c
SET ERRORLEVEL=value if "%value%" GTR 1 (
ECHO This batch is not first
ECHO quitting ...
)
if "%value%" LSS 2 ECHO continue
I am getting the "unexpected i error" message in the EXAMPLE 02!
----------- EXAMPLE 02 -------
#echo off
FOR /F "usebackq tokens=2" %i IN (`tasklist ^| findstr /r /b "cmd.exe"`)
DO taskkill /pid %%i
I am getting the "is first" message in the EXAMPLE 03, even with three CMD.exe opened!
----------- EXAMPLE 03 -------
#echo off
wmic process where name="cmd.exe" | find "cmd.exe" /c
if "%errorlevel%" LEQ 1 echo CMD is first
if "%errorlevel%" GTR 1 echo CMD is already running
It is also possible that I will not have access to the Wmic command at work, so, another possibility is found in the EXAMPLE 04 ... but to no avail.
----------- EXAMPLE 04 -------
#echo off
Tasklist /FI "IMAGENAME eq cmd.exe" 2>NUL | find /I /N "cmd.exe">NUL
if "%ERRORLEVEL%"==0 do (goto Use) else (goto Cont)
:Cont
ECHO Only one instance running
pause
:Use
echo Application running already. Close this window
Kind regards,
Maleck
wmz identified a number of errors in the OP's code, and also has an excellent suggestion to use a lock file for concurrency control.
Below is a robust batch solution that uses a lock file to prevent multiple instances of the batch file from running at the same time. It uses a temporary lock file for the concurrency control. The mere presence of the lock file does NOT stop the script from running. The script will only fail if another process has an exclusive lock on the lock file. This is important in case the script should crash or be killed without deleting the lock file. The next process to run the script will still succeed because the file is no longer locked.
This script assumes the script is installed on a local drive. It allows only one instance for the entire machine. There are lots of variations to control the amount of concurrency allowed. For example, incorporating the %USERNAME% into the lock file name would allow one instance per user in a network environment. Incorporating %COMPUTERNAME% in the name would allow one instance per machine in a network environment.
#echo off
setlocal
:: save parameters to variables here as needed
set "param1=%~1"
:: etc.
:: Redirect an unused file handle for an entire code block to a lock file.
:: The batch file will maintain a lock on the file until the code block
:: ends or until the process is killed. The main code is called from
:: within the block. The first process to run this script will succeed.
:: Subsequent attempts will fail as long as the original is still running.
set "started="
9>"%~f0.lock" (
set "started=1"
call :start
)
if defined started (
del "%~f0.lock" >nul 2>nul
) else (
echo Process aborted: "%~f0" is already running
)
exit /b
:start
:: The main program appears below
:: All this script does is PAUSE so we can see what happens if
:: the script is run multiple times simultaneously.
pause
exit /b
EDIT
The error message "The process cannot access the file because it is being used by another process." can be suppressed by redirecting stderr output to nul in an outer block.
2>nul (
9>"%~f0.lock" (
set "started=1"
call :start
)
)
The problem with the above is that all error messages for the main program will also be suppressed. That can be fixed by 1st saving the current definition of stderr to another unused file handle, and then adding yet another inner block that redirects stderr back to the saved file handle.
8>&2 2>nul (
9>"%~f0.lock" (
2>&8 (
set "started=1"
call :start
)
)
)
You do not set value anywhere. Even if you did it would not work as variables are expanded on parse. You would need to use delayed expansion if you need to both set and test variable against what was set. Quotes on comparison are not required (see 3). help set will show you info delayed expansion.
%i should be %%i. DO taskkill /pid %%i must be on same line as rest of for command. You also use findstr in regex mode, which means it will search for cmd[any character]exe
You use string (lexical) comparison: "1" leq 1 is true (as "4" leq 1 also is...). Use errorlevel 1 which is equivalent to errorlevel equal or greater than 1. help if shows syntax
Same as 3 plus do in do (goto Use) should be removed. %errorlevel%==0 would work, but it's normally advised to use errorlevel number.
How to check if there is only 1 cmd running:
#echo off
for /f %%i in ('Tasklist /FI "IMAGENAME eq cmd.exe" 2^>NUL' ^| find /I /c "cmd.exe"') do (
if %%i leq 2 (echo only one instance) else (echo More than one instance)
)
Note: it's just an example. I do not recommend this as a real method of concurrency control. I would rather use lock file for that.
How can I count how many calls of a cmd file?
I'm struggling with something like this but it didn't work:
#IF NOT EXIST Calls.log echo. > Calls.log
#for %%i in (Calls.log) do set size=%%~zi
#IF %size% EQU 0 (
#ECHO 1 > Calls.log
) ELSE (
#set /p v=<Calls.log
#set /A v+=1
#echo %v% > Calls.log
)
If all you're trying to do is count how many times a cmd script is called, you can just append one character to a file every time it runs rather than fiddling around with expression evaluation every time the script is run. This also has the advantage of making the script quicker since the analysis of the count is moved elsewhere.
The counter file expands by one byte every time, so watch out if you're calling it a truly large number of times since the file will expand. But even, calling it once per second, a 1G file will accrue only after 30 years.
At the top of your script, just put:
set >>countfile.txt <nul: /p x=X
This simply adds the character X to the end of the countfile.txt file every time the script is called. It uses the "set/p" command with input/output redirection, which is the only way I'm aware of to get a character out without a CR/LF following it (like the UNIX "echo -n").
To get a count of the number of calls to date, you can use the file size environment variable modifier, as in the following script (I expect this will be done less often than running the script so it's better to put the grunt work here [in fact, it's not a lot of grunt work since it's not counting the characters, rather it gets the information directly from the directory entry]):
#echo off
goto :main
:getsize
echo %~z1
goto :eof
:main
setlocal enableextensions enabledelayedexpansion
call :getsize countfile.txt
endlocal
To reset the count, use the following extremely complicated command (I'm thinking of patenting this):
del countfile.txt
One other thing I'd suggest - you don't need to prefix every command with "#" to prevent echo, you can simply put "#echo off" at the top of your script for global no-echo. If you want to selectively echo some commands, just ignore this paragraph.
The complete if block is parsed at once and thus all environment variables in it are getting replaced by their values at the time before the if block gets executes. You need to enable delayed variable expansion and use !v!:
#setlocal enabledelayedexpansion
#IF NOT EXIST Calls.log echo. > Calls.log
#for %%i in (Calls.log) do set size=%%~zi
#IF %size% EQU 0 (
#ECHO 1 > Calls.log
) ELSE (
#set /p v=<Calls.log
#set /A v+=1
#echo !v! > Calls.log
)
And you can simplify the code as follows:
#echo off
setlocal enabledelayedexpansion
IF NOT EXIST Calls.log (
ECHO 1 > Calls.log
) ELSE (
set /p v=<Calls.log
set /A v+=1
echo !v! > Calls.log
)
There is no need to create the file beforehand (and even that I'd solve with copy nul Calls.log, since that ensures a file size of 0).
The following code works on my computer:
#if not exist Calls.log (
echo 0 > Calls.log
)
#set /p v=< Calls.log
#set /a v=v+1
echo %v% > Calls.log