[<<]Message[>>]    [<<]Author[>>]    [<<]Subject        [<<]Thread    

Number : 4421 Date : 2003-04-23 Author : Garry Deane Subject : Re: errorlevel Size(KB) : 3
--- In xxcopy@yahoogroups.com, "jtv1966" wrote: > Hi Garry, > > I tried your code & still get the wrong exit code for xcopy. > When I xcopy a dir that doesn't exist the errorlevel return > a 0 within the script but if you do it interactively at c:\ > it returns a 4. I don't think errorlevel is holding the exit > code after the xcopy. Let me know if you have any more > suggestions. Thanks You can be assured that xxcopy will be setting an exit code and that the %errorlevel% will be set accordingly. I've just re-read your original post and realised that I mistakenly overlooked that you were doing the tests within a FOR loop. You've run into the problem of non- delayed variable expansion when evaluating variables within multi- line commands. In brief, the value of the variable is substituted into the multi-line commands (commands within parentheses) BEFORE the commands are executed. Thereafter, even though the value of these variables may change, the commands are still using the values that were substituted before the command started. Additionally the use of EQU, NEQ etc., doesn't work in a FOR statement under NT but is OK under W2k/XP. Since you didn't mention which OS you are using, either or both of these problems may have affected the errorlevel tests. There are a number of ways to work around/avoid these problem. 1. You can safely use IF ERRORLEVEL nn tests as these will evaluate correctly but they need to be a bit more complex to handle the 4 major errorlevel groups (0, 1-48, 100, 101-255). for /f %%i in (%1) do ( xxcopy %%i c:\arcto%%i /E /V /H /I /Y /R if errorlevel 101 (echo error 100+) else ( if errorlevel 100 (echo error 100) else ( if errorlevel 1 (echo error 1+) else ( (echo no error) )))) See xxcopy /helpe for an explanation of the error codes. In your case, since you are deleting the source directory, errorlevel 0 and 100 are equivalent to success and any other is a failure. 2. You can use the /ER switch to simplify the tests. Errorlevel 0 or 1 are equivalent to 0 and 100 (success) and 2 or greater is equivalent to 1-48 and 101-255 (some failure). for /f %%i in (%1) do ( xxcopy %%i c:\arcto%%i /E /V /H /I /Y /R /ER if errorlevel 2 (echo an error occurred) else ( (echo no error) )) 3. On W2k/XP (but not NT) you can use the command "SETLOCAL ENABLEDELAYEDEXPANSION" then within the multi-line commands, you use "!" instead of "%" around variable names to have them evaluated at the time of execution (see SET /?) e.g. setlocal enabledelayedexpansion for /f %%i in (%1) do ( xxcopy %%i c:\arcto%%i /E /V /H /I /Y /R Echo the errorlevel is !errorlevel! ) 4. You can move all the code within the DO (...) part to a subroutine and then "CALL :subroutine parameters" which overcomes the problem completely (see CALL /?) e.g. for /f %%i in (%1) do call :xxcopy %%i goto :eof :xxcopy :: the parameter passed from the FOR statement is %1 xxcopy %1 c:\arcto%1 /E /V /H /I /Y /R Echo the errorlevel is %errorlevel% goto :eof There are a few more methods which address this problem but one of the above should do you. Sorry for leading you astray with my earlier replies. Garry
This message if part of XXCOPY's message Archive. The archive contains all the messages posted at Yahoo!Groups: XXCOPY.