Bypass the messages - Batch File - batch-file

I have written the batch file as below :
D:\Build\Build_2.0.023\GrConv\FTGraphicConverter.exe -s "D:\Customer Data\JSM Prj\JSM_DEMO\HIS0364\WINDOW\L3_TB3B_MNFLD.EDF" -d "D:\FastTools\FTOutput\common" -l "D:\FastTools\VPInput\CentumTagsToFASTTOOLSitems.csv"
D:\Build\Build_2.0.023\GrConv\FTGraphicConverter.exe -s "D:\Customer Data\JSM Prj\JSM_DEMO\HIS0364\WINDOW\L3_TB3B_MNFLD.EDF" -d "D:\FastTools\FTOutput\common" -l "D:\FastTools\VPInput\CentumTagsToFASTTOOLSitems.csv"
xcopy D:\FastTools\FTOutput\common\Displays C:\Users\Public\tls\wap\cfg\operatorInterfaces\DEPLOY\displays /y
when i am running it and due to some reason the 1st line fails then it shows the error message on command windows and does not move the 2nd line of execution without manual intervention.
I want to suppress the error message and move to second line of execution automatically .

Use Start /B to don't stop the execution of your script when launching a process, that means all process starting with "/B" will start at once:
Start /B Process.exe
Start /B "" "D:\Build\Build_2.0.023\GrConv\FTGraphicConverter.exe" -s "D:\Customer Data\JSM Prj\JSM_DEMO\HIS0364\WINDOW\L3_TB3B_MNFLD.EDF" -d "D:\FastTools\FTOutput\common" -l "D:\FastTools\VPInput\CentumTagsToFASTTOOLSitems.csv"
Use the output redirection to exclude the error messages: http://ss64.com/nt/syntax-redirection.html
Simplified code:
#Echo OFF
Set "FTGraphicConverter_ExecutablePath=D:\Build\Build_2.0.023\GrConv\FTGraphicConverter.exe"
Set "EDF_File=D:\Customer Data\JSM Prj\JSM_DEMO\HIS0364\WINDOW\L3_TB3B_MNFLD.EDF"
Set "CSV_File=D:\FastTools\VPInput\CentumTagsToFASTTOOLSitems.csv"
Set "Output_Dir=D:\FastTools\FTOutput\common"
Set "Input_Displays_Dir=D:\FastTools\FTOutput\common\Displays"
Set "Outut_Displays_Dir=C:\Users\Public\tls\wap\cfg\operatorInterfaces\DEPLOY\displays"
Start /B "" "%FTGraphicConverter_ExecutablePath%" -s "%EDF_File%" -d "%Output_Dir%" -l "%CSV_File%" 2>NUL
%FTGraphicConverter_ExecutablePath% -s "%EDF_File%" -d "%Output_Dir%" -l "%CSV_File%"
xcopy /y "%Input_Displays_Dir%\*" "%Outut_Displays_Dir%\"
EDIT
If you don't want to launch process in /Background and only want to interact with the error message autmatically then:
Echo Y| Start process.exe
If process.exe sends at the end a error message with a user prompt asking a question for "Y" "N" key then we automatically are sending the "Y" key so, if error at the end the execution will not stop.

Related

How to get the return code of a batch started with the PSEXEC command?

I use the PSEXEC command to start a batch file on a remote computer:
psexec \\remotemachine -s -d cmd.exe /c c:\test_dir\build_dummy.bat
The build_dummy.bat script:
#echo off
SETLOCAL EnableDelayedExpansion
>output_build_bummy.bat.log (
rem just print something into an output file
echo.
echo This is a dummy batch script
rem close the file output
)
EXIT /B -12345
I want that psexec returns the code -12345
However, I get just the process ID of the started cmd.exe.
How can I get the error code?
Error code of any command is stored in %errorlevel% variable. Simply type echo %errorlevel% and you will get it.
I discovered that if I omit the option -d in the psexec call then psexec returns exactly what I need - the exit code of my batch script
:)

How to get a prompt for information using a batch file?

I currently have a .bat file named RunRDA.bat which contains:
#echo off
rda -v %1 -i "C:\me\rda-tools-1.7.0.Ra1\Input" -o "C:\me\rda-tools-1.7.0.RC1\Output"
Which I can run by navigating to the following folder for example:
C:\RDA>
and entering a command like
C:\RDA>RunRDA 848
so the batch file takes the input number and runs the command.
As you can see this requires a step of navigating to the specific folder c:\RDA before running the .bat file. I was wondering if there is a way I could double click to open the .bat file so that when CMD opens all i need to do is enter the input number and hit enter, without having to navigate to the mentioned directory, therefore eliminating the navigation step.
If I can understand your target, next code snippet could lead to a solution:
#echo off
setlocal
set "param=%1"
if not defined param set /P "param=Please enter the input number: "
if not defined param goto :doNothing
pushd C:\RDA
rda -v %param% -i "C:\me\rda-tools-1.7.0.Ra1\Input" -o "C:\me\rda-tools-1.7.0.RC1\Output"
echo done with %param%
popd
goto :doNext
:doNothing
echo no input number defined!
:doNext
pause
Resources (required reading):
An A-Z Index of the Windows CMD command line
Windows CMD Shell Command Line Syntax
This opens a console window and prompts the user to enter the number before continuing with rda:
#echo off
set /p rda_param=Enter rda parameter:
rda -v %rda_param% -i "C:\me\rda-tools-1.7.0.Ra1\Input" -o "C:\me\rda-tools-1.7.0.RC1\Output"

I need my batch file to execute 3 files at the same time - wait for them to finish - then move on to send the email

Here's what I've got:
START 1_0.bat
START 1_0_1.bat
START 1_0_2.bat
START /w sendEmail -f blaahg#gmail.com -t blahg#gmail.com .....
Anyway,
I want it to simulataneously execute the first three items (which it is) then wait for all of them to complete - then execute the last email step. It's currently not waiting to execute the last step.
REM start the processes, giving them a "unique" title:
START "MyProcess1" 1_0.bat
START "MyProcess2" 1_0_1.bat
START "MyProcess3" 1_0_2.bat
:loop
timeout /t 1 >nul
REM check if a task with the "unique" title is still running:
tasklist /v|find "MyProcess">nul && goto :loop
echo all tasks have finished.
&& works as "if previous command (find) was successful, then...
Best to put these into another batch file like step1.bat
Step1.bat
START 1_0.bat
START 1_0_1.bat
START 1_0_2.bat
Then in your main have
START /wait step1.bat
START /w sendEmail -f blaahg#gmail.com -t blahg#gmail.com .....
You may use a file as flag to indicate that a given Batch file is running. Create a flag file for each running Batch file and remove it before each Batch file ends:
echo X > 1_0.flg
START 1_0.bat
echo X > 1_0_1.flg
START 1_0_1.bat
echo X > 1_0_2.flg
START 1_0_2.bat
rem Wait for all previous Batch files to end
:wait
if exist *.flg goto wait
START /w sendEmail -f blaahg#gmail.com -t blahg#gmail.com .....
And remove the flag file in each one of the Batch files this way:
del "%~N0.flg"
... or directly writing the name of each Batch file in the DEL command.
You may also insert a ping -n 2 localhost > NUL command in the :wait cycle in order to not spend too much CPU in the wait cycle. Increment the 2 value if you wish.

Redirect DOS Output Only if there is Output

I have a series of lines in a batch file (.bat) running on a Windows machine, e.g.:
start /b prog.exe cmdparam1 cmdparam2 > test1.txt
start /b prog.exe cmdparam1 cmdparam2 > test2.txt
Sometimes proj.exe returns nothing (empty) instead of useful data. In those cases I want to not generate a text file, is this something easily doable on the batch file side of things? The current behavior is that a text file is always created, in the case of empty output it's just a blank file.
The jpe solution requires your parent batch to know when the started processes have completed before it can check the output file sizes. You can use the START /WAIT option, but then you lose the advantage of running in parallel.
You can use the fact that redirection to a file will fail if another process has already redirected output to that same file. When your parent batch can redirect to them successfully then you know the started processes have all completed.
You probably should redirect stderr to your output file as well as stdout
#echo off
::start the processes and redirect the output to the ouptut files
start /b "" cmd /c prog.exe cmdparam1 cmdparam2 >test1.txt 2>&1
start /b "" cmd /c prog.exe cmdparam1 cmdparam2 >test2.txt 2>&1
::define the output files (must match the redirections above)
set files="test1.txt" "test2.txt"
:waitUntilFinished
:: Verify that this parent script can redirect an unused file handle to the
:: output file (append mode). Loop back if the test fails for any output file.
:: Use ping to introduce a delay so that the CPU is not inundated.
>nul 2>nul ping -n 2 ::1
for %%F in (%files%) do (
9>>%%F (
rem
)
) 2>nul || goto :waitUntilFinished
::Delete 0 length output files
for %%F in (%files%) do if %%~zF==0 del %%F
Just delete all files with zero length. Edit: to accommodate the fact that start without the /WAIT flag returns before waiting the prog.exe to terminate, you could create the following wrapper script progwrapper.bat for your prog.exe:
prog.exe "%1" "%2" > "%3"
if %~z3==0 del "%3"
Then call the wrapper from your master script:
start /b progwrapper.bat cmdparam1 cmdparam2 > test1.txt
start /b progwrapper.bat cmdparam1 cmdparam2 > test2.txt
etc.
If prog.exe is a GUI app, then you should have a start /B /WAIT prog.exe in the progwrapper.bat.

dos batch script using psexec shows 'help' text every time it loops

Currently working on a script to ping every host on a /24 subnet, and then executes another script which runs psexec on those machines which are online. The ping sweep script is called ping.bat and the other script which actually runs psexec on the machines is called deploy_mir.bat. I can simply run deploy_mir.bat on a remote host and it will run no problem.
The problem im having is that every time mir.bat runs, which itself contains a loop, it will display the help info for psexec in the cmd window. As far as i can tell everything is working fine, aside from the annoying fact that everytime the loop inside of mir.bat runs my cmd window gets filled with the help info for psexec. I dont have #echo enabled, not that it would cause this anyway.
hoping for a quick fix, but if my code is needed to get an answer ill post it.
Posting the code anyway...
#echo on
setlocal EnableDelayedExpansion
set /p ipAddress="enter ip address: "
for /l %%i in (1,1,255) do (
ping -n 1 %ipAddress%.%%i | find "TTL" > nul
if !errorlevel! == 0 (
call deploy_mir.bat %ipAddress%.%%i
)
)
endlocal
deploy_mir.bat code
#ECHO OFF
echo "Mir Agent deployment to: %1"
rem net use T: \\%1\C$ /user:administrator "password"
net use T: \\%1\C$ /user:administrator "username"
copy /y conf.xml T:\WINDOWS\
copy /y setup_mir.bat T:\WINDOWS\
net use t: /delete
rem psexec \\%1 -i -u administrator -p "password" c:\windows\setup_mir.bat
psexec \\%1 -i -u administrator -p "username" c:\windows\setup_mir.bat
Desired cmd line result of running deploy_mir.bat
C:\DOCUME~1\socuser2\MIR>deploy_mir.bat 10.180.145.66
"Mir Agent deployment to: 10.180.145.66"
The command completed successfully.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
t: was deleted successfully.
PsExec v1.94 - Execute processes remotely
Copyright (C) 2001-2008 Mark Russinovich
Sysinternals - www.sysinternals.com
c:\windows\setup_mir.bat exited on 10.180.145.66 with error code 0.
C:\DOCUME~1\socuser2\MIR>
Just a suggestion. Not sure if it will solve your problem, but may provide some guidance:
My first step would be a small test by explicitly calling psexec on some test batch file in place of the line call deploy_mir.bat %ipAddress%.%%i. If no help message appears, since deploy_mir.bat works find on its own, try explicitly placing it's content in place of the same line call deploy_mir.bat %ipAddress%.%%i. If that works, then there is some issue in the said line we've been replacing. I believe dos / batch will open a sub shell from this line of code and run it's code in that scope. That may be causing the problem. Just guessing with the information provided.
Code Specific Notes:
#echo is enabled, but you say it is not in your question.
!errorlevel! == 0 should be !errorlevel! EQU 0
Some General Notes:
In general, I used to pass parameters to batch scripts in quotes, then strip the quotes with %~1 once inside the batch script. Similarly for if conditions, as !someVar! == a will throw and error if someVar is not set / empty, while "!someVar!" == "a" will gracefully not meet the criteria of the if condition.
I don't know why it works when called from outside the loop. But the psexec line in deploy_mir.bat should have cmd /c.
psexec \\%1 -i -u administrator -p "username" cmd /c c:\windows\setup_mir.bat

Resources