将所有内容放在除FOR之外的其他行上很容易。你只需要使用 动态%TIME%和%DATE%变量,而不是时间和日期命令
@echo off
echo %time% "--" %date% -I- Purging
FORFILES /P "D:\Logs" /M *.log /D -90 /C "cmd /c echo @file @fdate"
rem FORFILES that will purge the files
如果您还想要的文件名出现在同一行,那么你可以使用一个临时像EitanT建议的那样变化。但是这限制了文件的数量以适应最大8191变量大小。要处理无限数量的文件,您可以使用SET/P代替。似乎FOR/F声明似乎不是必要的,但有一个引用问题,如果没有它,我就无法解决。
@echo off
<nul (
set/p="%time% -- %date% -I- Purging "
for /f "delims=" %%A in (
'FORFILES /P "D:\Logs" /M *.log /D -90 /C "cmd /c echo @file @fdate"'
) do set/p="%%A "
)
rem FORFILES that will purge the files
没有理由不在清除文件的同时列出它们。由于FORFILES速度很慢,因此在相同的命令中进行清除和列表会更有效率。
@echo off
<nul (
set/p="%time% -- %date% -I- Purging "
for /f "delims=" %%A in (
'FORFILES /P "D:\Logs" /M *.log /D -90 /C "cmd /c del @path&echo @file @fdate"'
) do set/p="%%A "
)
更新2015年1月6日
我想出了一个解决方案,而无需使用FOR/F。我使用0x22将引号中的SET/P提示符括起来,并使用FINDSTR消除FORFILES在请求输出之前写入的空行。
@echo off
<nul (
set/p="%time% -- %date% -I- Purging "
forfiles /p "d:\logs" /m *.log /d -90 /c "cmd /c del @path&set/[email protected] @fdate 0x5e0x22"'|findstr .
)
谢谢,但我需要用%% CMD_STR %%替换%CMD_STR%并在清除之前添加另一个'echo'以使其工作。 – TomaszRykala 2012-08-14 12:38:30
这样做。谢谢。 – TomaszRykala 2012-08-14 12:41:03
@TomaszRykala很高兴帮助。为什么'%%'? – 2012-08-14 12:44:10