There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
audio avg avg 8 bios boot browser bsod computer cpu crash css dell desktop driver dvd email error excel explorer firefox firefox 3 freeze game graphics hard drive hardware help please hijackthis hjt install internet internet explorer itunes javascript lan laptop malware missing monitor msn network networking openoffice outlook outlook 2003 outlook express php popups problem problems router seo slow sound sp3 spyware startup trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless word
DOS/PDA/Other
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Operating Systems > DOS/PDA/Other >
bat file to del sub folders and there files


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
kdsprocket's Avatar
Junior Member with 3 posts.
 
Join Date: Apr 2008
22-Apr-2008, 02:20 PM #1
bat file to del sub folders and there files
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's Avatar
Distinguished Member with 12,231 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
22-Apr-2008, 06:44 PM #2
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's Avatar
Computer Specs
Senior Member with 1,537 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
22-Apr-2008, 11:40 PM #3
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 (out in pdf symb data) do rmdir /s/q "%%I\%%J"
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 /F "tokens=*"  %%J in (DelThese.txt) do rmdir /s/q "%%I\%%J"
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 "usebackq tokens=*" %%J in ("C:\Some Path\DelThese.txt") do rmdir /s/q "%%I\%%J"
HTH

Jerry
__________________
Of course I know all the answers ; I just don't always match the answers to the right questions

Warning -- Windows spoken here. (Rated R for Strong Language and Violence -- When your Windows PC flies through a window, that's violent, right?)

Last edited by TheOutcaste : 23-Apr-2008 07:21 PM. Reason: added tokens=* parameter
kdsprocket's Avatar
Junior Member with 3 posts.
 
Join Date: Apr 2008
23-Apr-2008, 11:12 AM #4
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's Avatar
Distinguished Member with 12,231 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
23-Apr-2008, 12:35 PM #5
Quote:
Originally Posted by kdsprocket View Post
What is %% J
ANd what is %%I
Thanks guys for your input
Variables used in the For Loops.
Squashman's Avatar
Distinguished Member with 12,231 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
23-Apr-2008, 12:36 PM #6
Quote:
Originally Posted by kdsprocket View Post
Rd only removes empty folders
What version of Windows are you using. Using the /s switch to remove everything.
kdsprocket's Avatar
Junior Member with 3 posts.
 
Join Date: Apr 2008
23-Apr-2008, 03:51 PM #7
Quote:
Originally Posted by Squashman View Post
What version of Windows are you using. Using the /s switch to remove everything.
WINDOWS XP PRO SP2
TheOutcaste's Avatar
Computer Specs
Senior Member with 1,537 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
23-Apr-2008, 07:37 PM #8
Quote:
Originally Posted by kdsprocket View Post
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      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
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
__________________
Of course I know all the answers ; I just don't always match the answers to the right questions

Warning -- Windows spoken here. (Rated R for Strong Language and Violence -- When your Windows PC flies through a window, that's violent, right?)

Last edited by TheOutcaste : 23-Apr-2008 09:42 PM. Reason: typo
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 04:30 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.