?
Solved

Using Batch File to Move a File

Posted on 2004-11-01
6
Medium Priority
?
455 Views
Last Modified: 2008-03-04
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
0
Comment
Question by:rabelle
6 Comments
 
LVL 17

Expert Comment

by:aflockhart
ID: 12461387
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
 

Author Comment

by:rabelle
ID: 12461814
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
 
LVL 9

Accepted Solution

by:
DrWarezz earned 1200 total points
ID: 12462387
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
LVL 30

Expert Comment

by:SteveGTR
ID: 12463225
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
 

Author Comment

by:rabelle
ID: 12463284
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
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12463592
:-) ThanQ Rabelle.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…
Suggested Courses

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question