My batch variable saver keeps crashing - batch-file

This is what I have right now:
:saveDetector
if exist %USERPROFILE%/Desktop/savefile.txt
goto :saveDetectorName
else goto :name
:saveDetectorName
if exist %USERPROFILE%/Desktop/savename.txt
set /P c=Save detected. Would you like to load it?[Y/N]
if /I "%c%" EQU "Y" goto :saveloader
if /T "%c%" EQU "N" goto :choice
goto :saveDetectorName
:saveloader
set /p save=<savefile.txt
set /p name=<savename.txt
goto :%save%
I've attempted to go and fix it by doing things like fix the variables in the text files, use several branches, and stuff like that. However, it keeps crashing. Anyone know why? (And yes, the text files only contain 1 line)

if exist %USERPROFILE%/Desktop/savefile.txt
goto :saveDetectorName
else goto :name
The syntax of an if statement is specific.
the command to be executed must be on the same line as the if or at least start on the same line if that command is a code block (parenthesised sequence of lines)
If an else clause is used then the if-true command must be parenthesised and the closing parenthesis, the else keyword and the opening parenthesis of the if-false command must all be on the same line and separated by spaces.
if exist %USERPROFILE%/Desktop/savefile.txt (
goto saveDetectorName
) else (
goto name
)
The colons are not required in a goto.
If you run thecode directly from the prompt, you will get a syntax-error report shown on-screen.
/ is used to indicate a switch in windows. \ is used to separate directories. Sometimes, but not always / will be translated.

Related

My batch window closes when running if exist

I have a little program and when i run "if exist" it closes, can somebody tell me why? I have checked for syntax errors and none (i think), i will paste my code here and see what you can do.
echo What drive would you like to launch MCEdit on?
set /p DRIVE="Drive: "
if exist "%DRIVE%:\MC\DATA\mcedit\mcedit.exe" (
set APPDATA=%DRIVE%:\MC\DATA
start %DRIVE%:\MC\DATA\mcedit\mcedit.exe
) else (
echo You do not have MCEdit installed on this drive, would you like to install it?
set /p YesNoIn="[y/n]: "
if "%YesNoIn%"=="y" goto:yes
goto:menu
:yes
echo Choose the "mcedit.exe" file from a current installation on your pc.
pause
call "%DRIVE%\MC\DATA\FileViewer.bat"
xcopy "%OutCD%" "%DRIVE%:\MC\DATA\mcedit" /E
echo Done!
set /p yesnol="Would you like to launch MCEdit now? [y/n]: "
if "%yesnol%"="y" (
set APPDATA=%DRIVE%:\MC\DATA
start %DRIVE%:\MC\DATA\mcedit\mcedit.exe
)
)
Right after you type the drive it closes, any help?
The issue that causes the script to exit early is a syntax error in the last if-statement of your else branch. If you run the script from the command line (not by double clicking on it) you'll get the error message:
="y" was unexpected at this time.
Indeed your last if-statement is:
if "%yesnol%"="y" (
but the cmd parser expects a double "=" sign (==) for comparisons, so you should have:
if "%yesnol%"=="y" (
The reason why it will see it even if it won't take the else branch is because an if-block (actually, all block of codes delimited by ( ... )) is parsed as if it was one single command written on one line (with each "subcommand" in your block separated with &&). As the parser will process that whole "line" in one go, it will detect whatever syntax error present inside the whole block.
Besides that syntax error, there are some little mistakes you've made in your script. The first one is that it is actually not good to declare labels inside a block of code. You've declared the :yes label inside the if-block in the else branch. A goto inside an if-block will "destroy" the if-context. You maybe won't notice in your code, but consider this example:
#echo off
set var=Let's brake the if statement
IF DEFINED var (
echo The variable var exists
IF "%var%"=="Let's brake the if statement" goto :expected
echo The variable var is not what I would expect though.
echo You have to change it to something else...
goto :outside
:expected
echo I'm happy with its value. It is at least something I would expect
echo This is the end of the if-branch
) ELSE (
echo Strange ... the variable var is not defined.
echo Indeed: var="%var%"
)
:outside
rem just to get outside
You would expect an output like
The variable var exists
I'm happy with its value. It is at least something I would expect
This is the end of the if-branch
but the output will be
The variable var exists
I'm happy with its value. It is at least something I would expect
This is the end of the if-branch
Strange ... the variable var is not defined.
Indeed: var="Let's brake the if statement"
The goto destroyed the if-context. As said earlier, the cmd parser will parse the whole if-block as one command. See it this way: you're asking the parser to abandon the command it was processing (the whole if block with the condition it just checked) and jump to somewhere else. That somewhere else is after the if condition, so it won't even evaluate that one again. So once you're inside a block (espacially if-blocks and for-loops) don't use goto to ignore some piece of code inside that block, use if-statements and put the code to ignore inside the if-block. A goto to jump outside a block of code is not a problem but from the moment the label is inside an if block for example, it can result in unexpected results.
The second mistake is about the variables YesNoIn and yesnol. As I said the whole if block is parsed in one go as one single command. It is not possible to give a variable a new value, and read that new value in the same command with the simple variable expansion %YesNoIn% or %yesnol%. A solution to this problem is delayed expansion (the link also has an example). As I was writing this answer, I saw #Josefz already posted an answer with delayed expansion so I won't repeat it here. But what I would recommend is that you take a look at the choice command. Using the choice command, you won't need delayed expansion. It just sets the errorlevel and there exists a way to check the errorlevel without worrying about delayed expansion: IF ERRORLEVEL n will check if the errorlevel is greater or equal to n. On top of that, choice automatically verifies if the user entered a correct value!
Your script with choice instead of set /p will look like this:
echo What drive would you like to launch MCEdit on?
set /p DRIVE="Drive: "
if exist "%DRIVE%:\MC\DATA\mcedit\mcedit.exe" (
set APPDATA=%DRIVE%:\MC\DATA
start %DRIVE%:\MC\DATA\mcedit\mcedit.exe
) else (
echo You do not have MCEdit installed on this drive, would you like to install it?
choice /c YN
REM Y ==> errorlevel = 1 ; N ==> errorlevel = 2
if ERRORLEVEL 2 goto:menu
echo Choose the "mcedit.exe" file from a current installation on your pc.
pause
call "%DRIVE%\MC\DATA\FileViewer.bat"
xcopy "%OutCD%" "%DRIVE%:\MC\DATA\mcedit" /E
echo Done!
choice /c YN /m "Would you like to launch MCEdit now? "
if NOT ERRORLEVEL 2 (
set APPDATA=%DRIVE%:\MC\DATA
start %DRIVE%:\MC\DATA\mcedit\mcedit.exe
)
)
PS: No Enter key needs to be pressed when choice is used.
EDIT: A third mistake is the choice of the variable APPDATA, it is already used by your windows OS in a user's context as you can see here.
You have simple typo in comparison operator in the latter if. Correct to if "%yesnol%"=="y"
I'm not sure whether changing APPDATA variable is a good idea.
Missing :menu label in your code.
Important: read and apply http://ss64.com/nt/delayedexpansion.html.
Never use :label nor :: label-like comment inside a command block enclosed in () parentheses
SETLOCAL EnableExtensions EnableDelayedExpansion
:::
:menu
:::
echo What drive would you like to launch MCEdit on?
set /p DRIVE="Drive: "
if exist "%DRIVE%:\MC\DATA\mcedit\mcedit.exe" (
set APPDATA=%DRIVE%:\MC\DATA
start %DRIVE%:\MC\DATA\mcedit\mcedit.exe
) else (
echo You do not have MCEdit installed on this drive, would you like to install it?
set /p YesNoIn="[y/n]: "
if /I "!YesNoIn!"=="y" (
echo Choose the "mcedit.exe" file from a current installation on your pc.
pause
call "%DRIVE%\MC\DATA\FileViewer.bat"
xcopy "%OutCD%" "%DRIVE%:\MC\DATA\mcedit" /E
echo Done^!
set /p yesnol="Would you like to launch MCEdit now? [y/n]: "
if /I "!yesnol!"="y" (
set APPDATA=%DRIVE%:\MC\DATA
start %DRIVE%:\MC\DATA\mcedit\mcedit.exe
)
) else goto:menu
)

Batch: The goto statement isn't working properly

I have three different labels, but it doesn't matter with one I choose: It will always got to the first label (= cmd is also opening if i am writing "google")! What's the error and why is it happening?
#echo off
:start
set /p input = What do you want to open?
if "%input%" == "cmd" goto cmd
if "%input%" == "notepad" goto notepad
if "%input%" == "google" goto google
:cmd
start
goto start
:notepad
start notepad
goto start
:google
start https://www.google.at/
goto start
Your first problem is in this line
set /p input = What do you want to open?
This line creates a variable called "input " with a space after it, and which is different than the "input" variable you're testing. Unlike every other programming language, you can't add spaces to beautify your code. Change the line to
set /p "input=What do you want to open? "
Your second problem is that after all three if statements fail (because of testing the wrong variable), CMD simply executes the next line it sees, which is the :cmd label. To avoid this, you need to add a default label after your three if statements, for when the variable doesn't match.
if "%input%" == "cmd" goto cmd
if "%input%" == "notepad" goto notepad
if "%input%" == "google" goto google
goto default
:cmd
start
goto start
If you just want the script to end there and don't want to create a :default block, you can change it to goto :eof, which is a special label for the end of the script. Or even better, use exit /B 1 which terminates the script immediately with an error code of 1 (which another script can check using %errorlevel%)
Your set /p isn't working as you think it should. input ends up empty and it falls through all of your if statements and right into the :cmd block.

Troubles with goto(cmd)

#echo off
:WriteAgain
set x=
set /p Variables=Write your expression
set /a x=%Variables%
if %errorlevel% neq 0 goto ErrorOccured
echo %x%
goto :eof
:ErrorOccured
echo.Your expression is not valid
goto WriteAgain
:eof
Greeting, It is supposed to be a simple calc, but for some reasons, when "if" works(for 1/0) it looks like "goto" doesnt(I may be mistaken here). Could you help me to solve this problem? Also I am thinking about typing error in any txt: should I use 2>txt_name.txt after neq 0 or what?
goto :eof is a built-in construction to return from a subroutine (call :subroutine). It exits current batch file when used not in a subroutine.
Rename the label to end, for example.
Or use exit instead of goto to the end of batch file.
For output redirection examples and syntax see http://ss64.com/nt/syntax-redirection.html so in your case echo prints to standard output thus > must be used:
echo Your expression is not valid >errlog.txt
Some utilities indeed print the errors to STDERR and the standard > won't catch the messages, so command 2>errlog.txt should be used.
You don't put
goto :eof
Try using
goto eof
And also I am not sure but maybe the name eof is no good (Is used by CMD itself) so keep things simple and use any other name like "exit, problem, fail, etc..."

ELSE was unexpected at this time batch file

This is the script:
#echo off
set BSL_Scripter_go="BSL_Scripter.exe"
set BSL_Script=WriteMain_s.txt
set yes=Y
set no=N
::%BSL_Scripter_go% %BSL_Script%
:LABEL1
set /p answer=Have you turned device off?[Y\N]
IF "%answer%"=="%yes%" (GOTO LABEL_DEVICE_RUN
) ELSE (IF "%answer%"=="%no%"(GOTO LABEL1) ELSE (GOTO LABEL_TYPO))
:LABEL_DEVICE_RUN
echo Device is runing..
GOTO END
:LABEL_TYPO
echo UNCORRECT ANSWER, PLEASE TYPE 'Y' or 'N'
GOTO LABEL1
:END
and I got the error:
ELSE was unexpected at this time
Little help?
There is a SPACE missing between "%no%" and (GOTO LABEL1):
IF "%answer%"=="%no%" (GOTO LABEL1)
Otherwise, "%no%"(GOTO is the string to compare and so there a closing ) too much. The command line interpreter tries to execute the following line (omitting values and commands for visualisation):
IF <value>==<value> (command) ELSE (IF value==value command) ELSE (command))
You will notice that it looks like there are two ELSE clauses for the first IF statement.
Side Note: I recommend using /IF /I "%answer%"==... so that Y/N and y/n are accepted.
This drove me nuts. In my case it was unescaped() in my comment lines prefixed with :: inside my IF statements.
Apparently one should be using REM inside IF statements instead of ::, but escaping all my ^(m ^) (even in non-comment lines, i.e. ECHO statements) worked for me.
See: https://stackoverflow.com/a/16839602/913223

Batch code: how to pass a parameter choosing from an input list?

I'm really new in this forum so I hope to respect all your rules, if not please forgive me!
I've just started studying something about batch files and I'm trying to execute a simple program from batch passing a parameter (the last aim is to submit a SAS program passing a date parameter).
Is it possible to activate a sort of list where I can choose some between pre-defined parameters?
--> This is the real aim of my work
I'm trying to "play" with this code:
#echo off
title Setting up execution period
echo Insert your date in the format GGMMMAAAA (es: '31DEC2003'D).
SET /p data_par=Insert the date to filter datas:
SET first_byte=%data_par:~1,1%
if "%first_byte%"=="" (
GOTO tag1
) else (
GOTO tag2
)
:tag1
msg * Missing value
:tag2
msg * Well done!
pause
I've tried in a lot of ways but it looks like the IF statement is not executed, I don't know where am I wrong.
Another question: why the prompt closes after i press "Enter" (afte the set/p command is executed)?
--> this has been resolved putting the "pause" command at the end of the script.
Thank you all for the attention,
Best regards!
Squotty
Put a pause at the end of your code to see the errormessages.
correct syntax for if when using else is:
if "a"=="b" (dosomething) else (dosemethingelse)
You can write it in several lines, but there are rules, where to set the paratheses:
if "a"=="b" (
echo this is code for something
rem more lines possible
) else (
echo this is code for something else
rem more lines possible
)
The first ( has to be on the same line than if.
) else ( have to be on one line.
If you press just enter with set /p, the variable remains unchanged (propably empty), so your code will go on with the code and hits the line else. Here it will tell you "else is not recognized as a command..."
at your tagx you should tell batch, where to stop execution. Use goto :eof to stop execution or goto somewhere to continue somewhere else. If you don't, it will just continue with the next lines.
Example:
:tag1
msg * Missing value
goto :eof
:tag2
msg * Well done!
goto :continue
pause
:continue
REM go on with the program...
(note: the pause will never be reached. I let it there to show you, how things work)
EDIT instead of just checking for some input you can check for the correct format:
echo %data_par%|findstr /r "[0-3][0-9][A-Z][A-Z][A-Z][1-2]0[0-9][0-9]">nul && (
echo correct format
goto continue
) || (
echo wrong format
goto startover
)
It's not bullet proof (eg. 38ABC2019 would be considered "correct"), but at least it checks for the correct format (e.g. 15.12.2019 or 12/15/2014 would be "not correct")
#ECHO OFF
SETLOCAL
SET "item1=date1"
SET "item2=date2"
SET "item3=date3"
SET "item4=date4"
FOR /l %%a IN (1,1,4) DO CALL ECHO(%%a. %%item%%a%%
ECHO(U. User-specified
choice /c 1234u
CALL SET selection=%%item%errorlevel%%%
IF NOT DEFINED selection (
SET /p selection="Your date-selection ? "
)
IF NOT DEFINED selection ECHO No selection made&GOTO :EOF
ECHO selection is %selection%
GOTO :EOF
This code may be of assistance.
It's normal to develop batch code using a batch window. Simply set up a shortcut to command prompt (Start>Programs>Accessories) which would allow you to run the script over and over and retain the results on-screen without using 'pause'. Editing can be accomplished by using notepad batchfilename.bat from the prompt (if you are using notepad for an editor - if using something better, then substitute that program's name). You can exit from the batch window by executing an exit command.
You can also get help on batch commands by using commandname /? - it's often cryptic and there are plenty of quirks. Extensive help available here on SO.

Resources