删除文件夹中的所有文件,但跳过包含特定字符串的文件 [英] Delete all files in a folder but skip files that cointain certain string
问题描述
我正在尝试清理临时文件,但我想跳过一些具有特定名称的文件.不管扩展名是什么.
I'm trying to clean the temp files, but I want skip some files that cointain a certain name. No matter the extension it is.
尝试过:
@echo off
setlocal EnableDelayedExpansion
for /f "eol=: delims=" %F in ('dir "%windir%/temp" /b /a-d * ') do find "TESTE" %F > nul || del "%F"
pause
想要删除名称中包含 TESTE
的所有文件.
Wanted all files that cointains TESTE
in name got skipped from deletation.
但是我的脚本甚至无法运行.
But my script not even run.
有人可以向我解释什么地方不对吗?
Can someone explain me what is wrong?
推荐答案
并不确定是否要删除名称或内容包含特定字符串的文件;无论如何,这是针对前者的:
It is not absolutely clear whether you want to exclude files from deletion whose names or whose contents include a certain string; anyway, this is for the former:
pushd "%WinDir%\TEMP" && (
for /F "delims= eol=|" %%F in ('
dir /B /A:-D "*.*" ^| findstr /V /I /C:"TESTE"
') do (
ECHO del /F "%%F"
)
popd
)
对输出满意后,请删除大写的 ECHO
命令.
Once you are satisfied with the output, remove the upper-case ECHO
command.
上面的脚本也不会删除扩展名包含给定字符串的文件.但是,如果您只想考虑基本名称,则可能要改用以下代码:
The above script would also not delete files whose extensions contain the given string. But if you want to regard only the base name, you might want to use this code instead:
pushd "%WinDir%\TEMP" && (
for /F "delims= eol=|" %%F in ('
dir /B /A:-D "*.*"
') do (
set "NAME=%%~nF"
setlocal EnableDelayedExpansion
if /I "!NAME:TESTE=!"=="!NAME!" (
endlocal
ECHO del /F "%%F"
) else endlocal
)
popd
)
这篇关于删除文件夹中的所有文件,但跳过包含特定字符串的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!