# bat file to del sub folders and there files



## kdsprocket

Hello
I'm trying to create a bat file that will del specific subfolders and there files.
I have a tree that looks like this
C:\CAD\Projects\projectname\
and I want to delete the subfolders
\out
\in
\pdf
\symb
\data
I have multiple Projectnames so I was trying to us a wildcard. There are sub folders I want to keep in each projectname
del c:\cad\projects\*\out
del c:\cad\projects\*\in
del c:\cad\projects\*\pdf
del c:\cad\projects\*\out
It didn't work
I also tried deltree ...no luck
anyone have a answer


----------



## Squashman

Code:


C:\>rd /?
Removes (deletes) a directory.

RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path

    /S      Removes all directories and files in the specified directory
            in addition to the directory itself.  Used to remove a directory
            tree.

    /Q      Quiet mode, do not ask if ok to remove a directory tree with /S


----------



## TheOutcaste

And to avoid having to change the batch file when project names change:


Code:


for /D %%I in ("C:\CAD\Projects\*") do For %%J in ([COLOR="Red"][B]out in pdf symb data[/B][/COLOR]) do rmdir /s/q [COLOR="Blue"][B]"[/B][/COLOR]%%I\%%J[COLOR="Blue"][B]"[/B][/COLOR]

The quotes shown in blue are only needed if the path contains spaces
This won't work if any of the subfolders shown in red contain a space in their name.
If they do, create a file with the folder names listed one per line named for example DelThese.txt and use this instead:


Code:


for /D %%I in ("C:\CAD\Projects\*") do For [B][COLOR="Red"]/F "tokens=*" [/COLOR][/B] %%J in ([COLOR="Red"][B]DelThese.txt[/B][/COLOR]) do rmdir /s/q [B][COLOR="Red"]"[/COLOR][/B]%%I\%%J[B][COLOR="Red"]"[/COLOR][/B]

if DelThese.txt is not in the same folder as the batch file, you must specify the path. If the path contains spaces you must add the usebackq option:


Code:


for /D %%I in ("C:\CAD\Projects\*") do For /F "[B][COLOR="Red"]usebackq [/COLOR][/B]tokens=*" %%J in ([COLOR="Red"][B]"C:\Some Path\[/B][/COLOR]DelThese.txt[COLOR="Red"][B]"[/B][/COLOR]) do rmdir /s/q "%%I\%%J"

HTH

Jerry


----------



## kdsprocket

Rd only removes empty folders

I'm not familar with all the switches
/f deletes all the files in a specified directory
/s specified sub directories
/f forces read only

What is %% J
ANd what is %%I

Is the (delthese.txt) a separate file that list the folders to delete?

Thanks guys for your input


----------



## Squashman

kdsprocket said:


> What is %% J
> ANd what is %%I
> Thanks guys for your input


Variables used in the For Loops.


----------



## Squashman

kdsprocket said:


> Rd only removes empty folders


What version of Windows are you using. Using the /s switch to remove everything.


----------



## kdsprocket

Squashman said:


> What version of Windows are you using. Using the /s switch to remove everything.


 WINDOWS XP PRO SP2


----------



## TheOutcaste

kdsprocket said:


> Rd only removes empty folders
> 
> I'm not familar with all the switches
> /f deletes all the files in a specified directory
> /s specified sub directories
> /f forces read only
> 
> What is %% J
> ANd what is %%I
> 
> Is the (delthese.txt) a separate file that list the folders to delete?
> 
> Thanks guys for your input


RD (rmdir) does not have a /f switch, but DEL does, to force deletion of read only files.
The /F in the batch is a switch for the FOR statement
/s removes all files:


Code:


Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>rd /?
Removes (deletes) a directory.

RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path

    /S      [COLOR="Red"]Removes all directories and files[/COLOR] in the specified directory
            in addition to the directory itself.  Used to remove a directory
            tree.

    /Q      Quiet mode, do not ask if ok to remove a directory tree with /S

The delthese.txt file only needs to be used if the name of a folder you want to delete contains a space -- it's a restriction of the way the FOR command works.

To step through what the command does:
assume you have this structure:
c:\CAD\Projects
Project1
data
in
out
pdf
symb​Project2
data
in
out
pdf
symb​
*for /D %%I in ("C:\CAD\Projects\*") do*
This part reads the top level folder names one at a time under c:\CAD\Projects\ (Project1 and Project2) and assigns it to the %I variable.
In a batch file you have to use two % signs, so it has to be entered as %%I​First time through the *FOR* loop, %I=C:\CAD\Projects\Project1. This is passed to the second *FOR* command
*For %%J in (out in pdf symb data) do*
This loop assigns one of the values in red to the variable %J. First time through it is *out*.
The %I and %J values are now used in the RMDIR command.
*rmdir /s/q "%%I\%%J"*
When you expand the variables, this makes the rmdir command look like this:
*rmdir /s/q "C:\CAD\Projects\Project1\out"*
The /s switch tells it to remove all files and folders in *C:\CAD\Projects\Project1\out*, then to remove the *out* folder.
The blue quotes are only needed if the projectname contains a space, ie *Project 1* instead of *Project1*. It doesn't hurt to include them just in case a future folder name contains a space.
The *FOR* loop then repeats with *in*, then *pdf*, then *symb*, then *data*
Once complete, control passes back to the first *FOR* loop, and %I is now assigned *C:\CAD\Projects\Project2* and the second *FOR* loop repeats the process using this new value.

The second *FOR* loop uses the spaces to separate the folder names (*out in pdf symb data*), so if any of these folder names contain a space, you have to put the names in a file (delthese.txt) and tell the *FOR* loop to read lines from the file by using the /F switch. Without the /F switch, %J would be assigned the file NAME, not the contents.
You can use any name, and it doesn't have to have an extension, so *@123##* would work. It just has to be a text file with each folder name on a separate line.

In my original post I left out the *tokens=** option on the 2nd and 3rd code blocks (I've edited the post to add it). The default delimiter is space and tab, so everything up to the first space would be assigned to %J. Adding tokens=* tacks on the rest of the line. This tells the *FOR* loop to assign the entire line to the %J variable. Using "delims=" also works as long as there is no space between the = and the ".

To test the file, you can insert *@echo* in front of *rmdir /sq*. This will just list the rmdir commands that will be created without deleting anything.

Entering *FOR /?* in a command prompt window will show the different options you can use with the FOR command.

HTH

Jerry


----------

