2012-10-16 63 views
1

我是批量編程的新手。我嘗試在批處理腳本中使用IF條件。代碼如下所示。在批處理腳本中使用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我越來越printed.which其實不應該發生的文件。

請幫我在這方面..謝謝!

回答

1

要麼把它在一行:

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

或使用塊分隔符:

if %age% LSS 40 (
    echo.%file% is %age% days old 
) 
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 
) 
+0

我的括號嘗試如建議,但沒有運氣:(回聲陳述正在打印每個文件.. – user1749169

+0

非常感謝。它工作.. – user1749169