Batchfile: Problems to get errorlevel, it is always 0 - batch-file

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

Related

Return value from a cmd function

I have a batch function and I need to return a value from it. Following is the script:
#echo off
call :Mode mode1
echo mode is %mode1%
:Mode
setlocal enabledelayedexpansion
set count=0
for /f "tokens=*" %%x in (map.txt) do (
set /a count+=1
set var[!count!]=%%x
)
for /f "tokens=2 delims=: " %%A in ("%var[2]%") Do (
set mode=OPEN
)
IF %mode%==OPEN (
echo coming into open
set %1=OPEN
echo %mode1%
) ELSE (
echo coming into shorted
set %1=SHORTED
echo %mode1%
)
EXIT /B 0
echo mode is %mode1% doesn't print anything. Any help? I've hardcoded set mode=OPEN for testing purposes.
Each exit, exit /b or goto :eof implicitly does an endlocal, so you need a trick for your variable %1 to survive an endlocal. endlocal & set ... & goto :eof does the trick because the whole line gets parsed in one go:
#echo off
call :Mode mode1
echo mode is %mode1%
goto :eof
:Mode
setlocal enabledelayedexpansion
set "mode=OPEN"
IF "%mode%" == "OPEN" (
echo coming into open
endlocal&set "%1=OPEN"&goto :eof
) ELSE (
echo coming into shorted
endlocal&set "%1=SHORTED"&goto :eof
)
For the same reason, echo %mode1% in your subroutine does not print the variable.
Note: I changed set and if syntax to recommended quoted syntax.

Adding a Timeout to Set /p in batch

I am trying to add a time out to Set /p Var1= So far I have only found this piece of code from here.
#echo off
setlocal EnableDelayedExpansion
if "%1" NEQ "" goto %1
del enter.tmp 2>nul >nul
start /b cmd /c %0 :secondThread
:FirstThread
set n=0
echo Enter Text (5 seconds timeout):
:loop
set /a n+=1
ping -n 2 localhost > nul
if !n! LSS 5 (
if not exist entER.tmp goto :loop
< Enter.tmp (
set /p input=
)
echo !input!
) ELSE (
echo Timeout for input
)
exit /b
:secondThread
set /p var=
> enter.tmp echo !var!
exit /b
This piece of code works great except it stops the count down only when the input is entered. I would like the count down to stop when any key is pressed. I don't know if this is possible. Thanks

Only accept numeric characters in batch file input

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

Batch file run cmd1 if time 10pm-4am else run cmd2

I have a batch file and within that batch file I need to run one of two commands depending on time of my server.
If the time is between 22:00:00 and 03:30:00 -- xcopy /Y a\1.txt c\1.txt
If the time is before or after this range -- -- xcopy /Y b\1.txt c\1.txt
This will use xcopy to switch a file back and forth depending on the time.
I know this is easy but my brain just won't work atm lol
Edit:
Went with 22:00 and 4:00... this is what I came up with but it doesn't seem like the best way...
set current_time=%time:~0,5%
if "%current_time%" lss "22:00" goto daycycle
if "%current_time%" gtr " 4:00" goto daycycle
echo Do this between 10pm and 4am
goto continue
:daycycle
echo Do this before 10pm and after 4am
:continue
#echo off
setlocal enableextensions disabledelayedexpansion
set "now=%time: =0%"
set "task=day"
if "%now%" lss "03:30:00,00" ( set "task=night" )
if "%now%" geq "22:00:00,00" ( set "task=night" )
call :task_%task%
endlocal
exit /b
:task_day
:: do daily task
goto :eof
:task_night
:: do nightly task
goto :eof
EDITED - The previous code should work under the conditions in the original question. But will fail in different time configurations. This should solve the usual problems
#echo off
setlocal enableextensions disabledelayedexpansion
call :getTime now
set "task=day"
if "%now%" lss "03:30:00,00" ( set "task=night" )
if "%now%" geq "22:00:00,00" ( set "task=night" )
call :task_%task%
echo %now%
endlocal
exit /b
:task_day
:: do daily task
goto :eof
:task_night
:: do nightly task
goto :eof
:: getTime
:: This routine returns the current (or passed as argument) time
:: in the form hh:mm:ss,cc in 24h format, with two digits in each
:: of the segments, 0 prefixed where needed.
:getTime returnVar [time]
setlocal enableextensions disabledelayedexpansion
:: Retrieve parameters if present. Else take current time
if "%~2"=="" ( set "t=%time%" ) else ( set "t=%~2" )
:: Test if time contains "correct" (usual) data. Else try something else
echo(%t%|findstr /i /r /x /c:"[0-9:,.apm -]*" >nul || (
set "t="
for /f "tokens=2" %%a in ('2^>nul robocopy "|" . /njh') do (
if not defined t set "t=%%a,00"
)
rem If we do not have a valid time string, leave
if not defined t exit /b
)
:: Check if 24h time adjust is needed
if not "%t:pm=%"=="%t%" (set "p=12" ) else (set "p=0")
:: Separate the elements of the time string
for /f "tokens=1-5 delims=:.,-PpAaMm " %%a in ("%t%") do (
set "h=%%a" & set "m=00%%b" & set "s=00%%c" & set "c=00%%d"
)
:: Adjust the hour part of the time string
set /a "h=100%h%+%p%"
:: Clean up and return the new time string
endlocal & if not "%~1"=="" set "%~1=%h:~-2%:%m:~-2%:%s:~-2%,%c:~-2%" & exit /b
Try this:
#echo off
set hour=%time:~0,2%
set min=%time:~3,2%
if %hour% GEQ 22 (
xcopy /Y a\1.txt c\1.txt
) ELSE (
if %hour% LEQ 3 (
if %hour% EQU 3 if %min% GTR 30 (
xcopy /Y b\1.txt c\1.txt
goto :END
)
xcopy /Y a\1.txt c\1.txt
) ELSE (
xcopy /Y b\1.txt c\1.txt
goto :END
)
)
:END
And I'm rather sure that will do what you want. Manual if statements for the win! Note it would be a lot easier if it was 22:00 to 4:00 or 3:00. I had to incorperate the :30 minute checker.
But yea, it might not work, so just check it before you put it up on your server.
Monacraft.
What should work:
#echo off
:loop
set hour=%time:~0,2%
set min=%time:~3,2%
cls
ECHO %hour%
ECHO %min%
ECHO This %I%
IF %hour% == 14 GOTO Test2
goto loop
:Test2
IF %min% == 58 GOTO YUP
IF %min% == 59 GOTO LATE
Goto loop
:YUP
SET I=0
GOTO loop
:LATE
SET I=NOPE
GOTO loop

How do I reduce the codes require to check error

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

Resources