Need ErrorLevel Help in XCopy Batch File.

August 15, 2011 at 12:13:59
Specs: Windows XP
Hi, I'm trying to create a batch file that will stop the xcopy and delete batch process and return a "Drive Full" statement, if there is not enough space on my destination drive. This is what I have so far:

_______________________

@echo off
set INPUT=
set /p INPUT= Enter PREVIOUS Date (ddmmyyyy) for which you want to create an ARCHIVE folder:
md h:\archive\graphics\%INPUT%

xcopy "i:\*.*" "h:\archive\graphics\%INPUT%" /D /E /R /I /K /-Y || goto Error

echo y | rd /s /q i:\DailyWork

echo y | del i:\Clients.xls

:Error
echo The Archive Drive is FULL! STOPPING Backup Procedure.
Exit
_______________________

The batch file does stop, when the backup drive is full, but instead of showing the error statement about the drive being full, I get a message that instead states ...

"Insufficient disk space on current disk.
Insert another disk and type <Return> to continue..."

... and I have to hit the "X" control button in the upper right of the batch's pop-up window to close out of and exit the batch file. I've also tried the following variations, but the batch file ended with the same result:

1) xcopy "i:\*.*" "h:\archive\graphics\%INPUT%" /D /E /R /I /K /-Y || goto :Error
(adding the colon before the word Error)

2) xcopy "i:\*.*" "h:\archive\graphics\%INPUT%" /D /E /R /I /K /-Y
if %ErrorLevel% GTR 0 goto Error

From what I understand, batch file exit codes go from 0 to 5, and the code pertaining to insufficient disk space is 4 ... but to be save, I should have any code greater than 0 stop my xcopy/delete batch.

I would greatly appreciate any help in correcting my coding syntax for this batch file. Thanks so much!

~ Sally


See More: Need ErrorLevel Help in XCopy Batch File.

Report •


#1
August 16, 2011 at 16:46:28
Next to the XCOPY command where you have | | goto error you can replace that with:
xcopy "i:\*.*" "h:\archive\graphics\%INPUT%" /D /E /R /I /K /-Y | IF '%ERRORLEVEL%'=='1' GOTO ERROR
(Note that the above is meant to be one line.)

Then under that line you could put:
GOTO SUCCESS

Then have success be:
:SUCCESS
ECHO The backup was successful.
PAUSE
EXIT

Does that work for you?

Mouse could not be found. Click OK to continue.
What to do now?


Report •

#2
August 17, 2011 at 08:12:40
Dear RMT2,

Thank you for your reply and assistance.

Using the single pipe character "|" that you indicated be placed before the "IF" statement (i.e., | IF '%ERRORLEVEL%'=='1' GOTO ERROR), the batch stalled, and I had to hit the "X" control button in the upper right of the batch's pop-up window to close out of and exit the batch file.

When I put in a double pipe before the aforementioned "IF" statement you kindly provided, I still receive the message --

"Insufficient disk space on current disk.
Insert another disk and type <Return> to continue..."

-- requiring me to hit the "X" control button in the upper right of the batch's pop-up window to close out of and exit the batch file.

Is it possible that these problems are being caused by my not correctly placing the statements in the correct order? Here is how it currently looks:

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

@echo off
set INPUT=
set /p INPUT= Enter PREVIOUS Date (ddmmyyyy) for which you want to create an ARCHIVE folder:
md h:\archive\graphics\%INPUT%

xcopy "i:\*.*" "h:\archive\graphics\%INPUT%" /D /E /R /I /K /-Y | IF '%ERRORLEVEL%'=='1' GOTO ERROR

GOTO SUCCESS

echo y | rd /s /q i:\DailyWork

echo y | del i:\Clients.xls

:ERROR
echo The Archive Drive is FULL! STOPPING Backup Procedure.
PAUSE
Exit

:SUCCESS
ECHO The backup was successful.
PAUSE
EXIT

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

Again, thanks so much for your assistance,

Sally :)


Report •

#3
August 17, 2011 at 15:28:10
You're sure welcome :).

It says that the "/D" switch needs to look like this when looking at the help in a command prompt:
/D:m-d-y Copies files changed on or after the specified date.
If no date is given, copies only those files whose
source time is newer than the destination time.
Does the file work if you take out the "/D" switch from the program?

Mouse could not be found. Click OK to continue.
What to do now?


Report •

Related Solutions

#4
August 18, 2011 at 09:03:24
No, after removing the /D switch, I still unfortunately receive the message --

"Insufficient disk space on current disk.
Insert another disk and type <Return> to continue..."

~ Sally :(


Report •

#5
August 18, 2011 at 15:33:30
When the error message comes up, can you press another key besides the return key to close the message?

Mouse could not be found. Click OK to continue.
What to do now?


Report •

#6
August 18, 2011 at 18:23:44
When the <Return> key is pressed, it doesn't close the pop-up window for the batch file, it simply causes the lines --

"Insufficient disk space on current disk.
Insert another disk and type <Return> to continue..."

-- to repeat. The aforementioned lines continue to repeat when one presses any other key on the keyboard. The only way to close the batch file pop-up window is to click on the "X" in the upper right corner of the Window, which then brings up a pop-up window titled "End Program - C:\WINDOWS\system32\cmd.exe" that states "Windows cannot end this program. It may need more time to complete an operation. To return to Windows and check the status of the program, click Cancel. If you choose to end the program immediately, you will lose any unsaved data. To end the program now, click End Now." So, I then click on the "End Now" button to end the batch file and close its pop-up window.

~ Sally


Report •

#7
August 19, 2011 at 16:10:58
Okay, how about trying this:
IF '%ERRORLEVEL%'=='1' GOTO ERROR | xcopy "i:\*.*" "h:\archive\graphics\%INPUT%" /D /E /R /I /K /-Y

Does it help at all putting the IF '%ERRORLEVEL%' statement before the XCOPY command?

Mouse could not be found. Click OK to continue.
What to do now?


Report •

#8
August 20, 2011 at 06:35:44
Placing the "IF" statement before the xcopy results in the new folder being created and line "The backup was successful" followed by the line "Press any key to continue..." showing on the screen, however none of the files are copied to the new folder; it is simply an empty folder. Additionally, the DailyWork folder on the i:\. drive is not removed, nor is the Clients.xls file deleted on that drive, as directed in the batch file.

~ Sally


Report •

#9
August 20, 2011 at 08:37:16
What if you tried putting the double | | lines in, any difference?

Mouse could not be found. Click OK to continue.
What to do now?


Report •

#10
August 21, 2011 at 11:17:29
Putting in the double lines "||" results in the new folder being created, but nothing is copied over, and the batch closed immediately.

I spent all Saturday searching the internet for information on xcopy, IF statements and errorlevels, and tried countless batch variations. What I currently have is as follows:

=========================

@echo off
set INPUT=
set /p INPUT= Enter PREVIOUS Date (ddmmyyyy) for which you want to create an ARCHIVE folder:
md h:\archive\graphics\%INPUT%

xcopy i:\*.* h:\archive\graphics\%INPUT% /D/E/R/I/K/-Y/exclude:i:\ErrorMssg\omit.txt

if errorlevel 5 goto diskerror
if errorlevel 4 goto copyerror
if errorlevel 2 goto copyterminated
if errorlevel 1 goto nofiles
if errorlevel 0 goto success

:diskerror
xcopy i:\ErrorMssg\DiskError.rtf h:\archive\graphics\%INPUT%
echo A DISK WRITE ERROR OCCURRED! STOPPING Backup Procedure.
goto end

:copyerror
xcopy i:\ErrorMssg\CopyError.rtf h:\archive\graphics\%INPUT%
echo The Archive Drive is FULL! STOPPING Backup Procedure.
goto end

:copyterminated
echo Ctrl + C was pressed, Terminating the Archive Procedure.
goto end

:nofiles
echo There were NO FILES to Archive.
goto end

:success
echo y | rd /s/q i:\DailyWork
echo y | del i:\Clients.xls
echo The backup was successful.
goto end

:end
pause
exit

=========================

I think that the problem lies with the ErrorLevel, which for some reason is not being recognized and subsequently the GOTO statements are not working correctly. So, for example, when I run a test where there is nothing but the ERRORMSSG folder on the i:/ drive (which is excluded in the copy process), the batch file in it's current incarnation should encounter an ErrorLevel of 1, as there are no files to copy over, resulting in the statement "There were NO FILES to Archive." showing up in the batch window on-screen. However, instead what one sees on the screen in the batch pop-up window is the following, when one runs the batch:

---------------------
Enter PREVIOUS Date (ddmmyyyy) for which you want to create an ARCHIVE folder:
20082011
0 file(s) copied
The system cannot find the file specified.
Could not find i:\Clients.xls
The backup was successful.
Press any key to continue ...
---------------------

The batch does create the new folder, in this example titled 20082011, and only states that it cannot find the file "Clients.xls", as opposed to not finding both the "Clients.xls" file and "DailyWork" folder. Then, rather than returning the statement "There were NO FILES to Archive." from :nofiles as the batch should, instead it returns the statement "The backup was successful." from :success, which is incorrect.

According to what I've read, the ErrorLevel statements need to be in descending order 5, 4, 2, 1, 0 (as opposed to ascending order) for it to work properly. Also, that the Exit Code definitions are as follows:

5 - Disk write error occurred.
4 - Various errors including insufficient memory or disk space, an invalid drive name, or invalid syntax.
2 - The user pressed Ctrl+C to terminate xcopy.
1 - No files were found to copy.
0 - Files were copied without error.

I've tried variations with the Comparison Operators; for example both == and EQU in the IF statement, but it doesn't make any difference. I've also tried both both using and not using a preceeding colon in the GOTO statement:

"if errorlevel 5 goto :diskerror" and if "errorlevel 5 goto diskerror"

as I've seen both ways used in tutorial examples, but my batch still doesn't work correctly.

I am at a total loss as to how to proceed. It's so frustrating.

~ Sally :(


Report •

#11
August 21, 2011 at 11:34:32
When you have the error lines like this:
:copyerror
xcopy i:\ErrorMssg\CopyError.rtf h:\archive\graphics\%INPUT%
echo The Archive Drive is FULL! STOPPING Backup Procedure.
goto end

the ECHO only displays the message on the screen, and does not actually pause the program. It is very simple to pause it:

:copyerror
xcopy i:\ErrorMssg\CopyError.rtf h:\archive\graphics\%INPUT%
echo The Archive Drive is FULL! STOPPING Backup Procedure.
pause
goto end

The above code would display:

The Archive Drive is FULL! STOPPING Backup Procedure.
Press any key to continue...

And when you press any key, the batch file will end. Try putting a pause command under each echo command for the errors, and see what happens.

Mouse could not be found. Click OK to continue.
What to do now?


Report •

#12
August 21, 2011 at 14:13:00
Having the "Pause" statement after the "Echo" statement is one of the variations I tried, but it didn't result in the batch working correctly ... it still returned the following, as in my previous posting ...

---------------------
Enter PREVIOUS Date (ddmmyyyy) for which you want to create an ARCHIVE folder:
20082011
0 file(s) copied
The system cannot find the file specified.
Could not find i:\Clients.xls
The backup was successful.
Press any key to continue ...
---------------------

... Where the batch did create the new folder, in this example titled 20082011, and stated that it could not find the file "Clients.xls", as opposed to not finding both the "Clients.xls" file and "DailyWork" folder. Then, rather than returning the statement "There were NO FILES to Archive." from :nofiles as the batch should, instead it returned the statement "The backup was successful." from :success, which is incorrect.

==================================================

NEW INFORMATION

After the xcopy line ...

xcopy i:\*.* h:\archive\graphics\%INPUT% /D/E/R/I/K/-Y/exclude:i:\ErrorMssg\omit.txt

... I added the line ...

echo XCOPY returned this code: %errorlevel%

... which returned the same result as above:

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

Enter PREVIOUS Date (ddmmyyyy) for which you want to create an ARCHIVE folder:
20082011
0 file(s) copied
XCopy returned this code: 0
The system cannot find the file specified.
Could not find i:\Clients.xls
The backup was successful.
Press any key to continue ...

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

The odd thing is that the error code (from the new line I inserted after the xcopy line) is showing as "0", rather than what I assume should be "1" (as I tested the batch with no files to copy). Should the IF statements go before the xcopy line, or do I have some other coding error?

~ Sally


Report •

#13
August 21, 2011 at 14:55:08
From your post #10, you had the code setup like this:

if errorlevel 5 goto diskerror
if errorlevel 4 goto copyerror
if errorlevel 2 goto copyterminated
if errorlevel 1 goto nofiles
if errorlevel 0 goto success

But that is one coding error you have. For an "IF" statement, it should look like this:

if '%errorlevel%'=='5' goto diskerror
if '%errorlevel%'=='4' goto copyerror
if '%errorlevel%'=='2' goto copyterminated
if '%errorlevel%'=='1' goto nofiles
if '%errorlevel%'=='0' goto success

With your old code, the IF command is thinking that the errorlevel (#) is an variable, not thinking IF the errorlevel equals the #. Try replacing those lines with mine, and see if it makes any difference.

Mouse could not be found. Click OK to continue.
What to do now?


Report •

#14
August 22, 2011 at 12:26:32
It didn't make any difference modifying the code in the IF statement, I still got the same return in the batch's pop-up window:

---------------------
Enter PREVIOUS Date (ddmmyyyy) for which you want to create an ARCHIVE folder:
20082011
0 file(s) copied
The system cannot find the file specified.
Could not find i:\Clients.xls
The backup was successful.
Press any key to continue ...
---------------------

I did, however, make a new modification in the batch code, which has the :no files section and the :success section working, but not the :copyerror section --

=========================================

for /f %%i in ('xcopy i:\*.* h:\archive\graphics\%INPUT% /D/E/R/I/K/-Y/exclude:i:\ErrorMssg\omit.txt') do (
if errorlevel 5 goto diskerror
if errorlevel 4 goto copyerror
if errorlevel 2 goto copyterminated
if %%i EQU 0 goto nofiles
if errorlevel 0 goto success
)

=========================================

In this test situation, where there isn't enough disk space on the destination drive (which should be Error 4), the batch stalls on-screen:

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

Enter PREVIOUS Date (ddmmyyyy) for which you want to create an ARCHIVE folder:
21082011
_ (with this cursor blinking)

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

The batch does create the new folder (in this case, named "21082011), and copies over the Clients.xls file to this folder, as well as copies over the DailyWork folder, which contains the Aer-Dgm.gif and NG.jpg files. The Aquatics-logo.cdr file is not copied over, of course, as it has a larger file size that the space left on the destination folder, but at this point the batch should jump to the :copyerror section, and next copy over the 1KB .RTF file (which has a single line of text stating that nothing copied over because the drive was full), and then show in the batch's pop-up window the statement "The Archive Drive is FULL! STOPPING Backup Procedure."

My next thought was that the .RTF file might be the issue, so I deleted the line in the :copyerror section ...

xcopy i:\ErrorMssg\CopyError.rtf h:\archive\graphics\%INPUT%

... to see whether that made any difference when I tested the batch again, but it didn't; the batch still stalled with the aforementioned information showing on the batch's pop-up window:

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

Enter PREVIOUS Date (ddmmyyyy) for which you want to create an ARCHIVE folder:
21082011
_ (with this cursor blinking)

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

Am I referencing the wrong ErrorLevel (i.e., "4") for the Insufficient Memory Line (i.e., :copyerror)? This is so confusing. :(

~ Sally


Report •

#15
August 22, 2011 at 17:50:21
Truthfully, I have never heard of the errorlevel going 0,1,2,4,5. The most errors I have ever had in a program is 1. What I think you need to try is a success/fail setup. Basically, replace your if errorlevel lines with this:

if errorlevel 1 goto failure
if errorlevel 0 goto success

Then have failure setup as:

:FAILURE
CLS
ECHO Backup has failed! Please see XCOPY.LOG.
GOTO END

I added the sentence "Please see XCOPY.LOG." for a reason. If you still wanted to know what the errors were, you can setup the XCOPY command like this:

xcopy i:\*.* h:\archive\graphics\%INPUT% /D/E/R/I/K/-Y/exclude:i:\ErrorMssg\omit.txt > XCOPY.LOG

What the extra code will do is create a file showing what would appear on the screen when executing the XCOPY command, including the errors, in the directory the batch file is located. I created a little remap of the entire code from post #10. Take a look:

@echo off
set INPUT=
set /p INPUT= Enter PREVIOUS Date (ddmmyyyy) for which you want to create an ARCHIVE folder:
md h:\archive\graphics\%INPUT%

xcopy i:\*.* h:\archive\graphics\%INPUT% /D/E/R/I/K/-Y/exclude:i:\ErrorMssg\omit.txt > XCOPY.LOG

if errorlevel 1 goto failure
if errorlevel 0 goto success

:FAILURE
CLS
ECHO Backup has failed! Please see XCOPY.LOG.
GOTO END

:success
echo y | rd /s/q i:\DailyWork
echo y | del i:\Clients.xls
echo The backup was successful.
goto end

:end
pause
exit

Perhaps try it out and see if you like it?

Mouse could not be found. Click OK to continue.
What to do now?


Report •

Ask Question