我想删除文件夹中的所有内容,但删除一些文件,例如fileA.txt
,fileB.exe
和fileC.dll
。我该如何处理?
这是我尝试过的,但它不会遍历所有文件并在第一个文件中停止,甚至不删除它:
for /R %%I in (*.*) do (
if "%%~nxI" == "fileA.txt" goto cd1
if "%%~nxI" == "fileB.exe" goto cd1
if "%%~nxI" == "fileC.dll" goto cd1
goto cd2
:cd1
goto fin
:cd2
echo HERE WE MUST DEL THE FILE !
goto fin
:fin
echo Done
)
答案 0 :(得分:3)
这是一种方式:
@echo off
call :hide +h
del *.*?
call :hide -h
echo done.
goto :EOF
:hide
for %%a in (
"fileA.txt"
"fileB.exe"
"fileC.dll"
) do attrib %1 "%%~a"
答案 1 :(得分:3)
生成列表,从中删除不需要的文件,删除其余文件
for /f "delims=" %%a in (
'dir /s /b /a-d * ^| findstr /v /i /e /c:"\\filea.txt" /c:"\\fileb.exe" /c:"\\filec.dll"'
) do del "%%a"
对于较长的排除列表,最好创建一个包含要排除的文件列表的文本文件,然后使用/g
切换findstr
来指示字符串。
答案 2 :(得分:1)
@echo off
setlocal EnableDelayedExpansion
set exclude=/fileA.txt/fileB.exe/fileC.dll/
for /R %%I in (*.*) do (
if "!exclude:/%%~nxI/=!" equ "%exclude%" del "%%I"
)