It's hard for me to explain my problem, so let's see the code :
#echo off
cls
set errors=0
bin\behat > test.txt || set /A errors=errors+1
if %errors% EQU 0 goto ok
goto ko
:ok
echo .
echo .OK
goto end
:ko
echo .
echo .KO
:end
When I launch this batch file, I never see the OK/KO output. It looks like the batch stopped after the call to behat. But I need it to continue, in order to detect errors and manage them.
This batch works fine with other calls and I need behat being called after them. Has someone an idea to resolve this ?
Thanks
You are calling bin\behat.bat, a batch file.
When a batch file directly invokes another one, the execution flow is transfered to the called one and does not return to the caller.
To call another batch file and allow the execution to return to the caller, you will need to use the call command
....
call bin\behat > test.txt || set /A errors=errors+1
....
Related
I am trying the following:
start /wait /B "C:\Users\Kiriti_Komaragiri\Desktop\sample" npm i
echo Y
start /wait /B "C:\Users\Kiriti_Komaragiri\Desktop\sample2" npm i
I would like to run the above in the same window with auto response "Y"
Currently, its running only the first command and not the third one. I am not sure why?
Here you go.
echo Y | start /wait /B "C:\Users\Kiriti_Komaragiri\Desktop\sample" npm i
echo Y | start /wait /B "C:\Users\Kiriti_Komaragiri\Desktop\sample2" npm i
More information please. What type of command are you trying to feed the response to? Is it Choice, Set /p, or something else your trying to feed a response to?
Without knowing more, the only suggestion I can make requires the existence of a label before the input is processed in the secondary batch.
A workaround exists whereby you can call and arrive at a label in another Batch by calling a label with the same name in your calling Batch. This allows you to define the value of the input (Whatever form the input takes) in your primary batch, then do as follows (substituting label names, variable names and file paths as appropriate)
-In the Calling (Primary) Batch:
Set ResponseVarName=Y
Call :targetLabel
(whatever code your batch has in between)
REM this is where you make your hack 'Call' to the other batch, without actually 'Calling' the batch itself.
:targetLabel
%userprofile%\desktop\yourotherbatch.bat
exit /b
Just to be sure your absolutely clear, this workaround is utterly dependant on being sent to a specific label after the response is set, BEFORE other commands are executed.
(EDIT-) A couple of example programs to show the concept:
::::::::: %userprofile%\desktop\HomeBatch.bat ::::::::::::::::
#ECHO OFF
:main
Set TestEnvironment=1
Call :targetLabel
:nottarget
ECHO NOT target
pause
exit
:targetLabel
%userprofile%\desktop\OtherBatch.bat npm i
:homeBatch
ECHO returned Home
pause
GOTO main
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::: %userprofile%\desktop\OtherBatch.bat ::::::::::::::::
#ECHO OFF
:NottheTarget
ECHO NOT THE TARGET
pause
exit
:targetLabel
ECHO Found the Target. TestEnvironment=%TestEnvironment% %~1 %~2
pause
Exit /b
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I have a batch (actually two, one simple, one complex) file to send IR commands to a blaster for HTPC. I didn't write the batch files myself; too complex. Both channel_changing batch files have input parameters, one just {channel} (=3 numbers) and the more complex {channel} and {IRdefinition} (an IR definition file). I cannot invoke start /priority from the calling App directly (NextPVR) which is why I need to use two batches.
All I need is to run the channel_changing batches at high priority.
I've tried calling the channel_changing batch from a simple starter_batch as follows:
GXPC4312H_StarterUUTX.bat
REM
SET command=GXPC4312H_UUTX.bat
REM SET command=GXPC4312H_Changer.bat for the complex one
start "" /REALTIME /B %command%
REM
Simple Channel Changer Batch GXPC4312H_UUTX.bat
#echo off
cd C:\users\public\NPVR\ChanChg\
set "channel=%1"
set "num=-1"
:loop
set /a num=num+1
call set "name2=%%channel:~%num%,1%%"
if defined name2 (
uutx.exe -r3 -s10 -fGXPC4312H.ir %name2%
echo %name2%
goto :loop
)
Complex Channel Changer Batch GXPC4312H_Changer.bat
set sleep=0
set loops=5
:WAIT
IF EXIST C:\Users\Public\NPVR\ChanChg\usblock.dat (GOTO PING) ELSE (GOTO CC)
:PING
timeout 1
set /A sleep=%sleep%+1
IF %sleep% EQU %loops% GOTO ERROR
GOTO WAIT
:CC
ECHO wait_lock > C:\Users\Public\NPVR\ChanChg\usblock.dat
set input=%1
if not "%input:~0,1%" == "" C:\Users\Public\NPVR\ChanChg\uutx.exe -r3 -s200 -f%~dp0%2 %input:~0,1%
if not "%input:~1,1%" == "" C:\Users\Public\NPVR\ChanChg\uutx.exe -r3 -s200 -f%~dp0%2 %input:~1,1%
if not "%input:~2,1%" == "" C:\Users\Public\NPVR\ChanChg\uutx.exe -r3 -s200 -f%~dp0%2 %input:~2,1%
if not "%input:~3,1%" == "" C:\Users\Public\NPVR\ChanChg\uutx.exe -r3 -s200 -f%~dp0%2 %input:~3,1%
del C:\Users\Public\NPVR\ChanChg\usblock.dat
exit
:ERROR
ECHO %DATE% %TIME% >> C:\Users\Public\NPVR\ChanChg\changer_error.log
GOTO :CC
I ran the first one with from an elevated command prompt with the one {channel} argument e.g. 115
What I got was a batch file that never ended and threw lots of errors.
It seemed NOT to be passing the channel number parameter across from the starter batch to the changing batch but I read they were passed across?
Error: unable to find codeName '~0,1' in codeFile 'GXPC4312H.ir'
~0,1
Error: unable to find codeName '~1,1' in codeFile 'GXPC4312H.ir'
~1,1
Error: unable to find codeName '~2,1' in codeFile 'GXPC4312H.ir'
~2,1
etc etc
When I ran the second starter with the TWO arguments {channel} {IRDef.ir} e.g. 115 GXPC4312H.ir I got a simple error that
"The syntax of the command is incorrect"
I'm not really sure what is going on here, as I've sort of inherited these batch files (and modified just path names and IRdef Files). Whilst I can follow the logic in them to some degree, I am struggling to troubleshoot the errors.
resources:
start "Name" /Priority "PATH .. executable" --arguments
https://www.tenforums.com/tutorials/89548-set-cpu-process-priority-applications-windows-10-a.html#option4
I've read here to use call but I believe I need to use start as I
wish to have the batch file run as high priority?
Batch script stops after first call to other batch script
I know you can alsio use wmic but I've read here not to use wmic as
it is not reliable (and I am happy using the start "" /priority
switch anwyay).
https://superuser.com/questions/620724/changing-windows-process-priority-via-command-line?noredirect=1&lq=1
Variable are passed from one batch file to another.
How to pass variables from one batch file to another batch file?
I am calling batch file (say file2.bat) from another batch file (say batch file 1).
file1.bat:
echo off
#call file2.bat
#echo abc
#echo xyz
exit
I am using command exit /b to exit from file2.bat ..
For some reason, the control is not coming back to file1.bat after file2.bat exits..
What needs to be done in order to return the control back to file1.bat so that the remaining commands #echo abc #echo xyz should execute?
Thanks
Use either cmd /c file2.bat or use goto :EOF (Where EOF means end of file), goto :eof should work in your case
I am trying to create a batch file which is doing these things :
move some folder
call another batch file
copy the result of batch file to clipboard
run the imacros (imacros will use the clipboard)
So the important thing is I have to be sure for every step that it is completed. Because every step is attached together.
My question is how can I do that ? I read something about SLEEP, PING, TIMEOUT, PAUSE etc.. But I cannot know very much prons and cons. Could someone give some tips about these commands or which should i use for safe programming ?
Not : I am working on windows xp and windows server 2008
And here is my batch code :
#echo off
set cwd=D:\workset\xx\yyy\
set wp=D:\workset\xx\yyyy\zzzzz\
:: ensure folder exist
mkdir %cwd% > nul 2>&1
mkdir %wp%\done > nul 2>&1
d:
cd %wp%
:export
::echo tidy folder %wp%
for %%i in (xx_to_yy*.zip) do (
move "%%i" "done\%%i"
)
echo call %cwd%aaa_bbb_export.bat
call %cwd%aaa_bbb_export.bat 5555
#echo off
for %%i in (xx_to_yy*.zip) do (
echo %wp%%%i | clip
move "%%i" "done\%%i"
)
#echo off
:finish
start "" "C:\Program Files\Mozilla Firefox\firefox.exe" http://www.google.com
ping 127.1.1.1
start "" "C:\Program Files\Mozilla Firefox\firefox.exe" imacros://run/?m=yyy.iim
Using the pause and echo command is a great way to debug a piece of scrip in batch.
Simply add:
Echo Task Complete: [Description] : Continue ?&>nul pause
At the end of every task. That way, you can check if everythings fine before giving the all clear for you batch file to continue. Later on, you'll learn to do error checking in batch files, in which case you could do something similar with an error message and include soloutions as options.
Edit:
If you wish to automate the script, so that it doesn't need user input, and instead attempts to check ikf an error occured you could either:
Check manualy, that is 1: check if folder has moved, 2: check if other batch file did what it needed (create new files etc.) 3: check if clipboard has suffiecent lines to be result of batch files (maybe clear it at the start) and lastly, check if imacros did what was needed
Check if commands were succeful. (redirec errors to a text file and then see if they were any.
Since, I don't know exactly what you are doing, I can only help you wit case 2 (though 1 would be better). You redirect the errors as so:
command [parameters] 2>>errors.log
And at the end of every section:
set /p error=<errors.log
if defined error (
Echo An Error Occured : Last Step == [Step Description]
ECHO ---------- RUN : %Date% %Time% ----------
type errors.log >> error-log.txt
del errors.log
if "%~1"=="RESTART" (
Echo Error Occured Two Times in a Row . . .
Echo Exiting in 20 seconds . . .
Exit /b 9
)
Echo Restarting procedure in 60 seconds . . .
sleep 60
Start %~f0 "RESTART"
Exit
)
The above code will check if there were any errors, add them to a log called error-log.txt. If there is an error, it will restart and if there is an error again, it will exit. There will be a small pause before either of these events, incase you want to cancel it (simply close)
Hope you found this helpful,
Mona.
First option is, after each "critical" command, check for errorlevel
copy someThing someWhere
if errorlevel 1 (
rem something goes bad
goto somewhereToHandleIt
)
Alternatively, you can add code to check that the operation was sucessful, testing everything is where it is supossed to be. But first option is usually better. Trust the system, it will do the work and report when problems found.
I need to know if the task ran successfully so I can create an event in the Application log saying so. Is there a way to get this in code? I tried the following:
echo ErrorLevel of "c:\windows\system32\tasks\my task" = %ErrorLevel%
But I get 0 every time, even if I stop it prematurely (0x41306) or while the task is still running (should be 0x41301). Does anyone have any ideas? Thank you.
I found a workaround to this. Instead of getting the exit code of the task, I got the exit code of the batch script that actually runs and if it's anything but 0 then I make an error application event, otherwise it's a success application event.
The following batch file accepts a parameter of a task name, for instance if you named the bat file "getresult.bat" you would call "getresult GoogleUpdateTaskMachineCore" (If the name has spaces, put quotes around it).
This is very verbose, so let me know if you need help adapting it to fit your needs.
Tested and working in Windows 8, I believe it should work for XP/Vista/7 as well.
#ECHO OFF
IF %1=="" GOTO EXITNOINPUT
ECHO Checking Tasks for "%1"...
FOR /F "tokens=2delims=:" %%I IN ('schtasks /tn %1 /fo LIST /v ^| FIND "Last Result"') DO (
SET result=%%I
)
IF NOT DEFINED result GOTO EXITNOTFOUND
ECHO Done...
ECHO The Last Result Was: %result%
GOTO EXITNORMAL
:EXITNOTFOUND
echo The scheduled task was not found.
GOTO EXITNORMAL
:EXITNOINPUT
echo You must provide a query. (getresult servicename)
GOTO EXITNORMAL
:EXITNORMAL