FTP Batch file with error handling

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
mackeyrjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DrewWatsonCommented:
tibidoudouCommented:
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
knightEknightCommented:
@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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Justin MathewsCommented:
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
knightEknightCommented:
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

mackeyrjAuthor Commented:
Tried this already. It uploads the files ok but does not delete the files on success.
knightEknightCommented:
which suggestion did you try?
mackeyrjAuthor Commented:
Sorry.. When I was replying there was only one response (DrewWatson).
Trying knightEknight now.
Bill PrewIT / Software Engineering ConsultantCommented:
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
knightEknightCommented:
I've always thought FTP needed a MOVE command.  :)
Bill PrewIT / Software Engineering ConsultantCommented:
Yes, with the -DD option of ncftpget and ncftpput we essentially have a "move" command.

~bp
mackeyrjAuthor 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.
knightEknightCommented:
did the "mls" suggestion above not work for you?
Bill PrewIT / Software Engineering ConsultantCommented:
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
knightEknightCommented:
I believe mls should tell you what files were successfully transferred ...
mackeyrjAuthor Commented:
The "mls" will upload the files but does not move or delete the pdf afterward.
knightEknightCommented:
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.
mackeyrjAuthor 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.
knightEknightCommented:
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.
mackeyrjAuthor Commented:
help mls gives me List contents of multiple remote directories.
Bill PrewIT / Software Engineering ConsultantCommented:
==> 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
knightEknightCommented:
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.
knightEknightCommented:
*it piped --> *is piped
mackeyrjAuthor Commented:
Thanks guys
Bill PrewIT / Software Engineering ConsultantCommented:
Not needed, but thanks for the assist, glad I was able to lend a small hand.

~bp
knightEknightCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.