Quote:
Originally Posted by 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 |
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
Project1data
in
out
pdf
symb
Project2data
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