rabelle
asked on
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%%DA TE:~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
IF ERRORLEVEL 1 echo ErrorSendingFile %DATE:~10,4%%DATE:~7,2%%DA
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
ASKER
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%%DA TE:~4,2% >> d:\orders\logfile.txt
:END
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%%DA
:END
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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
Thanks
Richard
:-) ThanQ Rabelle.
Brackets around the "Echo" command might be needed.
IF ERRORLEVEL 1 (echo ErrorSendingFile %DATE:~10,4%%DATE:~7,2%%DA
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
--------------------------