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

问题描述 投票:0回答:3

我有很多文件,比如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.tmp,不包括topic.tmp和所有其他文件......

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

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

windows batch-file command delete-file
3个回答
1
投票

下面的批处理文件删除以digit开头的*.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"
)

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过滤器。


0
投票

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

它以递归方式搜索文件。

attrib +h www\news\topic.tmp /s
del /s www\news\*.tmp
attrib -h www\news\topic.tmp /s
© www.soinside.com 2019 - 2024. All rights reserved.