1

I am trying to upload a file from Windows server to third party server (I do not know OS etc. of this) through windows batch script. I am using PSFTP to upload files. It was working since a long time but since yesterday while uploading files, I am getting 'Network error: connection timed out' and batch script file control is doing further steps after file uploading step.

My requirement is whenever there is a failure to upload a file through psftp command through batch script, system should not proceed further. It should stop executing further steps.

Please let me know how to do this in Windows batch scripting?

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
saikiran
  • 71
  • 2
  • 8

1 Answers1

2

psftp returns exit code 1 on error. So you just check the exit code and act accordingly.

To records errors, just redirect all psftp.exe output to a file.

psftp.exe -b script.txt > script.log 2>&1

if errorlevel 1 (
    echo Error
) else (
    echo Success
    rem Other commands to perform on success
)
Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
  • Thanks This helps! Is it possible to capture error message? Eg: Network timedout or file is corrupted etc. Just to know why command is failed and find a solution for it – saikiran May 09 '17 at 15:45
  • You are welcome. Though on Stack Overflow we [thank by accepting and/or upvoting the answer](http://stackoverflow.com/help/someone-answers). – Martin Prikryl May 10 '17 at 05:47
  • What do you want to do with the captured error message? – Martin Prikryl May 10 '17 at 05:47
  • I would like to know the root cause of the error. So that I can take necessary steps to avoid such occurance – saikiran May 30 '17 at 16:26
  • Sorry, but that's vague. So do you want to log the error to a file, or what? – Martin Prikryl May 31 '17 at 06:01
  • Yes. Error messages should be pushed to a log file. So that next day when I come, I can check the log file and see error messages. My idea is to know the reason for failure of each step in the script – saikiran May 31 '17 at 09:57