Using Batch File to Move a File

I have a program that ftps a file from one server to another. If this fails it gives an error code of 1. I have written the folllowing batch to handle this so that if it is successful it moves the file to the done folder. If not, it appends text to an error log and leaves the file where it is. The code is:

IF ERRORLEVEL 1 echo ErrorSendingFile %DATE:~10,4%%DATE:~7,2%%DATE:~4,2% >> d:\orders\orders.txt ELSE MOVE d:\orders\*.txt d:\orders\done

The only other output from the FTP is an error code of 0 which occurs if the transfer is successful.

Can anyone correct where I am going wrong as it doesn't sppear to work. Could do with a quick answer to this one if possible so am allocating 300 points for it :o)

Many thanks
Richard
rabelleAsked:
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.

aflockhartCommented:
Haven't done it this way fo a while but HELP IF at a command line gives some info about the syntax (Win XP).


Brackets around the "Echo" command might be needed.

IF ERRORLEVEL 1 (echo ErrorSendingFile %DATE:~10,4%%DATE:~7,2%%DATE:~4,2% >> d:\orders\orders.txt ) ELSE MOVE d:\orders\*.txt d:\orders\done


Info from HELP IF copied below:

--------------------
The ELSE clause must occur on the same line as the command after the IF.  For
example:

    IF EXIST filename. (
        del filename.
    ) ELSE (
        echo filename. missing.
    )

The following would NOT work because the del command needs to be terminated
by a newline:

    IF EXIST filename. del filename. ELSE echo filename. missing

Nor would the following work, since the ELSE command must be on the same line
as the end of the IF command:

    IF EXIST filename. del filename.
    ELSE echo filename. missing

The following would work if you want it all on one line:

    IF EXIST filename. (del filename.) ELSE echo filename. missing


----------------------------------------


0
rabelleAuthor Commented:
Whilst I can see now idea why it shouldn't work unfortunately it didn't. In the end we have used the following - this way if it worked, an errorlevel 0 would run MOVEFILE then GOTO END - and errorlevel of 1+ would skip MOVEFILE and write to the logfile:

IF errorlevel 1 GOTO RPTERROR
IF errorlevel 2 GOTO RPTERROR
IF errorlevel 3 GOTO RPTERROR
IF errorlevel 4 GOTO RPTERROR

:MOVEFILE
MOVE d:\orders\*.txt d:\orders\done
GOTO END

:RPTERROR
echo ErrorSendingFile %DATE:~10,4%%DATE:~7,2%%DATE:~4,2% >> d:\orders\logfile.txt

:END
0
DrWarezzCommented:
Hello :-)

What you must remember here is that the program COULD return an errorlevel, other than 1 OR 0! What would happen then? You'd be screwed! :P

Also, I THINK that you need to use a carriage return after the openning bracket in the IF/ELSE statements.

So, try this:


:// Run the program that returns the errorlevel here.

IF ERRORLEVEL 1 echo ErrorSendingFile %DATE:~10,4%%DATE:~7,2%%DATE:~4,2% >> d:\orders\orders.txt  & goto :EOF
IF ERRORLEVEL 0 MOVE d:\orders\*.txt d:\orders\done & goto :EOF

:DEFAULT
:// Neither 0 or 1 were returned (as errorlevels).

echo An unexpected errorlevel was returned: %err% >> d:\orders\orders.txt

:EOF
:// Output that the program has finished, or continue with another process ...


I hope that helps :-)
[r.D]
0

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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

SteveGTRCommented:
Just a point of clarification. Here's the help on ERRORLEVEL (located in the if /? help):

  ERRORLEVEL number Specifies a true condition if the last program run
                    returned an exit code equal to or greater than the number
                    specified.

IF ERRORLEVEL 1 will be true if the ERRORLEVEL >= 1. That's why it's important to test the ERRORLEVEL's in descending order and to branch out of the logic (with goto) like DrWarezz outlined:

IF ERRORLEVEL 1
IF ERRORLEVEL 0

Good Luck,
Steve
0
rabelleAuthor Commented:
Thanks for the tips and advice. We've modified our script and added some error handling based on your notes for the errorlevel and it now seems to work fine.

Thanks
Richard
0
DrWarezzCommented:
:-) ThanQ Rabelle.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.