DOS批处理FOR循环删除不包含字符串的文件

Neo*_*ash 6 for-loop batch-file

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

例如,

test_final_1.exe
test_initial_1.exe
test_sample_1.exe
test_sample_2.exe
Run Code Online (Sandbox Code Playgroud)

我想删除名称中包含样本的文件以外的所有文件.

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?
Run Code Online (Sandbox Code Playgroud)

谢谢.

dbe*_*ham 6

最简单的方法是使用FIND或FINDSTR /V选项来查找不包含字符串的名称,以及/I选项以进行不区分大小写的搜索.切换到FOR /F与管结果DIRFIND.

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

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