Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using Batch File to Move a File

Posted on 2004-11-01
6
Medium Priority
?
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Suggested Courses

618 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