We check error using
if !ERRORLEVEL! NEQ 0 (do something)
but this is littered everywhere in the batch file.
1) Is the a way to encapsulate it to log and exit program upon error?
2) how do I log the bat file line number that is causing the error?
#ECHO OFF
SETLOCAL
ECHO y|FIND "y" >NUL
CALL aberr matching y and y
pause
ECHO x|FIND "y" >NUL
CALL aberr matching x and y
pause
ECHO y|FIND "z" >NUL
CALL aberr matching y and z
pause
GOTO :EOF
The above is a testing scenario, setting errorlevel to 0, 1, 1 in succession, then CALLing then batch aberr.bat to analyse the result.
Here's aberr.bat
#ECHO OFF
IF %ERRORLEVEL%==0 GOTO :EOF
ECHO %ERRORLEVEL% found %*
EXIT
Note here that there is no SETLOCAL (which would set ERRORLEVEL to zero) and that the routine EXITs.
Consequence is that if aberr.bat is on the PATH then the message produced would show the errorlevel found plus any text that was on the CALL aberr line after CALL aberr.
You could place a PAUSE after the ECHO %ERRORLEVEL% line to show the result, or log the result to a file by using
>>logfile.txt ECHO %ERRORLEVEL% found %*
at 1) How to log and exit the batch Exit from nested batch file
at 2) How to get the current line number?
Mix them and you get it.
setlocal EnableDelayedExpansion
cd. & REM *** Set the errorlevel to 0
if %errorlevel% NEQ 0 (
call :getLineNumber errLine uniqueID4711 -2
call :log ERROR: in line !errLine!
)
set /a n=0xGH 2> nul & REM Force the errorlevel to !=1
if %errorlevel% NEQ 0 (
call :getLineNumber errLine uniqueID4711 -2
call :log ERROR: in line !errLine!
)
echo all OK
exit /b
:log
>error.log echo %*
call :HALT
exit /b
:HALT
call :__halt 2> nul
exit /b
:__halt
()
:::::::::::::::::::::::::::::::::::::::::::::
:GetLineNumber <resultVar> <uniqueID> [LineOffset]
:: Detects the line number of the caller, the uniqueID have to be unique in the batch file
:: The lineno is return in the variable <resultVar> add with the [LineOffset]
SETLOCAL
for /F " usebackq tokens=1 delims=:" %%L IN (`findstr /N "%~2" "%~f0"`) DO set /a lineNr=%~3 + %%L
(
ENDLOCAL
set "%~1=%LineNr%"
goto :eof
)
Use something like:
...
if !ERRORLEVEL! NEQ 0 Call :LogAndExit "some explanation"
...
GoTo :EOF
:LogAndExit
Echo %Date% %Time% - %~1>>Log.txt
Exit /B
Related
scripters,
at first sorry for the last post.
Realy, i hope i could make you understand my problem now.
I realized that the problem is inside my script only.
If i run the line in the command prompt it works fine.
That means the Errorlevel is stored to exitCode.txt correctly.
START /B "some title" cmd /C "subroutine.bat& >"exitCode.txt" (call echo %^errorlevel%)& >"status.txt" (echo done) & (if defined return call echo %^return%) >"return.txt"" 1>"stdOut.txt" 2>"stdErr.txt"
This is a part of my script
So in :treath.run is the START /B .... line
Inside the script the errorlevel is not stored correctly inside the definded file.
Why?
The return Value is stored perfect.
I dont't get it.
Save this as main.bat
#echo off
setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
pushd "%~dp0"
:main
call :treath.import
call :treath.init "t1" "call subroutine.bat"
call :treath.run "t1"
:loop
call :treath.status "t1"
if "!return!"=="done" goto done
echo !return!
goto loop
:done
echo Now goto "!%t1%.stdExitCode!"
echo.
echo The Subroutine should return Errorlevel 1
echo But it is 0, in the file.
pause
EXIT
goto main
:::::::::::::::::::::::::::::::::::::::
:treath.import ()
set "return="
setlocal
set "objID=__OBJ_TREATH"
set "workdir=%tmp%\BATCH_OBJ\%objID%"
if not exist "%workdir%" (mkdir "%workdir%" || exit /b 1)
set /a "timeout=999"
:newSessionID
set /a "sessionID=%random%"
set "sessionDir=%workdir%\%sessionID%"
if exist "%sessionDir%" (
set /a "timeout-=1"
if !timeout! LSS 0 exit /b 1
goto newSessionID
)
mkdir "%sessionDir%" || exit /b 1
endlocal & (
rem set "%objID%_identifier=%objID%"
set "%objID%_version=0"
set "%objID%_sessionID=%sessionID%"
set "%objID%_sessionDir=%sessionDir%"
)
exit /b 0
:treath.init (treathName,"command")
set "return="
setlocal
rem hardcoded obj ident
set "objID=__OBJ_TREATH"
rem objID
set "prefix=%objID%_%~1"
rem treath folder
set "workdir=!%objID%_sessionDir!\%~1"
mkdir "%workdir%" || exit /b 1
set "command=%~2"
endlocal & (
set "%prefix%.command=%command%"
set "%prefix%.workdir=%workdir%"
set "%prefix%.stdOut=%workdir%\stdOut.txt"
set "%prefix%.stdErr=%workdir%\stdErr.txt"
set "%prefix%.stdDump=%workdir%\stdDump.txt"
set "%prefix%.stdExitCode=%workdir%\stdExitCode.txt"
set "%prefix%.return=%workdir%\return.txt"
set "%prefix%.stdStatus=%workdir%\stdStatus.txt"
set "%~1=%prefix%"
)
exit /b 0
:treath.run (treathName)
set "return="
setlocal
set "title=%~1"
set "prefix=!%~1!"
type NUL >"!%prefix%.stdExitCode!"
type NUL >"!%prefix%.stdOut!"
type NUL >"!%prefix%.stdErr!"
type NUL >"!%prefix%.stdStatus!"
rem errorlevel ist immer 0 ???
START /B "%title%" cmd /C "!%prefix%.command!& >"!%prefix%.stdExitCode!" (call echo %%^errorlevel%%)& >"!%prefix%.stdStatus!" (echo done) & (if defined return call echo %%^return%%) >"!%prefix%.return!"" 1>"!%prefix%.stdOut!" 2>"!%prefix%.stdErr!"
>"!%prefix%.stdStatus!" (echo running)
exit /b 0
:treath.status (treathName)
set "return="
setlocal
set "prefix=!%~1!"
endlocal & (
set /p "return="<"!%prefix%.stdStatus!" || exit /b 1
)
exit /b 0
Save this as "subroutine.bat"
set "return=This is a Test"
exit /b 1
I've been echoing my way through the following code but I cannot determine why %ERRORLEVEL% is always zero.
#echo off
set activePerl_SiteBinPath=D:\ProgramFiles\ActivePerl\site\bin
call :isInPath %activePerl_SiteBinPath% & set foundActivePerl_SiteBinPath=%ERRORLEVEL%
echo %foundActivePerl_SiteBinPath%
set blub=d:\blub
call :isInPath %blub% & set foundBlub=%ERRORLEVEL%
echo %foundBlub%
exit /b
:isInPath
:: Tests if the path stored within variable pathVar exists within %PATH%.
::
:: The result is returned as the ERRORLEVEL:
:: 0 if pathVar is found in %PATH%.
:: 1 if pathVar path is not found in %PATH%.
:: 2 if parhVar path is missing/undefined.
:: Error checking
if "%~1"=="" exit /b 2
set pathVar=%~1
for /f %%i in ('echo ";%%PATH%%;" ^| find /c /i ";%pathVar%;"') do (
set /a foundPathVar=%%i
)
if /i %foundPathVar% equ 0 (
exit /b 1
)
set foundPathVar=0
exit /b 0
I get the following output
0
0
but I would expect
0
1
and according to the echoing I did inside :isInPath for case one exit /b 0 and for case two exit /b 1 is called. But why is %ERRORLEVEL% zero in both cases? I totally don't get it. Please help!
In cmd the whole line will be parsed for variable substitution at once. Hence at the time the following line is executed errorlevel is 0
call :isInPath %blub% & set foundBlub=%ERRORLEVEL%
You need to use delayed expansion
SETLOCAL EnableDelayedExpansion
call :isInPath %blub% & set foundBlub=!ERRORLEVEL!
I am making a game from a batch file and one of the inputs can accept any character (~!##$%^&*()`) and any other. Is there any way to look for any character other than numbers and use the GOTO command? This is my script so far:
set /p guess=
echo "%guess%"|findstr /L "[a-z][A-Z]~`!##$%^&*()-_=+\^|^^;:"',<.>/?*"
if %errorlevel% == 0 goto Invalid_Number
if %guess% == %number% goto Correct
... everything else here ...
:Invalid_Number
echo Invalid Number. Input must be a number
pause
Is there any way to make this work, all it says is Access Denied, I am testing this on a school computer though, it might not work.
Put this at the bottom of your script:
:isInt <str>
for /f "delims=0123456789" %%a in ("%1") do exit /b 1
exit /b 0
Then to invoke it, do
call :isInt %guess% && success || fail
Here's a more complete example:
#echo off
setlocal
set /a rand = %RANDOM% %% 10 + 1
:begin
set /P "guess=Guess a number between 1 and 10: "
call :isInt %guess% || goto invalid
if %guess% gtr 0 if %guess% lss 11 (
if %guess% equ %rand% (
echo Lucky guess!
exit /b
) else (
echo Oooh, so close. Try again.
goto begin
)
)
:invalid
echo Please enter a valid integer between 1 and 10.
goto begin
:isInt <str>
for /f "delims=0123456789" %%a in ("%1") do exit /b 1
exit /b 0
This is the same basic idea as MC ND's solution, but instead of using the for statement to unset %guess%, it sets %errorlevel% and stops looping at the first non-numeric character. This makes it infinitesimally more efficient. :)
And with either success or fail, I like to use conditional execution (the && and || stuff).
:ask
set /p "guess=?" || goto :ask
setlocal enabledelayedexpansion
for /f "delims=0123456789" %%a in ("!guess!") do set "guess="
endlocal & set "guess=%guess%"
if not defined guess (
echo invalid input
goto ask
)
echo valid input
The basic idea behind the test is to use the numbers as delimiters in a for /f command, so they are removed from the input. If anything remains it is not a number and the code in the do clause is executed.
The delayedexpansion is enabled/disabled to handle problematic characters (specially double quotes) that could be typed in the input field.
May I suggest you a different, better approach? Instead of read any line and then check if it contains a number, your program may directly read a number, so the checking is not necessary. The way to do that is emulating SET /P command via a subroutine. This way, you may add additional constraints to the input, like read a maximum number of digits, for example.
#echo off
rem Read a number emulating SET /P command
rem Antonio Perez Ayala
setlocal
rem Define the following variable before call InputNumber subroutine
set "thisFile=%~F0"
call :InputNumber number="Enter a number of up to 5 digits: " 5
echo Number read: %number%
goto :EOF
:InputNumber var="prompt" [digits]
setlocal EnableDelayedExpansion
rem Initialize variables
if "%~3" equ "" (set numDigits=9) else set "numDigits=%3"
set "digits=0123456789"
for /F %%a in ('copy /Z "%thisFile%" NUL') do set "CR=%%a"
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
rem Show the prompt and start reading
set /P "=%~2" < NUL
set "input="
set i=0
:nextKey
set "key="
for /F "delims=" %%a in ('xcopy /W "%thisFile%" "%thisFile%" 2^>NUL') do if not defined key set "key=%%a"
rem If key is CR: terminate input
if "!key:~-1!" equ "!CR!" goto endRead
rem If key is BS: delete last char, if any
set "key=!key:~-1!"
if "!key!" equ "!BS!" (
if %i% gtr 0 (
set /P "=!BS! !BS!" < NUL
set "input=%input:~0,-1%"
set /A i-=1
)
goto nextKey
)
rem If key is not a digit: ignore it
if "!digits:%key%=!" equ "%digits%" goto nextKey
rem If can not accept more digits: ignore it
if %i% equ %numDigits% goto nextKey
rem Else: show and accept the digit
set /P "=%key%" < NUL
set "input=%input%%key%"
set /A i+=1
goto nextKey
:endRead
echo/
endlocal & set "%~1=%input%"
exit /B
You may also add any other processing to the input line, like show asterisks instead of digits, etc. For a large example on this topic, see this post
I have this script within a .bat file and would like to only keep the most recent 100 lines, at the end of the file.
How do I delete all other rows?
#echo off
REM *DATETIME2.BAT
REM *Copy date and time to create a new log file
echo.|date>>"\\spserver\myfolder\dt.tmp"
echo.|time>>"\\spserver\myfolder\dt.tmp"
IF NOT EXIST "\\spserver\myfolder\dt.tmp" GOTO Error1
type "\\spserver\myfolder\dt.tmp"|FIND "current">"\\spserver\myfolder\dt.log"
rem del "\\spserver\myfolder\dt.tmp"
GOTO End
:Error1
Echo.
Echo There was an error processing the command.
Echo Unable to find temporary sort file DATETIME2.TMP.
Echo.
GOTO End
:End
Here you go:
Call :Tail 100 dt.log tempout.txt
move /y tempout.txt dt.log>nul
exit /b
:Tail <numlines> <FileIn> <FileOut>
FOR /F "usebackq tokens=3,3 delims= " %%l IN (
`find /c /v "" %2`) DO (call SET find_lc=%%l)
SET /a linenumber=%find_lc%-%1
IF %linenumber% LSS 1 (
more +0 %2 >> %3
) ELSE (
more +%linenumber% %2 >> %3
)
In Linux (Bash) there is a very useful functionality for dumping literal text out to another file like this:
cat > see.txt << EOF
contents going into
my file
EOF
What I need is the equivalent for a Windows batch script. I haven't found this kind of functionality built-in, but I was thinking I could write a subroutine to do it (I don't want to rely on anything that isn't natively in Windows since XP), but I'm having trouble. Here's what I have so far with help from various sources:
call:catMyChunk myCustomText c:\see.txt
exit /b
goto:myCustomText
This is my test file
Hope you like it.
<got these>
% and these %
! these too
yeah
:myCustomText
:catMyChunk
::Should call this function with 2 args, MYDELIM and outFile.txt
::where is to be catted to outFile.txt
::and text starts with <beginning of line>goto:MYDELIM
::and ends with <beginning of line>:MYDELIM
set searchStart=goto:%~1
set searchStop=:%~1
set outFile=%~2
set startLine=0
set endLine=0
for /f "delims=:" %%a in ('findstr -b -n !searchStart! %~dpnx0') do set "startLine=%%a"
for /f "delims=:" %%a in ('findstr -b -n !searchStop! %~dpnx0') do set "endLine=%%a"
set /a linesLeftToRead=%endLine% - %startLine%
del %outFile%
if "%linesLeftToRead%" LEQ "0" (
echo Error finding start and end delmieters for %searchStop% in catMyChunk routine
exit /B 1
)
setlocal DisableDelayedExpansion
for /f "usebackq skip=%startLine% delims=" %%a in (`"findstr /n ^^ %~dpnx0"`) do (
set "oneLine=%%a"
setlocal EnableDelayedExpansion
set "oneLine=!oneLine:*:=!"
set /a linesLeftToRead-=1
if !linesLeftToRead! LEQ 0 exit /B
echo(!oneLine!>>%outFile%
)
goto: EOF
So my requirement is that the chunk of text is output literally without any changes whatsoever (i.e. I don't want to have to escape blank lines, %, !, <, etc.). This code is doing almost everything I need, except I haven't found a way to get the exclamation points output properly. Here is the output I get, which isn't quite right:
This is my test file
Hope you like it.
<got these>
% and these %
these too
yeah
Edit:
For anyone wanting the modified version of the subroutine that now works, here it is:
:catMyChunk
::Should call this function with 2 args, MYDELIM and outFile.txt
::where is to be catted to outFile.txt
::and text starts with <beginning of line>goto:MYDELIM
::and ends with <beginning of line>:MYDELIM
set searchStart=goto:%~1
set searchStop=:%~1
set outFile=%~2
set startLine=0
set endLine=0
for /f "delims=:" %%a in ('findstr -b -n !searchStart! %~dpnx0') do set "startLine=%%a"
for /f "delims=:" %%a in ('findstr -b -n !searchStop! %~dpnx0') do set "endLine=%%a"
set /a linesLeftToRead=%endLine% - %startLine%
del %outFile%
if "%linesLeftToRead%" LEQ "0" (
echo Error finding start and end delmieters for %searchStop% in catMyChunk routine
exit /B 1
)
setlocal DisableDelayedExpansion
for /f "usebackq skip=%startLine% delims=" %%a in (`"findstr /n ^^ %~dpnx0"`) do (
set "oneLine=%%a"
set /a linesLeftToRead-=1
setlocal EnableDelayedExpansion
set "oneLine=!oneLine:*:=!"
if !linesLeftToRead! LEQ 0 exit /B
echo(!oneLine!>>%outFile%
endlocal
)
endlocal
goto: EOF
Your code is missing a single endlocal in your FOR-loop.
You will then create for each loop a new local-context through the setlocal EnableDelayedExpansion, this will explode with some more lines in your text file.
Also, to preserve the exclamation marks (and also the carets) you need the toggling technique:
DOS batch files: How to read a file?
setlocal DisableDelayedExpansion
for /f "usebackq skip=%startLine% delims=" %%a in (`"findstr /n ^^ %~dpnx0"`) do (
set "oneLine=%%a"
setlocal EnableDelayedExpansion
set "oneLine=!oneLine:*:=!"
set /a linesLeftToRead-=1
if !linesLeftToRead! LEQ 0 exit /B
echo(!oneLine!>>%outFile%
endlocal
)
+1, Interesting application! I modified your code for a simpler and faster version:
#echo off
call :catMyChunk myCustomText see.txt
exit /b
goto:myCustomText
This is my test file
Hope you like it.
<got these>
% and these %
! these too
yeah
:myCustomText
:catMyChunk
::Should call this function with 2 args, MYDELIM and outFile.txt
::where is to be catted to outFile.txt
::and text starts with <beginning of line>goto:MYDELIM
::and ends with <beginning of line>:MYDELIM
setlocal DisableDelayedExpansion
set searchStart=goto:%~1
set searchStop=:%~1
set outFile=%~2
if exist %outFile% del %outFile%
set copyFlag=No
echo No> copyFlag
for /f "delims=" %%a in (%~f0) do (
set "oneLine=%%a"
setlocal EnableDelayedExpansion
if !copyFlag! == No (
if !oneLine! == %searchStart% echo Yes> copyFlag
) else (
if !oneLine! == %searchStop% (
echo No> copyFlag
) else (
echo/!oneLine!>> %outFile%
)
)
endlocal
set /p copyFlag=< copyFlag
)
endlocal
goto :eof
I also created another version that looks more like the Linux version, that is, the lines to copy are placed directly after invoking the routine, and the execution continue after the last copied line. Of course, to make this possible the routine is not invoked via call, but entered with a goto, and when the routine ends it execute a goto %MYDELIM% instead of a "return" (exit /b or goto :eof). Also, because a goto can not have parameters, the "parameters" are defined in variables before the invocation.
#echo off
set searchStop=EndOfMyText
set outFile=see.txt
goto :catMyChunk EndOfMyText
This is my test file
Hope you like it.
<got these>
% and these %
! these too
yeah
:EndOfMyText
exit /b
:catMyChunk
::Before JUMP to this "function" define 2 vars: searchStop and outFile
::where is to be catted to %outFile%
::and text starts with goto :catMyChunk %searchStop%
::and ends with :%searchStop%
setlocal DisableDelayedExpansion
set searchStart=goto :catMyChunk %searchStop%
if exist %outFile% del %outFile%
set copyFlag=No
echo No> copyFlag
for /f "delims=" %%a in (%~f0) do (
set "oneLine=%%a"
setlocal EnableDelayedExpansion
if !copyFlag! == No (
if /I !oneLine! == !searchStart! echo Yes> copyFlag
) else (
if !oneLine! == :%searchStop% (
echo No> copyFlag
) else (
echo/!oneLine!>> %outFile%
)
)
endlocal
set /p copyFlag=< copyFlag
)
endlocal
goto %searchStop%
EDIT
This new version is even faster and now works with all special cases, including empty lines:
:catMyChunk
::Should call this function with 2 args, MYDELIM and outFile.txt
::where is to be catted to outFile.txt
::and text starts with <beginning of line>goto:MYDELIM
::and ends with <beginning of line>:MYDELIM
setlocal EnableDelayedExpansion
set searchStart=goto:%~1
set searchStop=:%~1
set outFile=%~2
if exist %outFile% del %outFile%
findstr /n ^^ "%~f0" > pipeline.txt
call :seekMyChunk < pipeline.txt
del pipeline.txt
exit /B
:seekMyChunk
set oneLine=:EOF
set /P oneLine=
if !oneLine! == :EOF goto startNotFound
set oneLine=!oneLine:*:=!
if not !oneLine! == %searchStart% goto seekMyChunk
:catNextLine
set oneLine=:EOF
set /P oneLine=
if !oneLine! == :EOF goto stopNotFound
set oneLine=!oneLine:*:=!
if !oneLine! == %searchStop% goto :eof
echo/!oneLine!>> %outFile%
goto catNextLine
:startNotFound
echo Error finding start delimiter for %searchStart% in catMyChunk
goto :eof
:stopNotFound
echo Error finding stop delimiter for %searchStop% in catMyChunk
goto :eof