Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 that done.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.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
I 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. –  Mark Allen Aug 24 '12 at 19:44
    
@Mark Allen - Could you elaborate? Don't quite follow what you mean. –  PeanutsMonkey Aug 24 '12 at 20:05

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.
share|improve this answer
    
Thanks. 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 Aug 26 '12 at 19:05
    
Secondly, why ping if it accomplishes nothing? –  PeanutsMonkey Aug 26 '12 at 19:18
    
Sorry, 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 just eof? What difference would there be? –  PeanutsMonkey Aug 26 '12 at 19:49
    
@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. :) –  Mark Allen Aug 27 '12 at 17:59
    
Sorry, I don't quite follow why you would want to cause a delay. –  PeanutsMonkey Aug 27 '12 at 18:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.