批处理文件删除选定的文件

发布于 2024-11-02 17:22:46 字数 114 浏览 1 评论 0原文

在大多数现代系统中,系统会自动创建不必要的额外文件。如何创建一个 .bat 文件,利用清单来删除不在其中的任何文件?

这对于删除我创建的自定义文件中自动生成的备份文件、绘图日志、错误日志等非常有用。

In most modern systems, the system will automatically create extra files that are unnecessary. How can I create a .bat file that makes use of a checklist to delete any file that isn't on it?

This would be useful for removing automatically generated backup files, plot logs, error logs, etc. in the customization files I have created.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满意归宿 2024-11-09 17:22:46

试试这个,确保将 SEARCHDIR 设置为真实目录,并使用要保留的文件填充 keep.txt,每行 1 个文件,使用包括目录的完整路径。如果您不想使用该目录,请调整 DIR /S /B 命令,使其输出的格式与 keep.txt 中的格式相同

@echo off
setlocal enabledelayedexpansion
set KEEPFILES=keep.txt
set SEARCHDIR=.\test_folder
SET /a KEEPTHISFILE=2

FOR /f "tokens=*" %%A IN ( 'DIR /S /B %SEARCHDIR%\*' ) DO (
SET /a KEEPTHISFILE=0
for /f "tokens=1,* delims=¶" %%B in ( '"type %KEEPFILES%"') do (
    if "%%A"=="%%B" SET /a KEEPTHISFILE=1       
)
if "!KEEPTHISFILE!"=="0" (
    echo "deleting %%A"
    del /f %%A  
)
)

Try this, make sure to set SEARCHDIR to the real dir, and populate keep.txt with the files you want to keep, 1 file per line, use the full path including the dir. If you don't want to use the dir, adjust the DIR /S /B command so it outputs the same format as what is in your keep.txt

@echo off
setlocal enabledelayedexpansion
set KEEPFILES=keep.txt
set SEARCHDIR=.\test_folder
SET /a KEEPTHISFILE=2

FOR /f "tokens=*" %%A IN ( 'DIR /S /B %SEARCHDIR%\*' ) DO (
SET /a KEEPTHISFILE=0
for /f "tokens=1,* delims=¶" %%B in ( '"type %KEEPFILES%"') do (
    if "%%A"=="%%B" SET /a KEEPTHISFILE=1       
)
if "!KEEPTHISFILE!"=="0" (
    echo "deleting %%A"
    del /f %%A  
)
)
半山落雨半山空 2024-11-09 17:22:46

创建一个名为“deletestuff.bat”的文件(或者任何您想要的名称,只要它是 .bat)。接下来,填写:

rm *.tmp
rm *.blah

其中 .tmp.blah 是您要删除的文件的扩展名(它们可能是 .log,例如)。首先通过将文件示例复制到新文件夹并在那里运行新的 .bat 进行测试,以确保它不会删除重要的内容。

Make a file called 'deletestuff.bat' (or whatever you want it to be called, as long as it is a .bat). Next, fill it with:

rm *.tmp
rm *.blah

Where .tmp, and .blah are the extensions of the files that you would like to delete (they might be .log, for instance). Test it first by copying a sample of files into a new folder and running the new .bat there, just to make sure it doesn't delete important things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文