#1 13 Jan 2011 10:53

allal
Member
Registered: 10 Jan 2011
Posts: 48

Remove all files and subfolders but NOT the root folder methods

echo deleting all but the root folder:
this first method is taken from http://ss64.com/nt/del.html

@echo off
color cb
REM method #1
pushd %1
del /q  *.*
for /f "tokens=*" %%G IN ('dir /b ')  do rd /s /q "%%G"

in this second method we will lock the root folder by pushd process
so when we try to remove the root all file and subfolders inside it wil be deleted
and give an error that the root folder can not be deleted because it's a process
is using it .

@echo off
color cb
::REM method #2
pushd %1
rd /s /q %1  2>nul
popd

and this third method is very obvious and clear and don't need explanation

@echo off
color cb
::REM method #3
pushd %1
for /d %%G IN (.) do (rd /s /q "%%G"
del /q /f "%%G"
)
popd

you can drag a folder to one of those batch and it will delete everything inside it
and the dragged folder will not be deleted
it's best used in a shutdown script file against the temporary folder %temp% where you pass
this folder as parametre %1 to delete all its content and leave the folder clean and existant.

enjoy

Last edited by allal (14 Jan 2011 11:15)

Offline

#2 08 Apr 2011 10:13

arash
Member
Registered: 27 Apr 2010
Posts: 27

Re: Remove all files and subfolders but NOT the root folder methods

I tested Method #3 for %temp% folder and it worked perfectly.
Absolutely fantastic.
Good job Allal, thanks a lot.

Last edited by arash (08 Apr 2011 10:13)

Offline

#3 11 Apr 2011 17:10

arash
Member
Registered: 27 Apr 2010
Posts: 27

Re: Remove all files and subfolders but NOT the root folder methods

I don't Completely understand this piece of code

pushd %1
for /d %%G IN (.) do (rd /s /q "%%G"
del /q /f "%%G"
)
popd

Could somebody explain that?
Thanks

Offline

#4 26 Apr 2011 19:41

allal
Member
Registered: 10 Jan 2011
Posts: 48

Re: Remove all files and subfolders but NOT the root folder methods

pushd %1
for /d %%G IN (.) do (rd /s /q "%%G"
del /q /f "%%G"
)
pop

this code is a bit misleading and actually it is the same method as #2
"." means the current directory and because we are pushing the current directory
then it will not be deleted and will delete its content instead and will give the error:
The process cannot access the file because it is being used by another process.

the error will be hidden by 2>nul
so it is better written as RD /s /q "." 2>nul
and it is the same as :

rd /s /q %1  2>nul 

what i want by the method #3 is

for /D %%G IN ("%~1\*.*") do rd /s /q "%%~G" &for %%a IN ("%~1\*.*") do del /Q /F "%%~a"

method #4 :

 pushd %1 && (
 popd & RD /S /Q %1 && (
       MD %1
    )
)

or just
RD /S /Q %1
MD %1

Offline

#5 30 Sep 2011 13:15

Drewfus
Member
From: Australia
Registered: 11 Jan 2010
Posts: 44

Re: Remove all files and subfolders but NOT the root folder methods

For files and folders under root, if folder then folder\ exists, but if file then file\ does not exist.

for /F %F in ('dir /b <RootDir>') do @if exist %F\ (rd /s /q "%F") else (del /q "%F")

You might need to alter the switches on rd and del. Also, this will not work if files are open, obviously.

Offline

#6 19 Jul 2017 20:13

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,013
Website

Re: Remove all files and subfolders but NOT the root folder methods

I have published a couple of scripts on the DelTree page below with the aim of not just deleting files and folders, but doing so as fast as possible on large folder trees. I'd be interested to see if any of you can suggest any improvements.
https://ss64.com/nt/deltree.html

The stand-alone version FastDel.cmd sets a Read-Only flag to avoid deleting itself which I expact will slow things down a little.

One alternative approach would be use Robocopy to copy an empty folder onto the one you want to empty using /MIR or /PURGE, I havent had the time to test if that would actually be faster.

Offline

Board footer

Powered by FluxBB