ERRORLEVEL

Discussion in 'Scripting' started by Jmnts, Oct 18, 2008.

  1. Jmnts

    Jmnts Guest

    Hi everyone

    I'm trying to do errorlevel handling but not with expected results:
    Code:

    Net use \\%%a [email protected] /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?
     
    Jmnts, Oct 18, 2008
    #1
    1. Advertisements

  2. 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 [email protected] /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.
     
    Pegasus \(MVP\), Oct 18, 2008
    #2
    1. Advertisements

  3. Jmnts

    Jmnts Guest

    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
     
    Jmnts, Oct 18, 2008
    #3
  4. Jmnts

    Al Dunbar Guest

    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:
    /Al
     
    Al Dunbar, Oct 18, 2008
    #4
  5. Jmnts

    Al Dunbar Guest

    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.
    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...
     
    Al Dunbar, Oct 18, 2008
    #5
  6. Jmnts

    Jmnts Guest

    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.
     
    Jmnts, Oct 18, 2008
    #6
  7. Jmnts

    Al Dunbar Guest

    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
     
    Al Dunbar, Oct 19, 2008
    #7
  8. Jmnts

    Jmnts Guest

    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.
     
    Jmnts, Oct 19, 2008
    #8
  9. 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"?
     
    Pegasus \(MVP\), Oct 19, 2008
    #9
  10. Jmnts

    Al Dunbar Guest

    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
     
    Al Dunbar, Oct 19, 2008
    #10
  11. Jmnts

    Jmnts Guest

    Hum... That's why is so confusing... The text file doesn't contain anything
    with the word "All". That statment is part of a loop to transfer files to a
    server. Basically does a dir>>textfile.txt to the file then using the
    textfile.txt, transfers all existing files to another server using the move.

    By my understanding, basically means that If there's a file named "All" that
    file shouldn't be moved?? Am I wrong?
     
    Jmnts, Oct 19, 2008
    #11
  12. Jmnts

    Jmnts Guest

    Not sure about the "/i" either... According with IF /?

    "The /I switch, if specified, says to do case insensitive string compares.
    The /I switch can also be used on the string1==string2 form of IF. These
    comparisons are generic, in that if both string1 and string2 are both
    comprised of all numeric digits, then the strings are converted to numbers
    and a numeric comparison is performed."

    According with the sample only one file at the time is loaded to the
    textfile.txt, there's no comparison to make? Perhaps the sample not well
    written.
     
    Jmnts, Oct 19, 2008
    #12
  13. Jmnts

    Jmnts Guest

    Okay... After I write I saw that the /I is to compare the %%a to the "All"...
    LOL
    Not sure why the "All" but...
     
    Jmnts, Oct 19, 2008
    #13
  14. Jmnts

    Al Dunbar Guest

    To know why the writer of that script included that you would likely need to
    ask that person. Given that the text file contains a list of fully qualified
    path names, it seems unlikely that any of them would ever be equal to "All".
    Perhaps he did this because he wanted to use a compound statement, and did
    not realize you only need the parentheses. Alternately, it could be that the
    script is not perfect.

    Where did this script come from, by the way?

    /Al
     
    Al Dunbar, Oct 20, 2008
    #14
  15. Jmnts

    Jmnts Guest

    The file.txt doesn't contain the fully qualified path names, it only contains
    the name of the files to be transfered... That's why it's hard to hunderstand
    the "All" unless the variable %%a must not be equal to a file named "All".

    Not sure where (I don't remember the URL) but a google search shows
    something similar at

    http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_22779605.html

    Again, in this is example everything is clear, except the "All" thing... The
    %%a must not be equal to "All", is "All"=Null? or Empty string or a space...
     
    Jmnts, Oct 20, 2008
    #15
  16. Jmnts

    Al Dunbar Guest

    Again, if there were a purpose for this, the person most likely aware what
    the intent was would be the person who wrote the script.
    When you say "not sure where", is this your response to my question "Where
    did this script come from?". Are you saying that you do not know where the
    script came from, but that, at the same time, and while not being sure what
    it does or is intended to do, you are running it in your environment? For
    some other purpose?

    If so, instead of running an unknown script that does not do what you want
    and trying to modify it so it does, why don't you decide what it is you want
    to do, and write a script to do that?
    No, that part is perfectly clear. In this case the text file contains a list
    of account names, not files. And, like the post says that this filters out
    "All" and "Administrator". The problem is that he wants it to filter out any
    account whose name starts with "admin"
    No, you are reading far too much into this. Sometimes a cigar is just a
    cigar, but in scripting "All" is *always* just an "All.

    As I hinted above, I am now confused as to your purpose. What is it you want
    to do:

    a) understand what this script does;
    b) understand what it is supposed to do this; or:
    c) change it so it does something else?

    /Al
     
    Al Dunbar, Oct 20, 2008
    #16
  17. Jmnts

    Jmnts Guest

    No, you are reading far too much into this. Sometimes a cigar is just a

    Correct. All is just All eheheh, nothing to worrie about... I just like to
    know exactly what I'm doing and when things don't make sence tome I like to
    try to understand it.

    Thank you.
     
    Jmnts, Oct 20, 2008
    #17
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.