将 x 个最新文件保存在特定文件夹中的批处理文件

Batch file that keeps the x latest files in a specific folder

如何指定特定文件夹?例如 "c:\temp with spaces"?

此命令有效,但仅在与执行它的文件夹相同的文件夹中:

for /f "skip=7 eol=: delims=" %%F in ('dir /b /o-d *.txt') do @del "%%F"

但是我怎样才能指定某个文件夹呢?这不起作用:

for /f "skip=3 eol=: delims=" %%F in ('dir /b /o-d "c:\temp\ with spaces*.txt"') do @del "%%F"

它会在当前目录中寻找txt文件,如果.bat文件在另一个文件夹中,则不起作用

将不胜感激

正如您通过 运行 手动看到的 dir /b 未指定 /s 仅列出没有完整路径的文件名,因此 @del "%%F" 尝试从当前文件夹中删除文件.

也只需在 del 中指定完整路径:

@del "c:\temp\%%F"

只需切换到所需的文件夹

pushd "x:\some folder\with\files" && (
    for /f "skip=7 eol=: delims=" %%F in ('dir /b /o-d *.txt') do @del "%%F"
    popd
)