I'm running this simple command from command prompt and from a batch file. Although both "worked" in that the file is copied to the destination but the return code is always 1 when run from batch, and 0 from command prompt.
copy /Y d:\my.zip C:\my\target\
I can't figure out where it was wrong except that I can see the return code when I
@echo on
Any tip is appreciated.
UPDATE
I digged further and noticed that I execute the command inside a if NOT %errorlevel% == 0 () block, once I moved it out then it runs ok. I've already setlocal enabledelayedexpansion and setlocal enableextensions, so I thought I should be ok doing stuff inside the if block, but turns out that it was a bad idea. Could you please explain what I did wrong initially?
Old code
setlocal enabledelayedexpansion
setlocal enableextensions
where mycmd
if NOT %errorlevel% == 0 (
copy /Y d:\my.zip C:\my\target\
if NOT %errorlevel% == 0 (
echo Failed to copy
goto :fail
)
start /wait cmd /c "tar -xvf c:\my\target\my.zip -C c:\my\target\"
if NOT %errorlevel% == 0 (
echo Failed to unzip
goto :fail
)
setx PATH "%PATH%;c:\my\target"
if NOT %errorlevel% == 0 (
echo Failed to update user path
goto :fail
)
)
All these commands inside the top-level if-block fail.
I thought the delayed expansion would work with the initial setlocal enabledelayedexpansion, but it turns out to be not the case.
Where was I wrong?