Results 1 to 9 of 9
  1. #1
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    How to set errorlevel 2

    I think I'm probably on a loser here, but does anyone know how to set errorlevel 2 using the same internal command in all of NT4, W2K, WXP and whatever the correct TLA for Windows Server 2003 is? The command must have no other effect.

    To encourage you, I would point out that
    VER 1>NUL sets errorlevel 0
    and
    MD; 2>NUL sets errorlevel 1

    Both
    SET ERRORLEVEL=2
    and
    SET /A ERRORLEVEL=2
    will be disqualified, since they are not setting the variable internal to CMD.EXE, which is tested by commands along the lines of
    IF ERRORLEVEL 2 ECHO Errorlevel was 2 or greater

    All those people who don't have a clue what I'm talking about can count themselves lucky...!
    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  2. #2
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    Cincinnati, Ohio, USA
    Posts
    7,089
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to set errorlevel 2

    So the problem is that you do not want to use this as an environmental variable? The following code produces what you want:
    <pre>SET ERRORLEVEL=2
    ECHO %errorlevel%
    IF %errorlevel% == 2 echo ERRORLEVEL was equal to or greater than 2</pre>

    But I suspect that you are aiming for the reserved internal errorlevel value? Perhaps this explanation will be of some assistance, although the example I was looking at does not apply to NT 4. In NT 4 you can obtain ERRORLEVEL 2 by issuing the DEL command for files that don't exist; every other MS operating system returns a 0 except for NT 4.

    I think perhaps the best way to accomplish what you seek is to use the exit code from another child process, using the EXIT command. This will return the current ERRORLEVEL to the parent process.
    -Mark

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: How to set errorlevel 2

    Does running a batch file with the single line
    <font face="Georgia">EXIT /b 2</font face=georgia>
    work on all versions of Windows. It certainly does the right thing on my W2K Pro system.

    StuartR

  4. #4
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: How to set errorlevel 2

    Replying to WyllyWylly and Stuart in one message (saves electrons!)

    Yes, I really need the errorlevel 2 following an ENDLOCAL, so any created environment variable is dropped.

    Sadly NT4 gives for EXIT /B 2 "The syntax of the command is incorrect" (and errorlevel 1!!), because the /b switch was added in Windows 2000.

    Both DIR NONEXISTENTFILE and REN NONEXISTENTFILE SOMEOTHERFILENAME give errorlevel 2 in NT4, but errorlevel 1 in W2K and WXP.

    I'm trying to write a BATch Function (after the Ritchie Lawrence model) to check whether one or more files (usually executable files) can be found in either the current directory or the path...
    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: How to set errorlevel 2

    So how about
    EXIT /b 2
    followed by DIR NONEXISTENTFILE

    Guessing that the W2K system will take the exit and the NT4 will drop through to the directory command

    StuartR

  6. #6
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: How to set errorlevel 2

    When I get into work tomorrow, I'll post the test BATch file and the function it contains, which may make my requirements a trifle clearer!
    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: How to set errorlevel 2

    <img src=/S/whisper.gif border=0 alt=whisper width=29 height=17> VBScript, which runs at the command line using cscript.exe, provides an alternative batch language for these sorts of operations.

  8. #8
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: How to set errorlevel 2

    You can whisper as much as you like, but it won't change my scripting language...! <img src=/S/razz.gif border=0 alt=razz width=25 height=17> Here's the promised file:

    <pre>@echo off
    :: test BATch file for function IsInPath
    setlocal
    :: list of files to be tested
    set list=schedlog.txt %~n0.bat
    echo Files: %list%
    call :IsinPath %list%
    if not errorlevel 1 echo IsInPath function found all^
    file(s) in Path or Current Directory
    if errorlevel 1 if not errorlevel 2 echo IsInPath function^
    found one or more files NOT in Path or Current Directory
    endlocal & goto :eof
    ::
    ::
    :::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::
    :IsInPath %ListOfFiles%
    ::
    :: By: John Gray, 2003-11-24. Version 1.1
    ::
    :: Func: Determines whether one or more files (usually executable files)
    :: is/are present in the current directory or in the path.
    :: Success of this function depends on there being no spaces in the path.
    :: Errorlevel 0 is returned if all the file(s) are found.
    :: Errorlevel 1 is returned if any of the file(s) is not found,
    :: or if no filename was specified, or if the path contain a space.
    :: (I'd really like to set errorlevel 2 for the last two conditions...!)
    :: For NT4/2000/XP/2003.
    ::
    :: Args: %1 [%2] [%3]...[%n] Filename(s) to be checked (not limited to 9)
    :::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::
    setlocal ENABLEEXTENSIONS
    :: check for at least one file
    if "%1"=="" md;2>nul & endlocal & goto :EOF
    :: ensure the path does not contain a blank
    set spath=%path: =%
    if not "%spath%"=="%path%" md;2>nul & endlocal & goto :EOF
    :: check if each file exists in the current directory or in the path
    set flag=on
    :: +-- curr dir --+ +------ path ------+
    for %%a in (%*) do if not exist %%a if "%%~f$path:a"=="" set flag=
    :: set el=0 set el=1
    if defined flag (ver>nul) else (md;2>nul)
    endlocal & goto :EOF
    :::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::</pre>

    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  9. #9
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: How to set errorlevel 2

    Reporting back, I am told that
    ASSOC. 2>NUL
    will set errorlevel 2.

    It works in NT4, W2K and WXP, but sadly I cannot test it in WIndows Server 2003. Does anyone have this, pls?
    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •