Sorry, it didn't hit me until I read your last reply, but you the
%errorlevel% appears within a compound statement.
As is the case with a simple statement, the statement is read in, the
environment %variable% references are expanded, and only then the command
starts executing. The upshot is that any variable references for all of the
statements in a compound statement are expanded before the first statement
is executed. And this will be before the execution of any statement that is
likely to modify the variables involved. Here is a test script you can run
to demonstrate this for yourself:
@echo off
setlocal enabledelayedexpansion
echo/%date% | find "0"
echo/%errorlevel%
echo/%date% | find "XX"
echo/%errorlevel%
for /l %%F in (1,1,1) do (
echo/%date% | find "0"
echo/%errorlevel%
echo/%date% | find "XX"
echo/%errorlevel%
)
for /l %%F in (1,1,1) do (
echo/%date% | find "0"
echo/!errorlevel!
echo/%date% | find "XX"
echo/!errorlevel!
)
pause
in each set of commands, the errorlevel value is zero and then some non-zero
value. This is accurately displayed in the first set of commands as they are
not contained within parentheses. In the second, the values displayed are 1
and 1. These are the values the variable had just before the for loop.
In the last for loop, the errorlevel values are displayed correctly because
the setlocal command enabled delayed expansion, and because "!" was used
instead of "%".
This is a common issue that catches just about everyone at one time or
another because it seems so counter intuitive.
/Al
"Jmnts" <> wrote in message
news:54108A13-F744-4B5F-8709-...
> Hi again and thank you all for you answers. Not sure why but I found out
> that
> the problem is that the %errorlevel% doesn't seemed to work (always
> returns
> the same code error) in the For condition. However if I don't use the For
> condition works pretty well!!! So I'm going to share the answer that I
> found.
> Basically I create a temporary file and check the results on that file:
>
> Begin---------------------
> cls
> net use * /d /y
>
> Echo. Begin >log.txt
> Echo. |date /T >>log.txt
> Echo. |time /T >>log.txt
> Echo. >>log.txt
>
> For /f "tokens=1,2,3" %%a in (ServerList.txt) do (
> Echo Username Password ServerIP
> Echo %%a %%b %%c
>
> Net use \\%%c %%b /User:%%a>tmp.txt
> Find "The command completed successfully" < tmp.txt > nul
> IF NOT ERRORLEVEL 1 (
> Echo Connected to %%c >>log.txt
> Echo Connected to %%c
> ) Else (
> Echo Connection Failed for to %%c >>Log.txt
> Echo Connection Failed for to %%c
> )
> del tmp.txt
> )
> End Batch ---------------------
>
> For now is working 
>
> Another question... I started this batch file from another one that I
> found
> in the net. The sample was something like this
>
> For /f "tokens=1" %%a in (C:\mytextfile.txt) do (
> If /i "%%a" NEQ "All" (
> etc... etc...
>
> this batch sample was reading servernames from mytextfile.txt and transfer
> files to those servers.
>
> What I don't full understand is the line "If /i "%%a" NEQ "All" ( "
> okay:
> "If" is the condition
> "/i" - Not sure what it means in this context
> "%%a" is variable that represents the "tokens=1"
> "NEQ" Not equal
> "All" - What is this? Is it trying to get something different from
> All???!!
>
>
> Thank you all.
>
>
>
>
> "Al Dunbar" wrote:
>
>>
>> "Jmnts" <> wrote in message
>> news:5AF5BF0A-2C87-4A6A-8D60-...
>> > Hi Pegasus and thank you for your time
>> > Using the commands on one batch works fine, but when I add them to the
>> > batch
>> > its stops working??
>>
>> Note that commands may not always return what you might expect as the
>> errorlevel. You sometimes have to find other means to verify the success
>> or
>> failure of the command being tested. For example, if the operation is to
>> delete a file, then you could test afterwards for its existence.
>>
>> > Perhaps you can help me if I put the entire script.
>> > This is working for successful connections, but when the connection
>> > fails
>> > or
>> > bad username the log is recorded as success "Connected to..."??!!
>> >
>> > Begin---------------------
>> > cls
>> > net use * /d /y
>> >
>> > Echo. Begin >log.txt
>> > Echo. |date /T >>log.txt
>> > Echo. |time /T >>log.txt
>> > Echo. >>log.txt
>>
>> As an aside, I'd suggest doing this a bit differently:
>>
>> (
>> Echo/ Begin
>> Echo/ %date%
>> Echo/ %time%
>> Echo/
>> ) >log.txt
>>
>> Using the DATE and TIME variables instead of the commands will allow them
>> to
>> appear on the same line:
>>
>> (
>> Echo/ Begin at %time% on %date%
>> Echo/
>> ) >log.txt
>>
>> Also, "echo/" is marginally better than "echo.", however I can't quite
>> remember why...
>>
>> > For /f "tokens=1,2,3" %%a in (ServerList.txt) do (
>> > Echo Username Password ServerIP
>> > Echo %%a %%b %%c
>> >
>> > Net use \\%%c %%b /User:%%a
>> >
>> > if %ErrorLevel% EQU 0 (
>> > Echo Connected to %%c >>log.txt
>> > Echo Connected to %%c
>> > ) Else (
>> > Echo Connection Failed for to %%c >>log.txt
>> > Echo Connection Failed for to %%c
>> > )
>> > )
>> > End Batch ---------------------
>> >
>> > Thank you
>> >
>> > "Pegasus (MVP)" wrote:
>> >
>> >>
>> >> "Jmnts" <> wrote in message
>> >> news:9EF692B8-8B62-468E-99AB-...
>> >> > Hi everyone
>> >> >
>> >> > I'm trying to do errorlevel handling but not with expected results:
>> >> > Code:
>> >> >
>> >> > Net use \\%%a P@ssw0rd /User:mydomain\admin
>> >> > IF NOT ERRORLEVEL 0 (
>> >> > Echo. Connection Failed for to %%a
>> >> > Echo. Connection Failed for to %%a >>Log.txt
>> >> > ) Else (
>> >> > Echo. Connected to %%a
>> >> > Echo. Connected to %%a >>Log.txt
>> >> > )
>> >> >
>> >> >
>> >> > The problem is that always "Echo. Connected to %%a" not matter if it
>> >> > fails
>> >> > or not, the result is always the saame. Can anyone help me?
>> >>
>> >> I hate double inverted logic. It goes against human thinking. Why not
>> >> keep
>> >> things simple and use direct logic, e.g. like so:
>> >> Net use \\%%a P@ssw0rd /User:mydomain\admin
>> >> if %ErrorLevel% EQU 0 (
>> >> Echo Connected to %%a
>> >> Echo Connected to %%a >>Log.txt
>> >> ) Else (
>> >> Echo Connection Failed for to %%a
>> >> Echo Connection Failed for to %%a >>Log.txt
>> >> )
>> >>
>> >> Note the changed syntax: In the above code "ErrorLevel" is an
>> >> environmental
>> >> variable and "EQU" must be in caps.
>> >>
>> >>
>> >>
>>
>>
>>