Software development, scripting and programming.
User avatar
!
25%
Posts: 2539
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.