How to create the boolean AND condition in a . bat code?

Asked

Viewed 166 times

2

I have a simple program that deletes certain files if they exist, but I would like to improve it using a boolean condition in case the two files do not exist. Is it possible simply? I found answers in English but all are very old (2010 or older) and involve commands that are unknown and are confusing, create variables, etc.

Part of the code I currently use works perfectly:

echo. & echo "Deletar WoWCombatLog.txt" & echo.
    
IF NOT EXIST "C:\Program Files (x86)\World of Warcraft\_retail_\Logs\WoWCombatLog.txt" (
        echo "CombatLog não encontrado"
    )

How I’d like it to work (no reason to post all the code):

echo. & echo "Deletar WoWCombatLog.txt" & echo.
        
IF NOT EXIST "C:\Program Files (x86)\World of Warcraft\_retail_\Logs\WoWCombatLog.txt" AND NOT EXIST "C:\Program Files (x86)\World of Warcraft\_retail_\Logs\WoWCombatLog-split*.txt"  (
            echo "CombatLog não encontrado"

2 answers

2


If the two conditions have to be true, nestle one in the other.

IF NOT EXIST "C:\Program Files (x86)\World of Warcraft\_retail_\Logs\WoWCombatLog.txt" (
    IF NOT EXIST "C:\Program Files (x86)\World of Warcraft\_retail_\Logs\WoWCombatLog-split*.txt" (
        echo "CombatLog não encontrado"
    )
)

0

  • Use dir with multiple files and use the errorlevel
set "_wow_path=%ProgramFiles(x86)%\World of Warcraft\_retail_\Logs"

2>nul >nul dir /b "%_wow_path%\WoWCombatLog.txt" "%_wow_path%\WoWCombatLog-split*.txt"
if %errorlevel% == 0 echo\Arquivo(s) encontrado(s)!
  • Or en bloc using operators (dir arq1.txt arq*.txt) && listed 1 (or 2) || didn’t list anything:
set "_wow_path=%ProgramFiles(x86)%\World of Warcraft\_retail_\Logs"

( dir /b "%_wow_path%\WoWCombatLog.txt" "%_wow_path%\WoWCombatLog-split*.txt"
) 2>nul >nul && echo\Arquivo(s) encontrado(s)! || echo\Arquivos nao encontrados!!

1. Sets the path to the folder of the files you want to check:

set "_wow_path=%ProgramFiles(x86)%\World of Warcraft\_retail_\Logs"

2. Run a command to list, such as dir arquivo1 arquivo2

 dir /b "%_wow_path%\WoWCombatLogX.txt" "%_wow_path%\WoWCombatLog-split*X.txt"

3. Limit the above execution in a block (dir arquivo1 arquivo2) 2>nul >nul and delete any error(s) of arquivo não encontrado:

( dir /b "%_wow_path%\WoWCombatLog.txt" "%_wow_path%\WoWCombatLog-split*.txt"
) 2>nul >nul && Arquivo(s) sim encontrado(s)! || echo\Arquivos nao encontrados!!
  • Note: 1 Or use a single line in the block:
( dir /b "%_wow_path%\WoWCombatLog.txt" "%_wow_path%\WoWCombatLog-split*.txt" ) 2>nul >nul && echo\Arquivo(s) encontrado(s) || echo\Arquivos nao encontrados!!
  • Note: 2 you can pass by parameter more than one file to the command dir *dir /b arq1.log arq2.txt arq3.dat..., if only one file is listed, dir returns return 0 and if none is listed, return non 0.

4 Use the operator && to treat return 0 and the operator || to treat return non 0:


rem :: lógica aplicada na listagem de 2(+) arquivo(s) ⁄⁄ mensagens ::

rem :: Não listou os 2 arquivos  ==  ||  Arquivos nao encontrados
rem :: Sim listou os 2 arquivos  ==  &&  Arquivo(s) encontrado(s)!   
rem :: Não listou 1 dos arquivos ==  &&  Arquivo(s) encontrado(s)! 

rem :: Output/saídas nas listagem de 2(+) arquivo(s) ⁄⁄ mensagens ::
rem :: Arquivo(s) nao encontrado(s) (dir) || mensagem 
rem :: Nao exist True  ==  WoWCombatLog-split 
rem :: Nao exist True  ==  WoWCombatLog-split*.txt
rem ::    Return "1"   ==  Arquivos nao encontrados

rem :: Um Arquivo encontrado (dir) && mensagem 
rem :: Nao exist False  ==  WoWCombatLog-split 
rem :: Nao exist False  ==  WoWCombatLog-split*.txt
rem ::     Return "0"   ==  Arquivo(s) encontrado(s)

rem :: Um ou (*) Arquivos encontrados (dir) && mensagem 
rem :: Nao exist True   ==  WoWCombatLog-split 
rem :: Nao exist False  ==  WoWCombatLog-split*.txt
rem ::     Return "1"   ==  Arquivo(s) encontrado(s)

rem :: Um ou (*) Arquivos encontrados (dir) && mensagem 
rem :: Nao exist False  ==  WoWCombatLog-split 
rem :: Nao exist True   ==  WoWCombatLog-split*.txt
rem ::     Return "1"   ==  Arquivo(s) encontrado(s)

rem :: para apagar os dois arquivos somente se existirem: 

set "_wow_path_files=%ProgramFiles(x86)%\World of Warcraft\_retail_\Logs"

2>nul >nul dir /b "%_wow_path%\WoWCombatLog.txt" "%_wow_path%\WoWCombatLog-split*.txt"
if %errorlevel% equ 0 del /q /f "%_wow_path%\WoWCombatLog.txt" "%_wow_path%\WoWCombatLog-split*.txt"

In short:

  • Only if nay exist both (or more: ...split*.txt)
cd /d "C:\Program Files (x86)\World of Warcraft\_retail_\Logs"
2>nul >nul dir WoWCombatLog-split*.txt WoWCombatLog.txt || echo\Arquivos Nao Encontrados
  • Results:
Arquivos Nao Encontrados
  • Only if there is(m) 1 or more // ...split*.txt
cd /d "C:\Program Files (x86)\World of Warcraft\_retail_\Logs"
2>nul >nul dir WoWCombatLog-split*.txt WoWCombatLog.txt && echo\Arquivo(s) Encontrado(s)
  • Results:
Arquivo(s) Encontrado(s)
  • The two above options in a bat:
@echo off 

cd /d "C:\Program Files (x86)\World of Warcraft\_retail_\Logs"
>nul 2>&1 dir WoWCombatLog-split*.txt WoWCombatLogx.txt && <nul ^
echo\Arquivo(s) Encontrado(s) || echo\Arquivos nao Encontrados
  • Deleting these files in case two or more exist:
@echo off 

cd /d "C:\Program Files (x86)\World of Warcraft\_retail_\Logs"
>nul 2>&1 dir/b WoWCombatLog-split*.txt WoWCombatLog.txt && <nul ^
    del /q /f WoWCombatLog-split*.txt WoWCombatLog.txt >nul 2>&1 ^
        || echo\Arquivos nao Encontrados 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.