1

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!

2 Answers 2

1

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
)
Sign up to request clarification or add additional context in comments.

Comments

1
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
)  

1 Comment

I tried with Parens as suggested but no luck :( Echo statement is getting printed for every file..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.