Run a program in a batch script and wait for it to finish before continuing
Solution 1
use start /wait winscp ...
to start the file transfer, this will pause the batch script until winscp exits, you can add the move commands after that line or call done.bat
start /wait winscp ...
call done.bat
You should combine this with the errorchecking suggested by Mark Allen
Personally I'd use a command line SFTP client (like the one supplied with Putty or OpenSSH) which I think would avoid this sort of issue.
Solution 2
Modify start.bat to detect failure on the part of winscp.exe. Only call done.bat if winscp.exe exits with an exit code of 0.
Try something like this:
winscp.exe blah blah blah
if not errorlevel 0 goto end
call done.bat
:end
Note that I'm only assuming winscp.exe exits with 1 or greater upon error. Check the documentation for winscp.exe.
Editing to include the following:
Let's say winscp.exe doesn't use exit codes to denote success or failure. You might need to do something different.
winscp.exe /log=%temp%\wscpresults.log *other parameters here*
ping -n 300 -w 1000 127.0.0.1 > nul
find /i "success" %temp%\wscpresults.log
if errorlevel 0 goto end
call done.bat
:end
Where:
- The ping command accomplishes nothing but to wait for 5 minutes (300 x 1000ms or 1 second = 5 minutes)
- The /log switch causes winscp.exe to write the results of the operation to a file.
- The find command looks for the word success - I have no idea what winscp actually writes to the file, you'd have to try it once and identify the string that shows everything worked, and then modify the string that find searches for to that.
- If the find command doesn't find the word "success", go to the 'end' label without calling done.bat. Otherwise, we think everything succeeded and we call done.bat.
Related videos on Youtube
PeanutsMonkey
Updated on September 18, 2022Comments
-
PeanutsMonkey almost 2 years
I have a very specific requirement. I am attempting to upload files via sftp which uses private keys. I already have a bat file that connects to the host and uploads the file however I need to move the uploaded files once completed to another path.
The first bat file is called
start.bat
. This connects to the sftp server and uploads the file. I am using winscp to connect to the host.The second bat file is called
done.bat
. This moves files from one directory to another.I am calling the second bat file from the first using
call done.bat
. The issue I have is thatdone.bat
is completed even before the first batch file has had the chance to authenticate, login and upload.I want to only move the file once the upload has completed. If the upload fails for some reason, the second file doesn't execute.
The bat files are on a Windows XP machine.
-
Mark Allen almost 12 yearsI was assuming winscp.exe being called from a batch file wouldn't return until it was done in the first place. If that's not the case, then you instead have to do other things like see whether or not you can get winscp.exe to log to a file, use the ping 127.0.0.1 pause trick to give it 5 minutes or whatever, then look at the log to check for success/failure, etc.
-
PeanutsMonkey almost 12 years@Mark Allen - Could you elaborate? Don't quite follow what you mean.
-
PeanutsMonkey almost 12 yearsThanks. Had a read of the use of errorlevel as I have never used it but based on what you are suggesting and if I have interpreted it correctly, you are saying if the errorlevel is not 0 go to the label end. If this is correct how does
call done.bat
get executed? -
PeanutsMonkey almost 12 yearsSecondly, why ping if it accomplishes nothing?
-
PeanutsMonkey almost 12 yearsSorry, read your code again. You are expecting the error code to return greater than 0 if there is an issue and if it is not 0 then do not execute done.bat and go to
end
. Why not justeof
? What difference would there be? -
Mark Allen almost 12 years@PenutsMonkey The ping is to cause a delay. It's an easy to say "Wait for 5 minutes" in a batch file, that's all. I didn't use eof out of habit, I don't remember why anymore, I think I got burned once from using it but it was probably some old version of DOS (get off my lawn!) and works perfectly now. :)
-
PeanutsMonkey almost 12 yearsSorry, I don't quite follow why you would want to cause a delay.
-
Mark Allen almost 12 years@PeanutsMonkey The idea is that if WinSCP returns right away instead of after it's done, you'd need to give it a few minutes to make sure that it had actually finished. If you don't need to do that, then don't include the ping command.
-
Martin Prikryl about 7 yearsI do not understand a point of this answer. Actually even of the question. When you execute a program from a batch file, the batch file waits for it to complete. No need for
start /wait
. See also Why GUI application blocks a batch file?. -
Ian Yates over 4 yearsYou can use winscp.com rather than winscp.exe to have WinSCP wait in a batch file as you'd expect...