將所有內容放在除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