变量声明:
SET /A days=180
SET /A count=0
SET cmdStr=CMD /C DEL @FILE
SET xmlDir=D:\Client Communications\XML
我已经有一个使用 FORFILES 删除 xml 和 psr 数据文件的工作 for 循环,它看起来像这样:
FOR %%G IN (.xml, .psr) DO (
FORFILES /P "%xmlDir%" /M *%%G /D -%days% /C "%cmdStr%" >nul 2>nul
IF ERRORLEVEL 1 (
ECHO No %%G files %days% days old or older were found.
) ELSE (
ECHO %%G files as old as %days% days or older have been deleted.
)
)
现在我想修改这个批处理文件来计算被 FORFILES 命令删除的文件的数量(对于每种文件类型),我相信这可以通过将 FORFILES 命令放入 FOR /F 循环来实现。我不想做一个嵌套的 for 循环,但最终创建了一个子例程。我的代码现在看起来像这样:
FOR %%G IN (.xml, .psr) DO (
CALL :process_files %%G count
IF !count!==0 (
ECHO No %%G files %days% days old or older were found.
) ELSE (
ECHO !count! %%G files as old as %days% days or older have been deleted.
)
SET !count!=0
)
PAUSE
ENDLOCAL
:process_files
FOR /F "delims=" %%G IN ('
FORFILES /P "%xmlDir%" /M *%1 /D -%days% /C "%cmdStr%"
') DO SET /A %~2+=1
EXIT /B
似乎计数没有正确增加。当我尝试回显时!count!它输出:
!count! .xml files as old as 180 days or older have been deleted.
当我尝试改变!count!到 %count% 我得到这个输出:
No .psr files 180 days old or older were found.
如果我在 :process_files 结束之前回显计数,它会输出“计数”,所以看起来它被视为字符串,而不是整数。不知道为什么会这样。