Reputation: 187
I would like to delete everything in a folder but some files, let's say fileA.txt
, fileB.exe
and fileC.dll
. How do I process ?
Here is what I tried, but it does not loop over all files and stops at the first, and does not even delete it :
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
)
Upvotes: 0
Views: 237
Reputation: 67296
@echo off
setlocal EnableDelayedExpansion
set exclude=/fileA.txt/fileB.exe/fileC.dll/
for /R %%I in (*.*) do (
if "!exclude:/%%~nxI/=!" equ "%exclude%" del "%%I"
)
Upvotes: 1
Reputation: 70971
Generate the list, remove from it not desired files, delete the rest
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"
For a longer list of exclusions, it is better to create a text file with the list of files to exclude and then use /g
switch of findstr
to indicate the strings.
Upvotes: 3
Reputation: 41297
Here is one way:
@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"
Upvotes: 3