Click to See Complete Forum and Search --> : .bat errorlevel questions
Gary Andrews
08-28-2002, 12:43 PM
It is my understanding that running xcopy in a .bat file will return an error level code.
I believe a code of zero is successful and a code of 1 is unsuccessful.
Is the following syntax correct?
If not errorlevel 0 goto unsuccessful
Also it is my understanding that the command DEL does not return any errorlevel code.
Is this correct? (Or what codes does DEL return?)
Does executing the command EXIT change or modify the code last set by the .bat script?
TIA Gary
Matt Winer
08-29-2002, 09:11 AM
Gary,
try this site: http://www.microsoft.com/technet/treeview/default.asp?url=/technet/prodtechnol/winxppro/proddocs/xcopy.asp
and
http://www.microsoft.com/technet/treeview/default.asp?url=/technet/prodtechnol/winxppro/proddocs/ntcmds.asp
****SNIP****
xcopy c:outlook*.pst h: /y /f >>%logfile%
if not %errorlevel% == 0 goto fail:
****SNIP****
In that code I use xcopy to copy all pst files to h:
The /y is to suppress prompting and the /f is to display source and destination files while copying. The display is output to a logfile.
Then check the error level to make sure it worked (0 = success) if it didn't then go to fail.
Now according to the info in Microsoft's site. You don't need the % around the errorlevel var. But I was only able to get it to work that way.
On MS's site there is nothing that talks about errorlevel for del. However, you can do some simple testing to find out if it does. (I have taken the liberty for you):
I created a file called test.txt
At a cmd prompt. I ran "del test.txt" then "echo %errorlevel%" it returned 0
then I tried "del dfswueow.txt" then "echo %errorlevel%" it returned 1
So it goes to show that YES del does return error codes. So if you use
****SNIP****
del c:*.txt
if not %errorlevel% == 0 goto fail:
****SNIP****
It would work.
If you want to just check to see if a file is there you can also use
if exist c:filename.txt goto exists
or
if not exist c:filename.txt goto notexist
Good luck
-Matt
------------
Gary Andrews at 8/28/2002 1:43:31 PM
It is my understanding that running xcopy in a .bat file will return an error level code.
I believe a code of zero is successful and a code of 1 is unsuccessful.
Is the following syntax correct?
If not errorlevel 0 goto unsuccessful
Also it is my understanding that the command DEL does not return any errorlevel code.
Is this correct? (Or what codes does DEL return?)
Does executing the command EXIT change or modify the code last set by the .bat script?
TIA Gary
Gary Andrews
08-29-2002, 10:23 AM
Many, many thanks Matt. Your efforts are appreciated. Thanks for taking the time to share your expertise and knowledge with others.
Gary
------------
Matt Winer at 8/29/2002 10:11:24 AM
Gary,
try this site: http://www.microsoft.com/technet/treeview/default.asp?url=/technet/prodtechnol/winxppro/proddocs/xcopy.asp
and
http://www.microsoft.com/technet/treeview/default.asp?url=/technet/prodtechnol/winxppro/proddocs/ntcmds.asp
****SNIP****
xcopy c:outlook*.pst h: /y /f >>%logfile%
if not %errorlevel% == 0 goto fail:
****SNIP****
In that code I use xcopy to copy all pst files to h:
The /y is to suppress prompting and the /f is to display source and destination files while copying. The display is output to a logfile.
Then check the error level to make sure it worked (0 = success) if it didn't then go to fail.
Now according to the info in Microsoft's site. You don't need the % around the errorlevel var. But I was only able to get it to work that way.
On MS's site there is nothing that talks about errorlevel for del. However, you can do some simple testing to find out if it does. (I have taken the liberty for you):
I created a file called test.txt
At a cmd prompt. I ran "del test.txt" then "echo %errorlevel%" it returned 0
then I tried "del dfswueow.txt" then "echo %errorlevel%" it returned 1
So it goes to show that YES del does return error codes. So if you use
****SNIP****
del c:*.txt
if not %errorlevel% == 0 goto fail:
****SNIP****
It would work.
If you want to just check to see if a file is there you can also use
if exist c:filename.txt goto exists
or
if not exist c:filename.txt goto notexist
Good luck
-Matt
------------
Gary Andrews at 8/28/2002 1:43:31 PM
It is my understanding that running xcopy in a .bat file will return an error level code.
I believe a code of zero is successful and a code of 1 is unsuccessful.
Is the following syntax correct?
If not errorlevel 0 goto unsuccessful
Also it is my understanding that the command DEL does not return any errorlevel code.
Is this correct? (Or what codes does DEL return?)
Does executing the command EXIT change or modify the code last set by the .bat script?
TIA Gary