Remove all files and subfolders but NOT the root folder methods

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Remove all files and subfolders but NOT the root folder methods

Post by MigrationUser »

13 Jan 2011 10:53
allal

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

Code: Select all

@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 .

Code: Select all

@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

Code: Select all

@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)

----------------------------

#2 08 Apr 2011 10:13
arash

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)

----------------------------

#3 11 Apr 2011 17:10
arash

I don't Completely understand this piece of code

Code: Select all

pushd %1
for /d %%G IN (.) do (rd /s /q "%%G"
del /q /f "%%G"
)
popd
Could somebody explain that?
Thanks

----------------------------

#4 26 Apr 2011 19:41
allal

Code: Select all

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

Code: Select all

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

method #4 :

Code: Select all

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

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

#5 30 Sep 2011 13:15
Drewfus

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

Code: Select all

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.

----------------------------

#6 19 Jul 2017 20:13
Simon Sheppard

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 expect 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.
Post Reply