Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch Files third "If NOT" statement not reached

Hello stackoverflow community,

I am writing a batch file to do some automatic computer maintenance and have included several antivirus applications. For some reason, the third "if not" statement is never reached.

:AV
REM MSE
if not '%MSE%'=='' (
Echo Scanning for viruses using Microsoft Security Essentials.
Echo.
%MSE% -Scan -ScanType 1
Echo.
GOTO Defrag
)

REM AVG
if not '%AVG%'=='' (
Echo Scanning for viruses using AVG.
Echo.
%AVG% /COMP /QT /TRASH
Echo.
GOTO Defrag
)

REM NOD32
if not '%NOD32%'==''(
Echo Scanning for viruses using NOD32.
Echo.
if '%NOD32%'=='' GOTO NOD32NotFound
%NOD32% /aind /auto /log-file="%userprofile%\Desktop\Scan_Results.txt"
Echo.
GOTO Defrag
)

REM If all else fails...
GOTO AVNotFound

Currently, there are three blocks of codes, one for each antivirus program. Each block of code is executed only when the variable %AVG% %MSE% or %NOD32% is not empty, meaning they point to a valid file. I assign the variables using the code:

if exist "%programfiles(x86)%\AVG\AVG2012\avgscana.exe" set AVG="%programfiles(x86)%\AVG\AVG2012\avgscana.exe"

All three blocks of code works perfectly, nothing is wrong with the coding. The problem is that whatever the third block is, it never executes. So in the current example, the blocks of code go in the order of MSE, AVG, and NOD32. NOD32's block of code does not execute because it's the third block. Conversely, if I cut and paste the blocks into another order with AVG's block of code being the third block, it would not execute.

Any ideas?

Any suggestions?

Edited for clarification.

like image 835
lbrtdy Avatar asked Nov 25 '11 00:11

lbrtdy


People also ask

What is %% K in batch file?

So %%k refers to the value of the 3rd token, which is what is returned.

How do you write not equal to in batch file?

According to this, ! ==! is the not-equal string operator.

Can you use if statements in batch files?

One of the common uses for the 'if' statement in Batch Script is for checking variables which are set in Batch Script itself. The evaluation of the 'if' statement can be done for both strings and numbers.


2 Answers

You're missing a space in the line:

if not '%NOD32%'==''(

Try:

if not '%NOD32%'=='' (

When I tried the script this line caused a failure. After changing the line, it worked.

like image 59
Hand-E-Food Avatar answered Oct 16 '22 19:10

Hand-E-Food


Are the variables %MSE%, %AVG% or %NOD32% batch files? If yes, you will need to invoke them using "call" instead (for example call %AVG%)

If you call a batch file from another, the first one will exit after executing the 2nd one, unless it is called with "call".

like image 29
JohnD Avatar answered Oct 16 '22 20:10

JohnD