rely on errorlevel to give the feedback

June 28, 2009 at 23:48:50
Specs: Windows XP, 512mb
Hi,

As general, i had wrote one simple script to copy file from one location to another destination location. However the script is just a very script without any useful information for non-IT user.

So try to workout on the errorlevel which once succesful copy file, it will appears the message info at command prompt.

my trial command as below, but it does not shows the expected result.

@echo off

echo .. Summary ..
echo ..
echo ..

-- Expected have file to copy -- expected to copy file
copy c:\a\trial.txt c:\b\
echo.
if errorlevel 0 set true_=YES
if errorlevel 1 set true_=NO
if %true_%== YES goto level1
if %true_%== NO goto level2

:level1
echo A. Copied file trial.txt
:level2
echo A. Failed to copy file trial.txt

echo ..
echo ..

-- expected no file to copy - expected to be failed.
copy c:\trial2nd.txt c:\ab\
echo.
if errorlevel 0 set true1_=YES
if errorlevel 1 set true1_=NO
if %true1_%== YES goto level11
if %true1_%== NO goto level12

:level11
echo B. Copied file trial2nd.txt
:level12
echo B. Failed to copy file trial2nd.txt

echo ..
echo ..
echo .. end of summary

pause


Did my script have problem!
Thanks for point out my problem!

ang


See More: rely on errorlevel to give the feedback

Report •


#1
June 29, 2009 at 01:56:35
:: Your script has no shortgae of problems.
:: You can save much typing and confusion:

@echo off & setLocal enableDELAYedexpansion

copy a b

goto :%errorlevel%

:0
echo copy OK
goto :eof

:1
echo copy failed


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
June 29, 2009 at 06:58:23
Mechanix2Go: goto :%errorlevel%
What I don't like about this technique is that the script will bomb if COPY ever throws something other than 0 or 1.

One line version:

@(COPY a b >NUL 2>&1 &&echo Okay) ||@echo Failed.
.

Report •

#3
June 30, 2009 at 00:02:11
As Woodward famously said to Bernstein, Yours is better.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
June 30, 2009 at 03:12:06
Mechanix2Go: Yours is better.
I wouldn't say better. Just different; thrown in because I didn't want to criticize a style without contributing.

Report •

#5
June 30, 2009 at 21:11:09
Hi,

I had tried out the command. it works! Thanks!

However, i not get the clear structure on the condition you written!

@(COPY a b >NUL 2>&1 &&echo Okay) ||@echo Failed.

btw, can use the some command above to get the errorlevel value?

ang


Report •

#6
July 1, 2009 at 05:42:42
i not get the clear structure on the condition you written!
@
Turns off ECHO for this command
COPY a b
Copy a to b
>NUL
Do not display the standard output
2>&1
Send any error messages to the same place as standard output (do not display)
&&
Run only if the previous command (the copy) succeeded (errorlevel == 0)
ECHO
Display a message
( )
Group both commands together
||
Run only if the previous commands (the copy and technically the echo) failed (errorlevel != 0)

can use the some command above to get the errorlevel value?
Not on this line, no. You only know if the errorlevel was 0 or not 0


Report •


Ask Question