Computing.Net > Forums > Disk Operating System > Errorlevel and Echo Command in .bat file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Errorlevel and Echo Command in .bat file

Reply to Message Icon

Name: Renaissance Man
Date: July 4, 2002 at 10:04:36 Pacific
Comment:

In a test for ansi.sys I used this batch file:

@echo off
mem /c|find "ANSI">nul
cls
::SET 1
if errorlevel 2 echo (if 2) 2 or greater
if errorlevel 1 echo (if 1) 1 or greater
if errorlevel 0 echo (if 0) 0 or greater
::SET 2
if not errorlevel 3 echo (if not 3) 2 or less
if not errorlevel 2 echo (in not 2) 1 or less
if not errorlevel 1 echo (if not 1) 0
::SET 3
if errorlevel 2 if not errorlevel 3 echo (if 2 not 3) 2
if errorlevel 1 if not errorlevel 2 echo (if 1 not 2) 1
if errorlevel 0 if not errorlevel 1 echo (if 0 not 1) 0
::SET 4
if errorlevel 2 if not errorlevel 3 echo (if 2 not 3) 2 > T2
if errorlevel 1 if not errorlevel 2 echo (if 1 not 2) 1 > T1
if errorlevel 0 if not errorlevel 1 echo (if 0 not 1) 0 > T0

Ansi.sys is NOT present:

SET 1 Returns
(if 1) 1 or greater
(if 0) 0 or greater
SET 2 Returns
(in not 3) 2 or less
(in not 2) 1 or less
SET 3 Returns
(if 1 not 2) 1

So far, so good. BUT

SET 4 Returns three files:
T0 0 bytes
T1 17 bytes [containing (if 1 not 2) 1]
T2 0 bytes

Since this does not work as I want, in the real batch file where I need to test for ansi.sys, I create a file called “ansiyes,” and then delete it if it ansi.sys is not present (if errorlevel 1 del c:\ansiyes).

Since I have a workaround, this is not a problem, but why are the 0 byte files created? Why does the echo to screen work, but not echo to file?
=============================================



Sponsored Link
Ads by Google

Response Number 1
Name: Secret_Doom
Date: July 4, 2002 at 13:14:09 Pacific
Reply:

Hmmm... I don't see the problem you see. The batch seems to work as intended, to me.

Note that if you redirect the output of a IF check string, and if that IF returns a FALSE value (so, the cmd after ir won't be executed), it will create a zero-byte file.

if not errorlevel=0 echo notpossible > out

The following line will always return a false value, and it is supposed to create a zero-byte file named "out".

Anyway, if you want to check if ANSI.SYS is loaded or not, it's not necessary to create a file. Here are some ways:

MEM/c |FIND.exe " ANSI " > nul
if errorlevel=1 goto no_ansi
goto yes_ansi

MEM/c |FIND.exe " ANSI " > nul
if errorlevel=1 echo Ansi not loaded.
if not errorlevel=1 echo Ansi loaded.

:: In this example, you would have
:: to add a :EOF label to the end of
:: the file
MEM/c |FIND.exe " ANSI " > nul
if errorlevel=1 for %%? in (echo goto:eof) do %%? Error - ANSI.SYS not loaded. Cannot proceed.

See ya, RMan!

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br


0

Response Number 2
Name: Renaissance Man
Date: July 5, 2002 at 07:23:40 Pacific
Reply:

I guess I don’t understand why

The following line will always return a false value, and it is supposed to create a zero-byte file named “out”.
if not errorlevel=0 echo notpossible > out

If I used the command if [not] errorlevel X del filename I would not expect filename to be deleted if the condition were false. Since the delete command is not acted upon if errorlevel returns a false value, why is echo acted upon?


0

Response Number 3
Name: Secret_Doom
Date: July 5, 2002 at 16:19:20 Pacific
Reply:

The ECHO command is not executed (if it was, the file would contain the string ECHOed).

I think the zero-byte file is created because you're redirecting the line, no matter if there is an output or not. I think it's something like this: "well, there is an redirect simbol, so create the file. Now, put the output on the file". Since there's no output, the file is empty.

Anyway, does it really matter why it happens?

It happens, that's for sure...

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br


0

Response Number 4
Name: Renaissance Man
Date: July 5, 2002 at 16:24:33 Pacific
Reply:

Thanks for the info. And I had a good laugh, too!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: Errorlevel and Echo Command in .bat file

Find and Replace with a BAT file?!!!! www.computing.net/answers/dos/find-and-replace-with-a-bat-file/3291.html

In BAT file: test if Windows running? www.computing.net/answers/dos/in-bat-file-test-if-windows-running/10703.html

script in .bat file www.computing.net/answers/dos/script-in-bat-file/12893.html