Exclude a folder inside temp when executing a del

2020-04-10 23:31发布

问题:

What do I add to my DEL batch file if I want to exclude a folder inside a folder I want to delete?

I have this code to delete my all the contents of the temp folder

DEL /F /Q C:\temp

Now, I want to exclude a folder named imptfolder inside. This shouldn't be deleted whether it exists or not inside the temp folder. How do I do this?

回答1:

Here's some batch script code that'll do what you're asking.

for /d %%I in (c:\temp\*) do (
    if /i not "%%~nxI" equ "imptfolder" rmdir /q /s "%%~I"
)
del /q c:\temp\*

If you're just entering these commands from the console, use single percents rather than double.

cd /d c:\temp
for /d %I in (*) do @(if /i not "%~I" equ "imptfolder" rmdir /q /s "%~I")
del /q *