This code is used to remove all files and folders from the c:\test directory except "FolderA"
@echo off
pushd "C:\test" || exit /B 1
for /D %%D in ("*") do (
if /I not "%%~nxD"== "FolderA" rd /S /Q "%%~D"
)
for %%F in ("*") do (
del "%%~F"
)
popd
In case I want to omit more than one folder, how could I add it? i mean delete everything except FolderA FolderB and Folder C (Names can contain spaces)
this doesn't work
@echo off
pushd "C:\test" || exit /B 1
for /D %%D in ("*") do (
if /I not "%%~nxD"== "FolderA" "FolderB" "Folder C" rd /S /Q "%%~D"
)
for %%F in ("*") do (
del "%%~F"
)
popd
You can produce a logical AND if you write if-clauses in a row:
if /I not "%%~nxD" == "FolderA" if /I not "%%~nxD" == "FolderB" if /I not "%%~nxD" == "FolderC" rd /S /Q "%%~D"