i have case regarding control-M.
I have simple script batch and plan execute using control-M job, below detail script :
1.MasterParam.bat
#echo
Set PathPackage=D:\test\
2.Main.bat
#echo off
Set PackageName=Package.dtsx
Call MasterParam.bat
Dtexec /f %PathPackage%%PackageName%
If %errorlevel% NEQ 0 ( exit /b %errorlevel%)
Question :
Can control-M execution script.bat with containing Call another scritp.bat?
wheter control-M stop the job if script.bat return errorlevel !=0?
In addition to the original answers, note that you can run Control-M jobs as direct commands (instead of executing .bat or .cmd files) which is often a better way to control the task (and works fine with dtexec /F ...).
If %errorlevel% NEQ 0 command means Not Equal to 0 and yes control-M can stop the job if script.bat return errorlevel !=0
By using Call MasterParam.bat or even by Call MasterParam
u can also pass parameters while calling the script -Calls one batch program from another without stopping the parent batch program. The call command accepts labels as the target of the call.
Related
I have a job that runs a batch file as step1. I edited the batch file ( mybatch.bat ) as follows:
call sub: >>mylog.txt
exit /b %ERRORLEVEL%
command1
command2
As you can see I forgot to add the sub. The job step is
cmd.exe mybatch.bat || echo %ERRORLEVEL% .
The job runs as successful and the history is as follows:
call :sub 1>>D:\Server_Directory\Logging\Cmd_Shell\SQL_APD_download.log The system cannot find the batch label specified - sub C:\Windows\system32>exit /b 1 0. Process Exit Code 0. The step succeeded.
How do I get SQL to fail the job, and not report Exit Code 0 ?
I made a Main batch file with the lines below:
#echo off
color 1e
title ------ Just a Test ------
start "C:\Users\%USERNAME%\Desktop\Check.bat"
:START
echo Welcome to the Game!
...
And Check.bat contains:
#echo off
if not exist "C:\Users\%USERNAME%\Desktop\Batch_System\importantFile.dll" goto ERROR
if exist "C:\Users\%USERNAME%\Desktop\Batch_System\importantFile.dll" goto CONTINUE
:ERROR
cls
echo ERROR :
echo Important file not found. please reinstall the program
pause
exit /b
:CONTINUE
cls
exit /b
When I use the command start, it starts only a command prompt with the Check.bat directory and the main batch file continues executing the game. I want to force close the main batch file if importantFile.dll doesn't exist.
Okay, let me explain: When the main batch file is executed and runs the command start to start another batch file called Check.bat, the file Check.bat checks if the file importantFile.dll exists, and if not, Check.bat displays an error message.
Does anyone know how to write Check.bat in a manner that when the .dll file does not exist, force the main batch file to exit?
First, help on every command can be get by running in a command prompt window the command with /? as parameter. start /? outputs the help of command START. call /? outputs the help of command CALL usually used to run a batch file from within a batch file. Those two commands can be used to run a batch file as explained in detail in answer on How to call a batch file that is one level up from the current directory?
Second, the command line
start "C:\Users\%USERNAME%\Desktop\Check.bat"
starts a new command process in foreground with a console window with full qualified batch file name as window title displayed in title bar at top of the console window. That is obviously not wanted by you.
Third, the Wikipedia article Windows Environment Variables lists the predefined environment variables on Windows and their default values depending on version of Windows.
In general it is better to use "%USERPROFILE%\Desktop" instead of "C:\Users\%USERNAME%\Desktop".
There is no C:\Users on Windows prior Windows Vista and Windows Server 2008 by default at all.
The users profile directory can be on a different drive than drive C:.
It is also possible that just the current user's profile directory is not in C:\Users, for example on a Windows server on which many users can logon directly and for which the server administrator decided to have the users' profile directories on a different drive than system drive making backup and cleaning operations on server much easier and is also better for security.
Well, it is also possible to have the user's desktop folder not in the user's profile directory. But that is really, really uncommon.
Fourth, on shipping a set of batch files, it is recommended to use %~dp0 to call other batch files from within a batch file because of this string referencing drive and path of argument 0 expands to full path of currently executed batch file.
The batch file path referenced with %~dp0 always ends with a backslash. Therefore concatenate %~dp0 always without an additional backslash with another batch file name, folder or file name.
See also What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory?
Fifth, I suggest following for your two batch files:
Main.bat:
#echo off
color 1e
title ------ Just a Test ------
call "%~dp0Check.bat" || color && exit /B
echo Welcome to the Game!
Check.bat:
#echo off
cls
if exist "%~dp0Batch_System\importantFile.dll" exit /B 0
echo ERROR:
echo Important file not found. Please reinstall the program.
echo/
pause
exit /B 1
The batch file Check.bat is exited explicitly on important file existing with returning exit code 0 to the parent batch file Main.bat. For that reason Windows command processor continues execution of Main.bat on the command line below the command line calling the batch file Check.bat.
Otherwise Check.bat outputs an error message, waits for a pressed key by the user and exits explicitly with non zero exit code 1. The non zero exit code results in Main.bat in executing the next command after || which is COLOR to restore initial colors and next executing also EXIT with option /B to exit the execution of Main.bat.
See also:
Single line with multiple commands using Windows batch file
What are the ERRORLEVEL values set by internal cmd.exe commands?
Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success?
Where does GOTO :EOF return to?
exit /B without an additionally specified exit code is like goto :EOF.
The CALL command line in Main.bat could be also written as:
call "%~dp0Check.bat" || ( color & exit /B )
And Main.bat could be also written as:
#echo off
color 1e
title ------ Just a Test ------
call "%~dp0Check.bat"
if errorlevel 1 (
color
goto :EOF
)
echo Welcome to the Game!
I do not recommend using in Main.bat just EXIT instead of exit /B or goto :EOF. Just EXIT would result in exiting the current command process independent on calling hierarchy and independent on how the command process was started: with option /K to keep it running to see error messages like on opening a command prompt window and next running a batch file from within command prompt window, or with /C to close the command process after application/command/script execution finished like on double clicking on a batch file.
It is advisable to test batch files by running them from within an opened command prompt window instead of double clicking on them to see error messages on syntax errors output by cmd.exe. For that reason usage of just EXIT is counter-productive for a batch file in development. Run cmd /? in a command prompt window for help on Windows command processor itself.
Last but not least see:
Microsoft's command-line reference
SS64.com - A-Z index of the Windows CMD command line
start is asynchronous by default. Use start /wait so that main.bat can test the exit code of check.bat. Make check.bat return an appropriate exit code.
For example...
main.bat
#echo off
start /b /wait check.bat
if not %errorlevel% == 0 exit /b
echo "Welcome to the game!"
...
check.bat
#echo off
if exist "importantfile.dll" exit 0
echo ERROR: Important file not found. Please reinstall the program.
pause
exit 1
notes
Added /b to start to avoid opening another window. Change that per your preference.
You could use call instead of start but call gives the called code access to the variables of main.bat so encapsulation is improved if you use start as you did.
The logic in check.bat is simplified above. Once you identify the success path early in the script and exit, the rest of the script can assume the fail path. This saves you a few if's and labels which you might find simplifies writing and reading of similar scripts. Beware of potentially confusing multiple exit points in longer scripts though!
When choosing exit codes, 0 is a common convention for success.
The above code is just one technique - there are several other options (such as checksomething && dosomethingifok). Some useful information on return codes, and checking them, can be found in http://steve-jansen.github.io/guides/windows-batch-scripting/part-3-return-codes.html
Thanks to the answer from Mofi. I've my example and exp. on this. To be short, it's about the setting of log date format. You may change the format of time , date and log. you may have the result.
why-batch-file-run-with-failure-in-windows-server
When I run command Start C:\temp\sub2.bat in a command prompt window, errorlevel set is 4 and this is correct because a file which is indicated in batch does not exist. But when I run through this below, errorlevel returns 0. I have no idea why the exit codes are different.
Can anyone give me an advice for the reason?
#echo off
Call :Sub1
GOTO :EOF
:Sub1
start C:\temp\sub2.bat
echo %errorLevel%
Read the answer on How to call a batch file that is one level up from the current directory? It explains the 4 methods which exist to run a batch file from within a batch file and what are the differences.
You use command start which results in starting a new command process running parallel to the command process already executing your batch file for execution of C:\temp\sub2.bat.
The current command process immediately continues the execution of posted batch file and evaluates the exit code of command start which is 0 on successfully starting the executable or batch file.
You should use the command call to run the batch file as subroutine in your batch file and which makes it possible to evaluate the exit code set by C:\temp\sub2.bat.
#echo off
call C:\temp\sub2.bat
if errorlevel 1 echo There was an error with exit code %ERRORLEVEL%.
It would be also possible to start the other batch file in a new command process and what on its termination for example with exit 4 in current batch file.
#echo off
start /wait C:\temp\sub2.bat
if errorlevel 1 echo There was an error with exit code %ERRORLEVEL%.
In general it is not advisable to use exit without option /B as this results always in exiting the command process independent on calling hierarchy which makes it also impossible to debug a batch file by running it from within a command prompt window.
In case of C:\temp\sub2.bat really contains exit ExitCode without option /B and for some unknown reason the batch file can't be edited, it is really necessary to start C:\temp\sub2.bat in a separate command process and wait for its termination with start /wait.
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 /?
echo /?
exit /?
if /?
start /?
Read also the Microsoft support article Testing for a Specific Error Level in Batch Files.
See also the Stack Overflow questions:
What are the ERRORLEVEL values set by internal cmd.exe commands?
Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success?
So now i have 2 .bat files. one copies some file if it was updated ( robocopy C:\location C:\destination) and another one that executes a some kind of .exe file (start c:\BAT\fraps.exe) , now what i need is maybe a one file, so that WHEN a file was copied using "robocopy" the executive file would run automaticaly. So maybe there is a way to merge them into one or smth.
Errorlevels are set by robocopy: errorlevel 1 means that a file was successfully copied.
robocopy C:\location C:\destination
if errorlevel 1 if not errorlevel 2 start c:\BAT\fraps.exe
Here is proof of concept code - following extended comments:
#echo off
md test1
:loop
>test1\testfile.txt echo aaa
robocopy test1 test2
if errorlevel 1 if not errorlevel 2 pause
del test1\testfile.txt
goto :loop
Use /WAIT option, when the application is stared then it will wait until it terminates.
Use /B option, when application is started then it will not create a new window.
Example:
start /wait Command CALL D:\YourFirstScript.bat
start /wait program.exe
start /wait Command CALL X:\YourSecondScript.bat
It's a good idea to print a message before and after.
Example:
ECHO Starting program.
start /wait program.exe
ECHO Finished.
See below link for more details.
How do I launch multiple batch files from one batch file with dependency?
Note: When you run script as administrator then you need to set full path as the default is set to "C:\Windows\System32".
The easiest way to set is
start %~dp0Directory\program.exe
See for details about "%~dp0" here
What does %~dp0 mean, and how does it work?
This is my first post and I hope that this will help you.
I have the following layout for my test suite:
TestSuite1.cmd:
Run my program
Check its return result
If the return result is not 0, convert the error to textual output and abort the script. If it succeeds, write out success.
In my single .cmd file, I call my program about 10 times with different input.
The problem is that the program that I run 10 times takes several hours to run each time.
Is there a way for me to parallelize all of these 10 runnings of my program while still somehow checking the return result and providing a proper output file and while still using a single .cmd file and to a single output file?
Assuming they won't interfere with each other by writing to the same files,etc:
test1.cmd
:: intercept sub-calls.
if "%1"=="test2" then goto :test2
:: start sub-calls.
start test1.cmd test2 1
start test1.cmd test2 2
start test1.cmd test2 3
:: wait for sub-calls to complete.
:loop1
if not exist test2_1.flg goto :loop1
:loop2
if not exist test2_2.flg goto :loop2
:loop3
if not exist test2_3.flg goto :loop3
:: output results sequentially
type test2_1.out >test1.out
del /s test2_1.out
del /s test2_1.flg
type test2_2.out >test1.out
del /s test2_2.out
del /s test2_2.flg
type test2_3.out >test1.out
del /s test2_3.out
del /s test2_3.flg
goto :eof
:test2
:: Generate one output file
echo %1 >test2_%1.out
ping -n 31 127.0.0.1 >nul: 2>nul:
:: generate flag file to indicate finished
echo x >test2_%1.flg
This will start three concurrent processes each which echoes it's sequence number then wait 30 seconds.
All with one cmd file and (eventually) one output file.
Running things in parallel in batch files can be done via 'start' executable/command.
Windows:
you create a Batch File that essentially calls:
start TestSuite1.cmd [TestParams1]
start TestSuite1.cmd [TestParams2]
and so on, which is essentially forking new command lines,
which would work, if the application can handle concurrent users (even if its the same User), and your TestSuite1.cmd is able to handle parameters.
You will need to start the script with different parameters on different machines because whatever makes the program take so long for a task (IO, CPU time) will be in even shorter supply when multiple instances of your program run at once.
Only exception: the run time is cause by the program putting itself to sleep.
try the command start, it spawns a new command prompt and you can send along any commands you want it to run.
I'd use this to spawn batch files that run the tests and then appends to a output.txt using >> as such:
testthingie.cmd >> output.txt