Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > ERRORLEVEL

Reply
 
 
Jmnts
Guest
Posts: n/a

 
      10-18-2008
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?
 
Reply With Quote
 
 
 
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      10-18-2008

"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.


 
Reply With Quote
 
Jmnts
Guest
Posts: n/a

 
      10-18-2008
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??
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

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.
>
>
>

 
Reply With Quote
 
Al Dunbar
Guest
Posts: n/a

 
      10-18-2008

"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?


1) IF ERRORLEVEL 3: this returns a true value if the errorlevel is 3 or
greater

2) IF ERRORLEVEL 0: this returns a true value if the errorlevel is 0 or
greater. It is therefore always true, unless a negative errorlevel is
possible.

2) IF NOT ERRORLEVEL 0: this returns a false value if the errorlevel is 0 or
greater. It is therefore always false, unless a negative errorlevel is
possible.

This has been the case since the old DOS days. A more workable version of
your code above would be:

> IF ERRORLEVEL 1 (
> 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
> )


/Al


 
Reply With Quote
 
Al Dunbar
Guest
Posts: n/a

 
      10-18-2008

"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.
>>
>>
>>



 
Reply With Quote
 
Jmnts
Guest
Posts: n/a

 
      10-18-2008
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.
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
Al Dunbar
Guest
Posts: n/a

 
      10-19-2008
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.
>> >>
>> >>
>> >>

>>
>>
>>



 
Reply With Quote
 
Jmnts
Guest
Posts: n/a

 
      10-19-2008
Thank you, I'll give it a try latter, but sounds convincent. What about the
"If /i "%%a" NEQ "All" ( " Thing?
Can you explain that?

Thank you again for your time guys.

"Al Dunbar" wrote:

> 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.
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      10-19-2008

"Jmnts" <> wrote in message
newsE799BDB-C403-41C1-AF51-...
> Thank you, I'll give it a try latter, but sounds convincent. What about
> the
> "If /i "%%a" NEQ "All" ( " Thing?
> Can you explain that?
>
> Thank you again for your time guys.
>


The line
For /f "tokens=1" %%a in (C:\mytextfile.txt) do
sets %%a to the first word on each line in c:\mytestfile.txt, one at a time.
Does that file include the word "All"?


 
Reply With Quote
 
Al Dunbar
Guest
Posts: n/a

 
      10-19-2008

"Jmnts" <> wrote in message
newsE799BDB-C403-41C1-AF51-...
> Thank you, I'll give it a try latter, but sounds convincent. What about
> the
> "If /i "%%a" NEQ "All" ( " Thing?
> Can you explain that?


All of the relevent info can be found by typing this command: "if /?"

In brief, what the above does is it conditionally executes the compount
statement between the "(" and the matching ")", but only if the variable
"%%a" contains the word All in any combination of uppercase/lowercase.

/Al

> Thank you again for your time guys.
>
> "Al Dunbar" wrote:
>
>> 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.
>> >> >>
>> >> >>
>> >> >>
>> >>
>> >>
>> >>

>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to set errorlevel env variable properly from a simple batch file: Answered rajenk@gmail.com Windows Vista General Discussion 1 11-06-2007 11:34 PM
Problem with ROBOCOPY version XP026 an ERRORLEVEL Tom Meier Windows Server 2 09-08-2007 08:55 PM
errorlevel 9020 -lew- Windows Media Center 0 03-06-2007 04:07 PM
NTBackup errorlevel for Win 2k3 seanmig Windows Server 7 11-06-2006 10:13 PM
Re: Schtasks errorlevel Pegasus \(MVP\) Windows Server 0 02-10-2006 01:20 AM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59