Page 1 of 1

How to delete everything including folders in a directory with a batch file

Posted: 2015-12-19 10:12
by !
How to delete and remove everything recursively including folders in a directory (or several directories with a list of them inside an array) with a batch file.

Like this:

Code: Select all

@echo off

setlocal enabledelayedexpansion

set _folderstoclean[1]="c:myfoldera*"
set _folderstoclean[2]="c:myfolderb*"
set _folderstoclean[3]="c:myfolderc*"

for /l %%i in (1,1,3) do (
   del /q !_folderstoclean[%%i]!
   for /d %%x in (!_folderstoclean[%%i]!) do rd /s /q ^"%%x^"
)

This line:

Code: Select all

for /l %%i in (1,1,3) do (

"3" is the number of your folders in your array, in this case, we got 3 folders to clean:

Code: Select all

set _folderstoclean[1]="c:myfoldera*"
set _folderstoclean[2]="c:myfolderb*"
set _folderstoclean[3]="c:myfolderc*"

So if you had a fourth folder, you would add another line:

Code: Select all

set _folderstoclean[4]="c:myfoldera*"

...and change the 3 to 4:

Code: Select all

for /l %%i in (1,1,4) do (

Etc.

Posted: 2020-06-18 04:27
by Steven W
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)

:wink:

Posted: 2020-06-18 04:32
by Steven W
Of course, that's a nuke it all.

Posted: 2020-06-18 12:49
by !
Oh, very cool addendum, thank you. :clap: