1.

Solve : Need ErrorLevel/Exit Code Help.?

Answer»

Hi, I'm trying to create a batch file that will stop the xcopy and delete batch process and return an Error statement, in the event that there is not enough space on my destination drive or no files to copy or a physical disk error, etc ... or returns a successful copy message if there were no errors. 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/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.
pause
goto end

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

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

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

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

:end
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.

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

~ SallyWhat happens if you use the Windows 2000 (and later) errorlevel syntax?

Place this line immediately after the xcopy line

echo XCOPY returned this code: %errorlevel%

Hi, thanks for your reply.

I am testing the batch with no files to copy ... The following shows up is the batch pop-up window:

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

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 batch did create the new folder, in this example titled 20082011, and only stated that it couldn't 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. For some strange reason, the error code showing in the new line I inserted after the xcopy line is "0", rather than what I assume should be "1", as there were no files to copy in this test. Should the IF statements go before the xcopy line, or do I have some other coding error?

~ SallyI had the same problem when testing. Zero files copied produced a zero errorlevel, not one. In fact the only other errorlevel I could reproduce was four.

Try putting the xcopy in a for statement. You can then scrape the console and check to see how many files were copied:

Code: [Select]@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%

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
)

Good luck. Wow, thanks! That did the trick to fix the 'no files to copy' part of the batch. I then tested the situation where there was enough space on the destination drive for the files being copied over, and it worked like a charm -- creating the new folder, copying over the file and folder from i:\ to h:\, then deleting the deleting the DailyWork folder and Clients.xls on the i:\ drive, and showing the statement "The backup was successful." in the batch's pop-up window.

Unfortunately, when I next tested the situation where there wasn't enough disk space on the destination drive (which should be Error 4), the batch stalled on-screen:

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

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

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

The did create the new folder (in this case, named "21082011) was created, and copy over the Clients.xls file to this folder, as well as copied over the DailyWork folder, which contains the Aer-Dgm.gif and NG.jpg files. The Aquatics-logo.cdr file was not copied over, of course, as it has a larger file size that the space left on the destinatin folder, but at this point the batch should have jumped to the :copyerror section, and next copied over the 1KB .RTF file (which has a single line of text stating that nothing copied over because the drive was full), and then showed 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)

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

So, next I went to my backup batch, with the previous code + the line you offered to follow the xcopy line in order to have the ErrorLevel displayed.

------------------------------------
xcopy i:\*.* h:\archive\graphics\%INPUT% /D/E/R/I/K/-Y/exclude:i:\ErrorMssg\omit.txt
echo XCOPY returned this code: %errorlevel%

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
------------------------------------

The result was that the batch ran to the point of creating the new folder and copying over the first three files, but when it got to the fourth file (again, which exceeded the capacity of space left on the destination drive), instead of going to the :copyerror section and copying over the 1KB .RTF file and having the statement "The Archive Drive is FULL! STOPPING Backup Procedure." show on the batch's pop-up window, it executed the following:

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

Enter PREVIOUS Date (ddmmyyyy) for which you want to create an ARCHIVE folder:
21082011

I:\Clients.xls
I:\Archive\Aer.Dgm.gif
I:\Archive\NG.jpg
I:\Archive\Aquatics-logo.cdr

Insufficient disk space on current disk.
Insert another disk and type to continue...

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

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

~ SallyTesting turned up some interesting results:

1. If the XCOPY is embedded in a FOR loop, the errorlevel is always zero but you can scrape the console for data.

2. If the XCOPY is not embedded in a FOR loop, the errorlevel works as advertised, except the no files and success CONDITIONS both set errorlevel to zero.

This workaround takes a little from each situation. Not pretty but not Coyote Ugly either:

Code: [Select]@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 > %temp%\results.txt

if errorlevel 5 goto diskerror
if errorlevel 4 goto copyerror
if errorlevel 2 goto copyterminated
if exist %temp%\results.txt (
find /i "0 File(s)" %temp%\results.txt > nul
if errorlevel 1 (goto success) else (goto nofiles)
)

If the XCOPY command issues any prompts or messages, they go the the results file and won't be seen by the user.

Hope this helps
Quote from: Sidewinder on August 22, 2011, 06:12:42 PM

1. If the XCOPY is embedded in a FOR loop, the errorlevel is always zero but you can scrape the console for data.

What about using delayed expansion and checking !errorlevel! ?



Quote from: Salmon Trout on August 23, 2011, 12:02:13 AM
What about using delayed expansion and checking !errorlevel! ?

Been there, done that.

Same results. See my previous post. Don't always succeed but try very hard to post only code that works. Anything else is left on the cutting room floor.

The exclude file might create a problem with the VBScript CopyFolder method, but using Powershell to pipeline output from get-childitem into copy-item might work, depending on the actual exclude list.Thanks so much for your ongoing help!

Again, unfortunately, the :nofiles and :success sections are working, but the batch still stalls when there isn't enough disk space on the destination drive (which should be Error 4, right?). This is what shows up in the batch's pop-up window:

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

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

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

The batch does create the new folder (in this case, named "22082011), and copies over all the files that can fit onto the space left on the destination drive, but it doesn't then jump to the :copyerror section, and next copied 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."

I again, tried deleting the .RTF file xcopy line in the :copyerror section, in the event that it might be the issue ...

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

... but when I tested the batch again, it didn't make any difference; 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)

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

... even though the batch did create the new folder and copied over all the files that can fit onto the space left on the destination drive.

If the exclude file switch in the line:

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

I can always get rid of it --

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

-- and have the .RTF error messages in the :diskerror and :diskcopy sections be copied from a folder on the destination h:\ drive, rather than the i:\ drive. like so:

:diskerror
xcopy h:\ErrorMssg\DiskError.rtf h:\archive\graphics\%INPUT%
...

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

Or even eliminate those lines entirely; I just thought having a file stating that there was a problem with the copy process placed into the destination folder might be a good idea for purposes of record-keeping and archive integrity, but it doesn't need to be done, if it's causing problems with the execution of the batch file.

~ Sally

Quote
Again, unfortunately, the :nofiles and :success sections are working, but the batch still stalls when there isn't enough disk space on the destination drive (which should be Error 4, right?). This is what shows up in the batch's pop-up window:

You can see for yourself what the actual errorlevel is by echoing it to the screen:

Code: [Select]@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 > %temp%\results.txt

echo %errorelevel%

if errorlevel 5 goto diskerror
if errorlevel 4 goto copyerror
if errorlevel 2 goto copyterminated
if exist %temp%\results.txt (
find /i "0 File(s)" %temp%\results.txt > nul
if errorlevel 1 (goto success) else (goto nofiles)
)

Also try leaving @echo on as the first line. The output may look messy but you'll get a good indication of how the batch file is executing.

Code: [Select]The batch does create the new folder (in this case, named "22082011)

Not related to XCOPY, there is a MD instruction to specifically create it.

I guess what I'm not understanding why on errorlevel 4. you're copying a rtf file to a full disk and then knowing it will fail, echoing a message "The Archive Drive is FULL! STOPPING Backup Procedure.". XCOPY issues it's own messages based on the situation. Example: Errorlevel 4 also issues "Invalid drive specification" when the drive spec on the source or target paths is invalid.

Perhaps you could check for errorlevel 1, which includes all errorlevels greater or equal to one and leave the default code (errorlevel 0) to distinguish between zero records copied and more than zero records copied.



I checked my old DOS-5 manual and the XCOPY error codes have not changed in decades. It was my idea to have the .RTF file copied into the daily archive, in the event of some sort of copy error, since I am finding myself in the office less and less at the end of the day, and this task then falls to the office manager (who is the son of the boss, and is not detail-oriented, to say the least) or the OM's secretary/girlfriend (who is about as competent as Capt. Hazelwood on the Exxon Valdez) ... the problem being, that neither one writes down writes down error messages that might come up on their screens for anything they're doing, with any kind with any program, in any situation, which would then result in someone having to track down what had occurred, what caused the problem and how it could be fixed (if at all). I figured that with the .RTF file, we would at least have an explicit method with the daily archiving process of determining what error might have occurred, should there be any errors.

It was also my idea in the first place to be using the batch file for the archiving, since prior to that, I had simply been doing the process manually, using copy/paste in Windows Explorer at the end of the day. But as this little start-up began to grow, and I began to be out of the office more and more, meeting with existing and new potential clients, the archiving task was then PASSED on to the OM and his Sec (much to their unhappiness), and there were a couple instances where they were placing the archived files in the wrong place or completely overwriting files, and it all turned into an incredible headache. So, I volunteered to come up with this automated batch process (with albeit very limited coding ability), in order to solve our daily archiving issue. It was only when our archive drive got filled up that I realized that I needed to have a batch that was much more sophisticated and specific than what I had initially created, and that I was (quite frankly) in over my head with what we needed the batch to accomplish.

I tried adding in, after the xcopy line -- echo %errorelevel% -- but the batch didn't show any error codes and stalled onscreen at:

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

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

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

Though the batch did create the new folder and copied over those files that would fit onto the space left on the destination (i.e., archive) drive.

I also changed the first line to @echo on, but I didn't see any error codes, nor anything specific that would indicate where the problem was occurring. When I looked at the Results.txt file that was created in my Temp file, it also didn't show anything specific as to what might be causing the problem:

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

I:\Clients.xls
I:\archive\graphics\Aer-Dgm.GIF
I:\archive\graphics\NG.jpg
I:\archive\graphics\Aquatics-logo.cdr {this is the file that is larger than the space left on the destination drive for the :copyerror test}

Insufficient disk space on current disk.
Insert another disk and type to continue...

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

When you say:

"Perhaps you could check for errorlevel 1, which includes all errorlevels greater or equal to one and leave the default code (errorlevel 0) to distinguish between zero records copied and more than zero records copied."

Do you mean changing the code to look like this?:

-----------------------------------
@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 > %temp%\results.txt

echo %errorelevel%

if exist %temp%\results.txt (
find /i "0 File(s)" %temp%\results.txt > nul
if errorlevel 1 (goto success) else (goto nofiles)
)
-----------------------------------

Thus getting rid of errorlevel 5, 4, & 2? I guess that would work, except in the instance where there is a limited amount of space left on the archive drive, and during the archiving process only a limited number of files would get copied over; enough to fill up the remaining space, leaving the rest un-archived. But we need everything archived each day, and I can't count on the OM or Sec to be observant enough to notice that the archive drive is full, without a notice to that effect popping up on the screen. So, I guess I'm really at a quandry as to what to do.

~ SallyI was thinking more along these lines:

Code: [Select]@echo off
set INPUT=
set /p INPUT= Enter PREVIOUS Date (ddmmyyyy) for which you want to create an ARCHIVE folder:

xcopy i:\*.* h:\archive\graphics\%INPUT% /D/E/R/I/K/-Y/exclude:i:\ErrorMssg\omit.txt 1>%temp%\results.txt 2>%temp%\results.log

if errorlevel 1 goto :eof

if exist %temp%\results.txt (
find /i "0 File(s)" %temp%\results.txt > nul
if errorlevel 1 (goto success) else (goto nofiles)
)

The first if errorlevel checks results from XCOPY; the second from the FIND command.

results.log will contain any error messages issued by XCOPY
results.txt will contain a log of the file copies along with the total number of files processed

The two results files get overwritten each time the file is run. You can append each days output to the results files by using >> instead of > You can also change the paths of the results files. I chose %temp% hoping everyone has one. This should allow you to check the logs when convenient and not rely on someone else.

Good luck. Correction to above post. Better to use findstr instead of find. A regular expression can better distinguish between an actual zero count and a count that ends in zero (10, 20, 100, 150 etc)

Code: [Select]@echo off
set INPUT=
set /p INPUT= Enter PREVIOUS Date (ddmmyyyy) for which you want to create an ARCHIVE folder:

xcopy i:\*.* h:\archive\graphics\%INPUT% /D/E/R/I/K/-Y/exclude:i:\ErrorMssg\omit.txt 1>%temp%\results.txt 2>%temp%\results.log

if errorlevel 1 goto :eof

if exist %temp%\results.txt (
findstr /i /r "^0 files(0)" results.txt > nul
if errorlevel 1 (goto success) else (goto nofiles)
)



Discussion

No Comment Found