How to run a set of parallel batch files sequentially - windows

I am trying to write a test bat file that will do the following:
Download a file from a server
Upload a file to the same server
I need three laptops to perform download and upload simultaneously. therefore, I create such a script named 3-chanD.bat:
*echo /////////3-channel-Downloading////////
start cmd.exe /k >time2 timeit chan6D.bat
start cmd.exe /k >time3 timeit chan11D.bat
start cmd.exe /k >time1 timeit chan1D.bat*
chan6D.bat is basely a script coping file from a server to one of my laptops:
*xcopy "C:\Documents and Settings\All Users\Documents\test" "\\192.168.0.102\SharedDocs\test" /Y /E /S*
same as chan1D.bat and chan11D.bat:
*echo /////////channel 6 Downloading////////
xcopy "C:\Documents and Settings\All Users\Documents\test" "\\192.168.0.102\SharedDocs\test" /Y /E /S*
*echo /////////channel 11 Downloading////////
xcopy "C:\Documents and Settings\All Users\Documents\test" "\\192.168.0.104\SharedDocs\test" /Y /E /S*
The upload script does the opposite job.
The problem is I need to sit beside laptops to wait those three laptops complete downloading, then manually run the upload script. I try to write a script:
*#echo off
echo /////////3-channel-Downloading////////
call 3-chanD.bat
echo /////////3-channel-Uploading////////
call 3-chanU.bat
pause*
but it just run the 3-chanD.bat and 3-chanU.bat in a second. It doesn't wait till download is finished.
Is there a way to execute a set of commands in parallel, then execute another set of commands in parallel?

Have each batch file create a flag file, perform the xcopy, and delete the flag file when the xcopy has finished.
type nul > "%temp%\xcopy1.tmp"
xcopy "C:\Documents and Settings\All Users\Documents\test" "\\192.168.0.102\SharedDocs\test" /Y /E /S
del "%temp%\xcopy1.tmp"
Your launching batch file can check for the existence of all three flag files in a loop, and launch the upload processes when they no longer exist.

Related

Program started from batch file starts in background

I have a batch file that I run from a flash drive. This file attempts to
1. Close 2 programs
2. Update data on the computer based on the data on the flash drive
3. Restart the programs
The code in this batch is:
taskkill /IM "MyProgram".exe
taskkill /f /fi "imagename eq MyProgram.exe"
copy e:\File1.xml C:\Folder\SubFolder1\Themes\Data\File1.xml /Y
copy e:\File2.xml C:\Folder\SubFolder1\Themes\Data\File2.xml /Y
copy e:\File3.xml C:\Folder\SubFolder2\Themes\Data\File3.xml /Y
copy e:\File4.xml C:\Folder\SubFolder2\Themes\Data\File4.xml /Y
start /MAX "" "C:\Folder\SubFolder2\MyProgram.exe"
start /MAX "" "C:\Folder\SubFolder1\MyProgram.exe"
The first two steps work fine, with the commands that are on lines 1-6. My issue starts with step 3 and the commands on lines 7 and 8.
Is there something wrong with the batch commands?
EDIT: After digging a little more, I have found that the programs appear to be starting in the background. When looking at task manager, the programs appear in the "Background Processes" section instead of showing in the foreground as expected.
What I believe is happening is your program does not know where the configuration files are located because the working directory is where the batch file started itself. So your program is looking for its files on your usb drive. By using the /D option with the START command it will switch the working directory to whatever path you set it to.
start "" /MAX /D "C:\Folder\SubFolder2\" MyProgram.exe

CMD doesn't close after I run a batch file and files don't copy after deleting them once?

I made a batch file to copy my Witcher 3 save files and settings to another drive. It runs every time I start Windows and it runs again after 12 hours.
Problems are that it opens CMD and doesn't close it. I wan't it to close automatically, it doesn't matter if it shows deleting and copying the files I would actually prefer it showing those, just close automatically after those and don't show the seconds its counting for 12 hours.
Also after deleting the files from the backup location, it doesn't copy the files there anymore until I restart my PC.
I made the batch file to delete the files because it would just copy the new files and leave the old ones in and it would be a mess, so I'm not sure if I could do this only with the copy command and without the del command?
The batch file looks like this:
:loop
del /s /q "F:\Backups\The Witcher 3 save files"
xcopy /m /e /y "C:\Users\Kristian\Documents\The Witcher 3" "F:\Backups\The Witcher 3 save files"
timeout /t 43200
goto loop
If you want a batch file to run every 12 hours, you should use the Windows Task Scheduler. Then your script doesn't have to run 24/7.

Windows 7: Scheduler with xcopy don't copy new files

I've created a batch file to copy files from my machine to a backup machine. The batch file is this:
#echo off
cd D:\Backup
echo %date% %time% - Starting backup process. >> D:\Backup\backup_log.txt
xcopy D:\T\Source I:\AREA\USER\Backup\T\Source /s/d/y/c/v/r/i/e >> D:\Backup\backup_log.txt
echo %date% %time% - Stopping backup process. >> D:\Backup\backup_log.txt
exit
When running this bat file as command line, it copies all the new files and folders from source folder to a destination folder. Makes an update of the Source folder in I:\AREA\USER\Backup\T\Source. This is what I want.
D:\Backup>Backup.bat
But when I configure the same bat execution using Windows Task Scheduler, the scheduler runs without any errors but doesn't copy/update the new files.
I'm using Windows 7. I've made some tests and with this scenario, I can't see where the problem is. Maybe some configuration on Task Scheduler. I've read the information of the xcopy parameters. But with these parameters work in command line, why it doesn't work using windows scheduler?
Any help?
Thanks!
Filipe
Edit1: I've found something that can help to parse the error. If, in the Task Scheduler, I select the option Run only when user is logged on, it works. But when I select the other option Run whether user is logged on or not, it doesn't work.
Well... I resolve my problem with this.
The bat file is:
#echo off
cd D:\Backup
echo %date% %time% - Starting backup process. >> D:\Backup\backup_log.txt
xcopy "D:\T\Source" "I:\AREA\USER\Backup\T\Source" /D /E /C /R /H /I /K /Y /V >> D:\Backup\backup_log.txt
echo %date% %time% - Stopping backup process. >> D:\Backup\backup_log.txt
exit
In the Task Scheduler, I've changed the General tab and check "Run only when user is logged on".
I don't know, but in my understanding, the "Run whether user is logged on or not" should work when the user is logged.
I had a similar issue running bat file with XCOPY via Task Scheduler with "Run whether user is logged on or not" set. The ussue was that I have a network location \\domain mapped as disc D: thus my cmd was like
XCOPY /d /y \\192.168.1.1\Data D:\Data
It was running fine just by executing bat file, but task runner didn't copy anything. I've changed it to
XCOPY /d /y \\192.168.1.1\Data \\domain\Data
And everything was fine since then.

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.

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