加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 01:42:19 所属栏目:Windows 来源:网络整理
导读:我想删除当前目录中名称中不包含字符串“sample”的所有文件. 例如, test_final_1.exetest_initial_1.exetest_sample_1.exetest_sample_2.exe 我想删除名称中包含样本的文件以外的所有文件. for %i in (*.*) do if not %i == "*sample*" del /f /q %iIs the
我想删除当前目录中名称中不包含字符串“sample”的所有文件.

例如,

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

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

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?

谢谢.

最简单的方法是使用带有/ V选项的FIND或FINDSTR来查找不包含字符串的名称,使用/ I选项进行不区分大小写的搜索.切换到FOR / F并将DIR的管道结果发送到FIND.
for /f "eol=: delims=" %F in ('dir /b /a-d * ^| find /v /i "sample"') do del "%F"

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读