【问题标题】:Batch file: Loop through File names that contain a certain word and clear those folders' files IF older than X days批处理文件:遍历包含某个单词的文件名,如果超过 X 天,则清除这些文件夹的文件
【发布时间】:2017-12-08 19:20:23
【问题描述】:

我正在尝试编写一个批处理脚本来清理编译目录中的旧数据。它必须在某个头目录中搜索。在那里,它应该删除超过 7 天的文件,但仅限于名称包含“compile”一词的子文件夹中,f.i.编译335,编译336...

这是我到目前为止想出的,但它不能正常工作:

REM Remove files older than 7 days in compile directories of headDirectory
cd C:\headDirectory     
FOR /d /r . %%d IN (%Compile%\*) DO forfiles /p %%d /s /m *.* /d -7 /c "cmd /c Del @path"     

您能告诉我应该如何更改脚本吗?

编辑:

也许我的问题不够清楚。我想要这样的东西:

forfiles -p "C:\Whatever\Compile1" -s -m *.* -d -7 -c "cmd /c del @path" 
forfiles -p "C:\Whatever\CompileFoo" -s -m *.* -d -7 -c "cmd /c del @path"
forfiles -p "C:\Whatever\CompileBar" -s -m *.* -d -7 -c "cmd /c del @path" 
forfiles -p "C:\Whatever\Compile23" -s -m *.* -d -7 -c "cmd /c del @path"

但在一个循环语句中处理该目录中的所有编译文件夹,但保持其他文件夹不变。

【问题讨论】:

  • 编译变量的值是多少?
  • 它不是一个变量,我希望它找到包含“编译”一词的文件夹。
  • 好吧,您将它用作变量,因此请删除百分比符号。
  • 非常感谢,但删除百分号仍然不会产生结果。

标签: loops batch-file delete-file days


【解决方案1】:

找到了,是的

CD C:\HeadDIrectory

FOR /D /r %%G in ("*Compile*") DO forfiles -p %%G -s -m *.* -d -7 -c
"cmd /c del @path"

我用 VB 脚本调用它来让它安静下来:

Set WshShell = CreateObject("WScript.Shell" )  
WshShell.Run chr(34) & "path\mybatch.bat" & Chr(34), 0  
Set WshShell = Nothing

你们知道更优雅的保持安静的方法吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    相关资源
    最近更新 更多