gpt4 book ai didi

for-loop - DOS批量FOR循环删除不包含字符串的文件

转载 作者:行者123 更新时间:2023-12-03 06:44:34 24 4
gpt4 key购买 nike

我想删除当前目录中名称中不包含字符串“sample”的所有文件。

例如,

test_final_1.exe
test_initial_1.exe
test_sample_1.exe
test_sample_2.exe

我想删除除名称中包含 sample 的文件之外的所有文件。

for %i in (*.*) do if not %i == "*sample*" del /f /q %i

Is the use of wild card character in the if condition allowed?
Does, (*.*) represent the current directory?

谢谢。

最佳答案

最简单的方法是使用 FIND 或 FINDSTR 与 /V 选项来查找不包含字符串的名称,并使用 /I 选项进行不区分大小写的搜索。切换到 FOR/F 并将 DIR 的结果通过管道传输到 FIND

for /f "eol=: delims=" %F in ('dir /b /a-d * ^| find /v /i "sample"') do del "%F"

如果在批处理文件中使用,请将 %F 更改为 %%F。

关于for-loop - DOS批量FOR循环删除不包含字符串的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10788075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com