Software development, scripting and programming.
User avatar
!
27%
Posts: 2715
Joined: 2013-02-25 18:36

2015-12-19 10:12 »

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.

User avatar
Steven W
VIP
Posts: 1531
Joined: 2013-08-10 22:40

2020-06-18 04:27 »

for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)

:wink:

User avatar
Steven W
VIP
Posts: 1531
Joined: 2013-08-10 22:40

2020-06-18 04:32 »

Of course, that's a nuke it all.

User avatar
!
27%
Posts: 2715
Joined: 2013-02-25 18:36

2020-06-18 12:49 »

Oh, very cool addendum, thank you. :clap:

Post Reply