Solved

Using Batch File to Move a File

Posted on 2004-11-01
438 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
Question by:rabelle
    6 Comments
     
    LVL 17

    Expert Comment

    by:aflockhart
    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
    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:
    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
     
    LVL 30

    Expert Comment

    by:SteveGTR
    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
    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
    :-) ThanQ Rabelle.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    The Complete Ruby on Rails Developer Course

    Ruby on Rails is one of the most popular web development frameworks, and a useful tool used by both startups and more established companies to build strong graphic user interfaces, and responsive websites and apps.

    One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    856 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now