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.
-
1Why can't you create a new batch file with all the commands in batch1 then followed by all commands in batch2? Or a separate batch file to call batch1 then batch2. – Angus Comber Sep 05 '15 at 10:12
2 Answers
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

- 40,353
- 10
- 53
- 68
-
-
replace `start c:\BAT\fraps.exe` with `pause` and test it. It it also doesn't pause when you expect it to, then the problem is that there are no files being copied. – foxidrive Sep 05 '15 at 12:39
-
It does copy the files new content, but doesn't pause or start the exe file – Mister White Sep 05 '15 at 12:48
-
It it doesn't pause or start the exe, and robocopy is actually running and you see the screen output from robocopy running, then robocopy is not setting the errorlevel. That is either due to the version of robocopy you are using being outdated or simply because the files really are not being copied. If you place `pause` on the line directly after the robocopy command then you will see the robocopy summary on the screen and it will show you when files are copied and how many. Triple check it. – foxidrive Sep 05 '15 at 13:15
-
I created all new folders, bat files, and all that suff, but it still only copies the files without launching the .exe. And one more thing Launching separate batch files, (one runs exe, other does the copy ) everything goes well. Maybe the syntax for calling .exe IF the copying was performed is wrong? – Mister White Sep 05 '15 at 14:01
-
-
-
-
-
I can't see what you are doing and there is a flaw in your methodology. – foxidrive Sep 05 '15 at 14:30
-
I did, and it works. I added the test code to my answer for you to try. – foxidrive Sep 05 '15 at 14:37
-
should it work if i copy paste all of it without changing anything? – Mister White Sep 05 '15 at 14:39
-
For so freaking reason it does work :D, even without all those echo, md test and del. EDIT: FOUND A BUG. When the two folders and a bat file are all on desktop it works. when two folders are in C: and bat file on desktop, exe file doesnt launch :D – Mister White Sep 05 '15 at 14:50
-
So there is some issues with directories, when files and folders are not in the same place or some shit. – Mister White Sep 05 '15 at 15:07
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.
-
First of all. i need it to run like 24 hours a day, so when i did a loop (though maybe my loops is terrible) i had to unplug my pc cause there was too many windows popping up. Second of all it does copy the file, but does not start the exe file – Mister White Sep 05 '15 at 11:52
-
To run 24/7, you need to schedule the script. What type of loop you're running in script?. Check the error mentioned by _foxidrive_. May be there is a permission issue, run script as administrator. – Kas Sep 05 '15 at 13:40
-
Your script just doesn't work. it pops two windows, but doesn't prompt to execute the .exe – Mister White Sep 05 '15 at 13:52
-
One thing more, when you run script as administrator then you need to set full path as default is set to "C:\Windows\System32". The easiest way is this. `start %~dp0\Directory\program.exe` Have you read the line about "New Window?" See for details about "%~dp0" here http://stackoverflow.com/questions/5034076/what-does-dp0-mean-and-how-does-it-work – Kas Sep 05 '15 at 13:52