1

我是批处理编程的新手。我正在尝试在我的一个批处理脚本中使用 IF 条件。代码看起来像这样。

: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

现在的问题是,即使文件的年龄超过 40 年,我也会打印文件。这实际上不应该发生。

请在这方面帮助我..谢谢!

4

2 回答 2

1

要么把它放在一行:

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

或使用块分隔符:

if %age% LSS 40 (
    echo.%file% is %age% days old
)
于 2012-10-16T07:06:21.210 回答
1
if %age% LSS 40 
echo.%file% is %age% days old  

被解释为具有空主体(第一行)和无条件echo(第二行)的条件表达式。您需要将它们放在一行中:

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

或使用括号创建块(但左括号必须与 位于同一行if

if %age% LSS 40 (
   echo.%file% is %age% days old
)  
于 2012-10-16T07:07:37.493 回答