Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture the ftp error code in batch scripts?

Tags:

batch-file

ftp

I have a somewhat related, but different questions here.

I have a batch script (*.bat file) such as this:

@ftp -i -s:"%~f0"&GOTO:EOF
open ftp.myhost.com
myuser
mypassword
!:--- FTP commands below here ---
lcd "C:\myfolder"
cd  /testdir
binary
put "myfile.zip"
disconnect
bye

Basically this is a script that uploads a zip file to a ftp site. My question is that, the upload operation can fail from time to time ( the remote ftp is not available, "myfile.zip" is non-existent, upload operation interrupted and whatnot), and when such unfortunate things happen, I want my bat file return 1 ( exit 1).

It would be great if my upload wasn't successful, the ftp would throw an exception ( yes, like exception in C++), and I would have a catch-all exception that catches it and then exit 1, but I don' think this is available in batch script.

What is the best way to do what I need here?

like image 289
Graviton Avatar asked Aug 23 '11 08:08

Graviton


People also ask

How do I run an FTP script?

You can script the ftp command with the -s:filename option. The syntax is just a list of commands to pass to the ftp shell, each terminated by a newline. This page has a nice reference to the commands that can be performed with ftp .

How do I run an FTP script in Windows?

To run the script, you just execute the FTP command with the -s option. For example, ftp -s:ftpscript. txt .

What is batch script command?

In Windows, the batch file is a file that stores commands in a serial order. The command line interpreter takes the file as an input and executes in the same order. A batch file is simply a text file saved with the .bat file extension. It can be written using Notepad or any other text editor.


2 Answers

You can redirect the output to a log file and when the ftp session is finished the file can be parsed.

@ftp -i -s:"%~f0" > log.txt & GOTO :parse
open ftp.myhost.com
myuser
mypassword
!:--- FTP commands below here ---
lcd "C:\myfolder"
cd  /testdir
binary
put "myfile.zip"
disconnect
bye

:parse
for /F "delims=" %%L in (log.txt) Do (
  ... parse each line
)
like image 62
jeb Avatar answered Oct 26 '22 15:10

jeb


Windows FTP does not return any codes.

I suggest running a batch file that echo your ftp commands to a input response file, then use this file as input to the ftp command, redirecting stderr to a file and verifying the file size. something like this

echo open ftp.myhost.com >ftpscript.txt
echo myuser >>ftpscript.txt
echo mypassword >>ftpscript.txt
echo lcd "C:\myfolder"  >>ftpscript.txt
echo cd  /testdir  >>ftpscript.txt
echo binary  >>ftpscript.txt
echo put "myfile.zip"  >>ftpscript.txt
echo disconnect  >>ftpscript.txt
echo bye  >>ftpscript.txt

ftp -i -s:ftpscript.txt >ftpstdout.txt 2>ftpstderr.txt 
rem check the ftp error file size, if 0 bytes in length then there was no erros
forfiles /p . /m ftpstderr.txt /c "cmd /c if @fsize EQU 0 del /q ftpstderr.txt"
if EXIST ftpstderr.txt (
   exit 1
)
like image 40
ReCycliste Avatar answered Oct 26 '22 16:10

ReCycliste