FTP Batch file with error handling

mackeyrj
mackeyrj used Ask the Experts™
on
I have a batch file that uploads all PDFs in the folder containing the batch file and then deletes the PDFs.
However even on upload or connection failures the files are deleted.
I need help to only delete files that uploaded successfully.

Windows XP SP3
Here is the code:

@echo off
echo                                           DO NOT CLOSE
echo userusernamej> ftpcmd.dat
echo password>> ftpcmd.dat
echo bin >> ftpcmd.dat
echo prompt off>> ftpcmd.dat
echo cd data/index >> ftpcmd.dat
echo mput *.pdf >> ftpcmd.dat
echo quit>>ftpcmd.dat
ftp -n -s:ftpcmd.dat ftphost
del *.pdf
del ftpcmd.dat
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi, I haven't tried this, but I think it should work.

@echo off

echo                                           DO NOT CLOSE
echo userusernamej> ftpcmd.dat
echo password>> ftpcmd.dat
echo bin >> ftpcmd.dat
echo prompt off>> ftpcmd.dat
echo cd data/index >> ftpcmd.dat
echo mput *.pdf >> ftpcmd.dat
echo quit>>ftpcmd.dat
ftp -n -s:ftpcmd.dat ftphost && goto OK

goto END

:OK
del *.pdf
del ftpcmd.dat

:END
@echo off
echo                                           DO NOT CLOSE
echo userusernamej> ftpcmd.dat
echo password>> ftpcmd.dat
echo bin >> ftpcmd.dat
echo prompt off>> ftpcmd.dat
echo cd data/index >> ftpcmd.dat
echo mput *.pdf >> ftpcmd.dat

echo mls  *.pdf  movedfiles.txt

echo quit>>ftpcmd.dat

ftp -n -s:ftpcmd.dat ftphost

if exist movedfiles.txt for /f %%F in (movedfiles.txt) do del %%F

del ftpcmd.dat
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

check ERRORLEVEL after the ftp command. If >1 don't delete files.

ftp -n -s:ftpcmd.dat ftphost
if not ERRORLEVEL 1 del *.pdf
if not ERRORLEVEL 1 del ftpcmd.dat
Not sure, but I think the errorlevel could be zero even if the file transfer fails.  That is why I list the files that successfully transfered to a local file called movedfiles.txt.  Here is an enhancement:

if exist movedfiles.txt for /f %%F in (movedfiles.txt) do IF EXIST %%F del/q %%F
del/q movedfiles.txt

Author

Commented:
Tried this already. It uploads the files ok but does not delete the files on success.
which suggestion did you try?

Author

Commented:
Sorry.. When I was replying there was only one response (DrewWatson).
Trying knightEknight now.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Another option is to use the free NCFTP get and put utilities, they are much easier to script and have good error control, as well as only deleting files if they transfer correctly built right in.

http://www.ncftp.com/ncftp/

~bp
I've always thought FTP needed a MOVE command.  :)
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Yes, with the -DD option of ncftpget and ncftpput we essentially have a "move" command.

~bp

Author

Commented:
I don't want to use other 3rd party tools as this means that my end users will need to get them too.

This is meant to be as simply as possible for my end users.
The batch file connects to the FTP host, uploads files , deletes the local copy and closes.

I just want it to not delete any file that does not upload.

I tried
if not ERRORLEVEL 1 del *.pdf
if not ERRORLEVEL 1 del ftpcmd.dat
I put in incorrect password and it still deleted the file after it ailed to connect.
did the "mls" suggestion above not work for you?
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
I believe it's a known bug of the built in FTP program that it doesn't accurately use ERRORLEVEL, so it can report 0 even if errors occur.  As a result you will have to capture the output of the FTP session (log) and then examine that for certain triggers that will tell you if it succeeded or not.  Here's one example of that, but you will need to decide what your trigger strings are in the log file.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_21266734.html

~bp
I believe mls should tell you what files were successfully transferred ...

Author

Commented:
The "mls" will upload the files but does not move or delete the pdf afterward.
no, but per the script above, it will provide a list of the files that moved, and you can then parse that list and delete the files that moved.

Author

Commented:
knightEknight,

First I discovered that your original response had
echo mls  *.pdf  movedfiles.txt which was the main reason I did not respond since it did nothing.
I added the >> and changed it to
echo mls  *.pdf >>movedfiles.txt which did make the txt file.
However all I get in there is mls *.pdf. even thought there is a "test.pdf" that is uploaded.

I also added your other line
if exist movedfiles.txt for /f %%F in (movedfiles.txt) do IF EXIST %%F del/q %%F
del/q movedfiles.txt

But all that does is delete the txt file.
hmm, well if the movedfiles.txt does not contain the correct data then I would not expect the next line to work either...

When I use mls, I don't need to redirect.  For me, this outputs to a localfile:

   mls  *.pdf  movedfiles.txt

While in FTP, type "help mls"  ... maybe the syntax is different for your version of FTP.

Author

Commented:
help mls gives me List contents of multiple remote directories.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016
Commented:
==> kEk

I've only been half following, so if I'm off base bear with me.  But wouldn't your MLS command need to be piped into the command file being built?  So instead of:

  echo mls  *.pdf  movedfiles.txt

should it be:

  echo mls  *.pdf  movedfiles.txt>>ftpcmd.dat

~bp
You're spot on Bill, thanks...

Mack, note the difference -- the command it piped to the FTP file, not to the movedfiles.txt file.  Try that and see if it works for you.
*it piped --> *is piped

Author

Commented:
Thanks guys
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Not needed, but thanks for the assist, glad I was able to lend a small hand.

~bp
Thanks Mack!  I'm glad it works for you...

... and thanks again Bill!  I got busy and wasn't looking at this one anymore today.  Good catch.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial