#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..."
Related
Supposing we have CMD batch script code like this:
CALL :SUB
REM DO SOMETHING WITH THE RESULT HERE (300)
EXIT
:SUB
EXIT /B 300
What variable or mechanism can be used to replace the REMarked like above to do one thing if the result of SUB was 300, and something else if not? I want to write in there something like this:
IF %RESULT% EQU 300 (
ECHO Hi
) ELSE (
ECHO Bye
)
Please correct me if I'm wrong but I think my mechanism (the conditional statement) here is fine, but what about the variable?
This isn't intuitive as it might be in other programming languages, but the variable you want is %ERRORLEVEL%--the same variable used to record the results of other commands you might invoke in the batch script. Per Microsoft, the syntax of the exit command is:
exit [/b] [<exitcode>]
where exitcode, "Specifies a numeric number. If /b is specified, the ERRORLEVEL environment variable is set to that number. If you are quitting the command interpreter, the process exit code is set to that number."
So ultimately, you want to write,
IF %ERRORLEVEL% EQU 300 (
ECHO Hi
) ELSE (
ECHO Bye
)
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.
I seem to have a problem with my "if" statements. Currently after the pause in "start" the file just closes and nothing else happens. Chances are, I have no idea what I'm doing.
#echo off
set startnum=0
goto start
:start
pause
set startnum="!startnum!"+1
if "%startnum%"="%0%" goto fail
if "%startnum%"="%1%" goto success
goto start
:success
cls
echo It worked!
echo "%startnum%"
pause.
exit
:fail
cls
echo Failure
pause.
exit
First problem:
set startnum="!startnum!"+1
Evidently, you wish to add 1 to startnum.
Your set command would set startnum to "!startnum!"+1. Literally. To perform arithmetic, you need set /a.
set /A startnum="!startnum!"+1
well, this won't work as "!startnum! isn't numeric. Had you invoked delayedexpansion beforehand, then the value of startnum would have been substituted for !startnum! yielding set /A startnum="0"+1 which makes more, but still not much sense.
set /A startnum=startnum+1
adds 1 to startnum - see set /? from the prompt for documentation.
set /A startnum+=1
would also add 1 to startnum.
Next problem.
if "%startnum%"="%0%" goto fail
Well, you appear to have found lss and all that gang. Problem is that the simple comparison operator is ==, not =.
if "%startnum%"=="%0%" goto fail
Now - what will that do? It will compare "thecontentsofstartnum" to "thecontentsof0". Since both of these arguments are quoted, batch will perform a string comparison. With a string comparison, 123 is less than 89 because 1 is less than 8.
But - you are attempting an equivalence comparison (equ as the operator may be used instead of ==) so the preceding point is simply AAMOI.
The difficulty is %0% which you may believe attempts to extract the value of the variable 0 but actually it replaces %0 with the value of the 0th parameter to the batchfile, which is the batchfile name itself, so you get "batchfilename%" - probably not what you actually wanted.
if "%startnum%"=="0" goto fail
is the way to implement that test.
The first IF statement is preprocessed by cmd.exe to
if ""!startnum!"+1"="test.bat" goto fail
which is a completely invalid IF condition.
cmd.exe outputs a syntax error message because of "="test.bat"" and exits batch file processing. This can be seen by debugging the batch file.
The solution is using right syntax for
assigning a value to an environment variable,
an arithmetic expression,
and last but not least the IF condition itself.
The batch file code fixed:
#echo off
set "startnum=0"
goto Begin
:Begin
set /A startnum+=1
if "%startnum%" == "0" goto Fail
if "%startnum%" == "1" goto Success
goto Begin
:Success
cls
echo It worked!
echo "%startnum%"
pause
exit /B
:Fail
cls
echo Failure
pause
exit /B
It would be safe here to remove all double quotes on both IF conditions.
One more hint: Don't use the name of a command like START as label. It works, but it should be avoided in case of ever adding to batch file the command START and searching for either command or label.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains how to reference batch file arguments.
cls /?
echo /?
exit /?
goto /?
if /?
pause /?
set /?
Further read the answers on following questions:
Batch file comparison of variable with constant fails
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
Where does GOTO :EOF return to?
Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files
Currently I am writing a batch file to accept a parameter and jump to a specific part of the file based on the parameter value. The code I have is as follows:
SET param = %~n1
IF "%param%"=="q" GOTO :QUICK
IF "%param%"=="t" GOTO :THOROUGH
IF "%param%"=="b" GOTO :BOOT
IF "%param%"=="f" GOTO :FIX
IF "%param%"=="" GOTO :INVALID
:QUICK
echo "Quick"
pause
exit
:INVALID
echo "Invalid"
pause
exit
:BOOT
echo "Boot"
pause
exit
:FIX
echo "Fix"
pause
exit
:THOROUGH
echo "Thorough"
pause
exit
The only parameters the script should accept are /q, /t, /b or /f (or without /), and when an incorrect parameter is supplied, it should jump to INVALID. When I run it with an invalid parameter, it works, however, if I supply a parameter that is correct (for example, file.bat t) the result fails to work as I hope, and ends up going directly to INVALID. I have tried using "%param%" EQ "q", and setting q to a variable before doing the comparison, but I have had no luck coming up with a result that works.
you don't have a parameter %param%. But you have a parameter named %param<space>% And it's value isn't q, but <space>q
Write your setcommand without spaces:
set param=%~n1
or even better (to avoid unintended spaces at the end):
set "param=%~n1"
Note: you can make your parameters case independent with if /i "%param%"=="q"
First time posting here. I'm writing a DOS batch script to automate the process of archiving Oracle tables. The archive tool requires a parameter file with tags that need to be populated at run-time. I am using the following logic, trying 2 different methods to catch the error, but when redirecting to a bad path, the error is not being caught. Is there an alternative to ECHO, or an alternate way to detect this write error? Or any other ideas? Thanks much!
-Greg
#ECHO OFF
FOR /F %%G IN (O:\tmp\infile.txt) DO (
...
...
ECHO TABLE=%%G > P:\BADPATH\MYOUTFILE1.TXT
IF %ERRORLEVEL% NEQ 0 GOTO END
...
ECHO TABLE=%%G > P:\BADPATH\MYOUTFILE2.TXT
IF ERRORLEVEL 1 GOTO END
...
...
)
:END
IF %ERRORLEVEL% NEQ 0 ECHO FAILED!
ECHO EXITING
Not sure if I understand correctly, but it sounds like you want to check if a file/folder exists first. If so:
IF EXIST P:\BADPATH\ goto notexist else goto exist
:notexist
echo Doesn't exist.
:exist
echo It exists!
Or use standard () tags to contain code within.