Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 > T0Ansi.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) 1So far, so good. BUT
SET 4 Returns three files:
T0 0 bytes
T1 17 bytes [containing (if 1 not 2) 1]
T2 0 bytesSince 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?
=============================================

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_ansiMEM/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

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

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

![]() |
![]() |
![]() |

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