使用Windows DEL命令保护删除带.tmp扩展名的编号文件

时间:2014-05-28 15:05:18

标签: windows batch-file command delete-file

我有很多文件,例如11360.tmp3165.tmp以及我website的子目录中的许多文件......我想要获取的只是删除编号为扩展.tmp所以我有这样的网络结构:

www\news\1240\1240.tmp
www\news\1240\topic.tmp
www\news\1240\1240.bot
www\news\1240\1240.php
www\news\1240\1240.bot
www\news\1240\comm.txt
www\news\1240\true.txt

www\news\15640\15640.tmp
www\news\15640\topic.tmp
www\news\15640\15640.bot
www\news\15640\15640.php
www\news\15640\15640.bot
www\news\15640\comm.txt
www\news\15640\true.txt

文件夹新闻包含许多编号包含编号文件xxxxx.tmp

的目录

我只想删除xxxxx.tmptopic.tmp和所有其他文件...

del /S www\news\ *.tmp,不包括topic.tmp

怎么办?任何帮助表示赞赏!

3 个答案:

答案 0 :(得分:1)

@echo off
for /f "eol=: delims=" %%F in (
  'dir /b /s /a-d www\news\* ^| findstr "\\[0-9][0-9]*\.[^.]*$"'
) do del "%%F"

这将删除名称由数字组成的所有文件,后跟一个扩展名。例如,它将删除以下所有内容:

123.ext
123.456

不会删除以下任何内容:

text123.ext
123
123.456.ext

如果上述情况不符合您的要求,可以改进FINDSTR过滤器。

答案 1 :(得分:1)

下面的批处理文件删除以数字开头的*.tmp个文件

@echo off
setlocal EnableDelayedExpansion

set digits=0123456789

for /R "www\news" %%a in (*.tmp) do (
   set name=%%~Na
   for /F %%b in ("!name:~0,1!") do (
      if "!digits:%%b=!" neq "%digits%" del "%%a"
   )
)

如果这种方法不足以满足您的需求,可能会更精确地改变它,虽然它也会更慢......

另一种方法是删除除“topic.tmp”以外的所有* .tmp文件:

@echo off
for /R "www\news" %%a in (*.tmp) do (
   if /I "%%~Na" neq "topic" del "%%a"
)

答案 2 :(得分:0)

这是一种方法:它隐藏您要保留的文件,删除其余文件,然后再次取消隐藏文件

以递归方式搜索文件。

attrib +h www\news\topic.tmp /s
del /s www\news\*.tmp
attrib -h www\news\topic.tmp /s