user1749169
user1749169

Reputation: 11

Using IF Statement in a Batch Script

I'm new to Batch Programming.I'm trying to use an IF Condition in one of my Batch Script.The Code looks like this.

:rmfile
:: removes the file based on it's age.
::                     
SETLOCAL
set file=%~1
set age=%~2
set thrshld_days=40
if %age% LSS 40 
echo.%file% is %age% days old 
EXIT /b

Now the Problem is that even if the age of a file is more than 40 i'm getting the file printed.which actually should not happen.

Please Help me in this regard..Thanks!

Upvotes: 1

Views: 297

Answers (2)

wmz
wmz

Reputation: 3685

if %age% LSS 40 
echo.%file% is %age% days old  

is interpreted as conditional expression with empty body (first line) and unconditional echo (second line). You need to either put them on one line:

if %age% LSS 40 echo.%file% is %age% days old   

or use parens to create block (but the opening bracket must be on the same line as if):

if %age% LSS 40 (
   echo.%file% is %age% days old
)  

Upvotes: 1

Christian Hayter
Christian Hayter

Reputation: 31071

Either put it on one line:

if %age% LSS 40 echo.%file% is %age% days old

or use block delimiters:

if %age% LSS 40 (
    echo.%file% is %age% days old
)

Upvotes: 1

Related Questions