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