Reputation: 13
So I need to map a drive, copy some files and disconnect the drive... the problem is some of the computers which we don't know which are which will have different passwords... so I am trying to use this but it doesn't seem to recognize the error for an invalid password.
Any help would be appreciated.
net use Z: "\\POS1\c$\PATH" %password% /user:%username%
IF ERRORLEVEL 0 goto copy
IF ERRORLEVEL 1 goto retry1
:retry1
net use Z: "\\POS1\c$\PATH" %password2% /user:%username2%
IF ERRORLEVEL 0 goto copy
IF ERRORLEVEL 1 goto retry2
:retry2
net use Z: "\\POS1\c$\PATH" %password3% /user:%username3%
IF ERRORLEVEL 0 goto copy
IF ERRORLEVEL 1 goto POS2
:copy
xcopy /y "c:\avi\aaaaaa.ini" "Z:\"
xcopy /y "c:\avi\aaaaaa.avi" "Z:\"
net use Z: /delete
:POS2
Upvotes: 1
Views: 12665
Reputation: 186
There is a massive difference in the meaning of...
IF ERRORLEVEL 0 yourCommand
and
IF %ERRORLEVEL%==0 yourCommand
I stumbled on it too.
In the first case the ERRORLEVEL is like a keyword or function where you put "0" as a parameter in, only becoming "true" if the real ErrorLevel returned a Value of 0 OR HIGHER. This is counter intuitive for me and anyone who ever learned a proper script language and easy to fall for.
While the second Version is a simple STRING COMPARISON. Where the the execution of a Program automatically set the environment variable %ERRORLEVEL% to a String e.g. '0' which you compare to your own String, like 0. There it only matches if the both sides/Strings are identical.
Even this Case is solved already, i think its helpful to others for understanding or having another explanation to it.
Upvotes: 0
Reputation: 49
I know this is an old post but ...
Your errorlevel checking it the wrong way round.
The way "IF ERRORLEVEL" works means if the errorlevel is equal to or greater than.
So your first testIF ERRORLEVEL 0 goto copy
actually means if the errorlevel = 0 or 1 or 10 or 64 or any number greater than 0. So your script never gets to the 2nd test IF ERRORLEVEL 1 goto retry1
the solution is to just reverse your tests
IF ERRORLEVEL 1 goto retry1
IF ERRORLEVEL 0 goto copy
Upvotes: 1
Reputation: 49
if errorlevel 0
makes no sense, because it means errorlevel 0 AND all errorlevel above.
When you want to check errorlevel 0, you have to write: if not errorlevel 1 (and above)
Upvotes: 2
Reputation: 975
Some programs and command return an errorlevel
other than 1
or 0
. They can be negative, in the thousands etc, so if the net
command returns an errorlevel
of, lets say 10
for an invalid password, it will skip past because it is not 0
or 1
For the most part,
if errorlevel 0 (
goto :copy
) else (
goto :retry1
)
rem Or, a single line version;
if errorlevel 0 (goto :copy) else (goto :retry)
Should work.
An example using the variables;
if %errorlevel% equ 0 (
echo No errors detected!
) else (
echo An error occured: [%errorlevel%]
)
This is made for my pure joy of for loops. No logical reasoning, but it shows when !errorlevel!
could be used.
setlocal enableDelayedExpansion
for %%G in (!password1!, !username1!, !password2!, !username2!, !password3!, !username3!) do (
if not defined curPass (
set "curPass=%%G"
) else (
if not defined curUser (
set "curUser=%%G"
) else (
net use z: "\\pos1\c$\path" !curPass! /user:!curUser!
if !errorlevel! neq 0 (
xcopy /y "c:\avi\aaaaaa.ini" "z:\"
xcopy /y "c:\avi\aaaaaa.avi" "z:\"
net use z: /delete
)
set "curPass="
set "curUser="
)
)
)
)
:pos2
Upvotes: 1