Windows batch script to output log - windows

I am writing a simple batch script to execute 2 programs. I would want to create a log file everytime this batch script is executed regardless it is success or failed.
#echo on
START "" /b /wait 7za.exe a C:\nexus.7z c:\nexus -pTESTING -mx7 >> c:\7zip.txt
START "" /b /wait xcopy.exe C:\nexus.7z \\ketsrv14\Software >> c:\copy.txt
The script above will create log for each program. Is there a way to combine the log and create only 1 log for both program?

This should work. I think 7-zip will wait on it's own.
I use the 7z.exe and don't have a 7za.exe in mine.
#echo on
7z.exe a C:\nexus.7z c:\nexus -pTESTING -mx7 >> c:\7zip.txt
xcopy.exe C:\nexus.7z \\ketsrv14\Software >> c:\7zip.txt

Related

How to get return/exit code of a batch file started with START command?

I am trying to run a batch file with start /high and still get the return/exit code, i.e. %ERRORLEVEL%. The problem seems to be that command START does not return the exit code that the batch file returns.
We have a simple batch file for testing named BatFileThatReturnsOne.bat.
The contents of BatFileThatReturnsOne.bat are
EXIT /B 1
We are calling this as such:
start /high /wait BatFileThatReturnsOne.bat
But no matter what the batch file returns, the execution of start never has a %ERRORLEVEL% of anything other than 0 (zero).
This is all actually called by cfn-init in CloudFormation, but that is probably not relevant because we can reproduce it from a command line window.
The actual call is:
cmd.exe /C start /high /wait BatFileThatReturnsOne.bat
How do I get start to set the %ERRORLEVEL% to something other than 0 (zero)?
directly from a cmd window or a batch file you can use
start /high /wait cmd /c BatFileThatReturnsOne.bat
but if you need to start the cmd instance to execute the start command that launchs the batch file then you can use
cmd /v /e /c" start /high /wait cmd /c launched.cmd & exit ^!errorlevel^!"
Just change EXIT /B 1 by EXIT 1.
As explained in the Table 4 given in this answer about START /WAIT bat command:
When the started Batch file ends, set ERRORLEVEL = value from 'EXIT number' commmand.

copy command in batch file is not getting executed when calling the batch file from another batch file, but is getting executed when i double click

i am trying to execute a command xcopy path1 path2 /Y /C and it is getting executed successfully when i tried from command line and also when i copied it to a batch file and double clicking on that batch file.
But it is not getting executed when i cal this batch file from another fail.
Could anyone please help here?
Not working case:
C:\abcd>cmd.exe /C "xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z"
0 File(s) copied
Working case:
C:\abcd>cmd.exe /C "xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z"
C:\abcd\itsme.bat
1 File(s) copied
Extra Info:
Runme.bat:
call C:\folder1\copy.bat
call C:\folder1\clean.bat
copy.bat:
#echo off
xcopy "C:\folder1\runrun.bat" "Z:\" /C /Y /Z /Q
xcopy "C:\folder1\runrun.bat" "Y:\" /C /Y /Z /Q
xcopy "C:\folder1\runrun.bat" "X:\" /C /Y /Z /Q
Here, If I double click on Runme.bat, copy.bat is getting executed and copying all the files.
1 File(s) copied
1 File(s) copied
1 File(s) copied
But issue is, it is not copying anything when i try to run the same batch file from windows scheduler.
Output:
0 File(s) copied
0 File(s) copied
0 File(s) copied
looks like issue is only with copy command in the second batch file, which will return output. But all the commands in the other batch file clean.bat (which i am calling from the first batch file) are getting executed without any issues.
second batch file has simple echo commands, so that is why it is working fine.
Use xcopy with 'START' command.
For example, c:>> START xcopy "C:\folder1\itsme.bat" Y:\ /C /Y /Z
As Mofi pointed out, you can remove 'CMD'
The command cmd is for running a new instance of the command line interpreter and need to be used usually only for opening a command prompt window. Execute in a command prompt window cmd /? to get help about this command.
The command cmd does not need to be used usually if a command prompt window is opened already and a command is entered. A batch file is interpreted/processed by cmd and therefore it usually does not make sense to use cmd in a batch file.
So use only xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z in already opened command prompt window and in the batch file.
To process another batch file like batch file 2 from within a batch file like batch file 1 and continue processing of batch file 1 after processing of batch file 2 finished, use in batch file 1 the command call for calling batch file 2 like a subroutine.
Example for batch file 1:
#echo off
echo This is batch 1 calling now batch 2 and is waiting until it finished.
call "batch file 2.bat"
echo Batch 1 continues.
Example for batch file 2:
echo This is batch 2 running XCOPY.
xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z
echo XCOPY finished, batch 2 terminates.
Run batch file 1 and you get the output:
This is batch 1 calling now batch 2 and is waiting until it finished.
This is batch 2 running XCOPY.
XCOPY finished, batch 2 terminates.
Batch 1 continues.
Remove command call in batch file 1, execute it again and look what you get now.
This time without call in batch file 1 the processing of batch file 1 continues on batch file 2 without coming back to batch file 1 on reaching end of batch file 2 processing.
From your extra information it may be that x: y: z: are network drives and task scheduler doesn't have network privileges under the system account.
Also, change the name of copy.bat because copy is a system command and using system command names will bite you one day, if not today.

Sikuli multiple scripts batch

I'm trying to create a batch in Windows 7 that will open at least two Sikuli scripts in sequence. I've tried using the solutions found here and couldn't get them to work. This is the batch command I've used:
cmd /C C:\path\Sikuli\runIDE.cmd -r C:\path\Sikuli\all.sikuli
cmd /C C:\path\Sikuli\runIDE.cmd -r C:\path\Sikuli\sikuli_test.sikuli
I've also tried:
start /i /b /wait C:\path\Sikuli\runIDE.cmd -r :\path\Sikuli\all.sikuli
start /i /b /:\path\Sikuli\runIDE.cmd -r :\path\Sikuli\sikuli_test.sikuli
The first Sikuli script executes but the second one does not. The problem seems to be within Sikuli IDE opening in cmd, which once it initializes doesn't allow any more commands in the batch to execute as Sikuli's monitoring process takes over the cmd prompt.
start /wait will wait for the executable to exit before continuing. Remove the /wait switch and batch will proceed to your second command.
have you tried
cd C:\SikuliX && runScript.cmd -r C:\path\Sikuli\all.sikuli
cd C:\SikuliX && runScript.cmd -r C:\path\Sikuli\sikuli_test.sikuli
What I did is I have two batch files to call the sikuli files ("Dummy1.sikuli" and "Dummy2.sikuli").
And then I have a 3rd batch file that will call all other batch files.
Sikuli opens by using the command window, and this way both .sikuli files have a command window.
My examples are all located in:
C:\Dummy
Files located here:
Dummy1.sikuli
Dummy2.sikuli
Dummy1.bat
Dummy2.bat
RunDummies.bat
Dummy1.bat
#ECHO OFF
REM Run Dummy1.sikuli
C:\Sikuli\runIDE.cmd -r C:\Dummy\Dummy1.sikuli
Dummy2.bat
#ECHO OFF
REM Run Dummy2.sikuli
C:\Sikuli\runIDE.cmd -r C:\Dummy\Dummy2.sikuli
RunDummies.bat
#ECHO OFF
start cmd.exe /C Dummy1.bat
start cmd.exe /C Dummy2.bat

Hide CMD windows in .bat file? (echo off method not working)

A command prompt still show in this code?
I saw somewhere about >nul 2>nul but i don't know where to put it?
#echo off
rem script1
cd C:\Users\Blah\Downloads\Script\
start script1
rem script2
cd C:\Program Files (x86)\script\
start /W script3.exe
rem somthing
cd C:\Program Files (x86)\script\
start /W script4.exe
exit
1 .Save your code in a batch file lets say My.bat as below:
My.bat:
#echo off
rem script1
cd C:\Users\Blah\Downloads\Script\
start script1
rem script2
cd C:\Program Files (x86)\script\
start /W script3.exe
rem somthing
cd C:\Program Files (x86)\script\
start /W script4.exe
exit
2. Create a VBScript file lets say Master.vbs and call your My.bat file within it.
Lets assume your batch file is at C:\Test\My.bat then:
Master.vbs:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Test\My.bat" & Chr(34), 0
Set WshShell = Nothing
It'll run your batch file in invisible/hidden mode.
I suppose the output you're seeing is from the scripts that you're calling.
Try the following (i.e.):
cd C:\Program Files (x86)\script\
start /W script3.exe > nul 2> nul
The >-symbol redirects the output to a file, if that file is nul it is sent to the so called "null-device", meaning it is just dropped. the 2> also redirects any error output. You should be careful about the second, because it might cause you not to see real errors that happen in your script.

Running vbscript from batch file

I just need to write a simple batch file just to run a vbscript. Both the vbscript and the batch file are in the same folder and is in the SysWOW64 directory as the vbscript can only be execute in that directory. Currently my batch file is as follows:
#echo off
%WINDIR%\SysWOW64\cmd.exe
cscript necdaily.vbs
But the vbscript wasn't executed and just the command prompt is open. Can anyone tell me how can i execute the vbscript when i run this batch file?
You can use %~dp0 to get the path of the currently running batch file.
Edited to change directory to the VBS location before running
If you want the VBS to synchronously run in the same window, then
#echo off
pushd %~dp0
cscript necdaily.vbs
If you want the VBS to synchronously run in a new window, then
#echo off
pushd %~dp0
start /wait "" cmd /c cscript necdaily.vbs
If you want the VBS to asynchronously run in the same window, then
#echo off
pushd %~dp0
start /b "" cscript necdaily.vbs
If you want the VBS to asynchronously run in a new window, then
#echo off
pushd %~dp0
start "" cmd /c cscript necdaily.vbs
This is the command for the batch file and it can run the vbscript.
C:\Windows\SysWOW64\cmd.exe /c cscript C:\Windows\SysWOW64\...\necdaily.vbs
Just try this code:
start "" "C:\Users\DiPesh\Desktop\vbscript\welcome.vbs"
and save as .bat, it works for me
Batch files are processed row by row and terminate whenever you call an executable directly.
- To make the batch file wait for the process to terminate and continue, put call in front of it.
- To make the batch file continue without waiting, put start "" in front of it.
I recommend using this single line script to accomplish your goal:
#call cscript "%~dp0necdaily.vbs"
(because this is a single line, you can use # instead of #echo off)
If you believe your script can only be called from the SysWOW64 versions of cmd.exe, you might try:
#%WINDIR%\SysWOW64\cmd.exe /c call cscript "%~dp0necdaily.vbs"
If you need the window to remain, you can replace /c with /k
Well i am trying to open a .vbs within a batch file without having to click open but the answer to this question is ...
SET APPDATA=%CD%
start (your file here without the brackets with a .vbs if it is a vbd file)
You should put your .bat file in the same folder as your .vbs file and call the following code inside the .bat file.
start cscript C:\filePath\File.vbs

Resources