Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there ANY way in DOS to rmdir a tree using wildcards?

584 views
Skip to first unread message

Spammay Blockay

unread,
Jul 30, 2003, 3:03:42 PM7/30/03
to
I notice that the DOS rmdir doesn't allow you to give it
the '*' wildcard as an argument. What does one do when one
needs to remove a bunch of directories if one doesn't know
their name, and do it in DOS?

- Tim

--

Andreas Koch

unread,
Jul 30, 2003, 4:31:58 PM7/30/03
to

You could try using the FOR command:

for (%f in tmp*) do rmdir %f

--
Andreas
He screamed: THIS IS SIG!

Spammay Blockay

unread,
Jul 30, 2003, 5:37:52 PM7/30/03
to
In article <bg99uj$3o6$06$1...@news.t-online.com>,

Andreas Koch <ma...@kochandreas.com> wrote:
>Spammay Blockay wrote:
>> I notice that the DOS rmdir doesn't allow you to give it
>> the '*' wildcard as an argument. What does one do when one
>> needs to remove a bunch of directories if one doesn't know
>> their name, and do it in DOS?
>
>You could try using the FOR command:
>
>for (%f in tmp*) do rmdir %f

I just tried that, but I need to be able to use other
environment variables in the script, which Command.com
doesn't seem to like... that is:

SET MYDIR=C:\a\b\c
FOR %F IN (%MYDIR%\PATTERN*) DO RMDIR %f

... doesn't work, because it doesn't seem to like %MYDIR%
in the FOR command. Any way around this you know of?

- Tim

--

Phil D

unread,
Jul 31, 2003, 9:46:29 AM7/31/03
to

Spammay Blockay

unread,
Jul 31, 2003, 2:06:54 PM7/31/03
to
In article <3b7iivkfj6c2kgubi...@4ax.com>,
Phil D <din...@nortelnutworks.com> wrote:
>use deltree
>
>http://www.computerhope.com/deltree.htm

Doesn't come with Windows 2000. It has to be something that's part
of Win2K already. You'd think they believe in simple shell scripting
sorts of syntaxs... but nooooooooo! :-)

- Tim

>On Wed, 30 Jul 2003 19:03:42 GMT, SPAMB...@BLOCKEDTOAVOIDSPAM.com
>(Spammay Blockay) wrote:
>
>>I notice that the DOS rmdir doesn't allow you to give it
>>the '*' wildcard as an argument. What does one do when one
>>needs to remove a bunch of directories if one doesn't know
>>their name, and do it in DOS?
>>
>>- Tim

--

Herman D. Knoble

unread,
Jul 31, 2003, 2:47:22 PM7/31/03
to
For Windows NT/2000/XP issue the command: del /?
See if the /S command will help.
That is del c:\test\*.* /s will delete all files in c:\test and all c:\test's children
subdirectories.

Then issue: rmdir c:\test /s
to remove the empty folders.

Skip Knoble, Penn State

On Thu, 31 Jul 2003 18:06:54 GMT, SPAMB...@BLOCKEDTOAVOIDSPAM.com (Spammay Blockay)
wrote:

-|In article <3b7iivkfj6c2kgubi...@4ax.com>,
-|Phil D <din...@nortelnutworks.com> wrote:
-|>use deltree
-|>
-|>http://www.computerhope.com/deltree.htm
-|
-|Doesn't come with Windows 2000. It has to be something that's part
-|of Win2K already. You'd think they believe in simple shell scripting
-|sorts of syntaxs... but nooooooooo! :-)
-|
-|- Tim
-|
-|>On Wed, 30 Jul 2003 19:03:42 GMT, SPAMB...@BLOCKEDTOAVOIDSPAM.com
-|>(Spammay Blockay) wrote:
-|>
-|>>I notice that the DOS rmdir doesn't allow you to give it
-|>>the '*' wildcard as an argument. What does one do when one
-|>>needs to remove a bunch of directories if one doesn't know
-|>>their name, and do it in DOS?
-|>>
-|>>- Tim


Herman D. (Skip) Knoble, Research Associate
(a computing professional for 38 years)
Mailto:h...@psu.edu
Web: http://www.personal.psu.edu/hdk
Penn State Information Technology Services
Academic Services and Emerging Technologies
Graduate Education and Research Services
Penn State University
214C Computer Building
University Park, PA 16802-21013
Phone:+1 814 865-0818 Fax:+1 814 863-7049

p...@prairienetx.org

unread,
Jul 31, 2003, 2:51:11 PM7/31/03
to
Isn't that what "RMDIR /s <path>" does?

Bryan

Spammay Blockay

unread,
Jul 31, 2003, 2:59:27 PM7/31/03
to
In article <fvoiivs6i1hf80aie...@4ax.com>,

Herman D. Knoble <h...@psu.edu> wrote:
>For Windows NT/2000/XP issue the command: del /?
>See if the /S command will help.
>That is del c:\test\*.* /s will delete all files in c:\test and all c:\test's children
> subdirectories.
>
>Then issue: rmdir c:\test /s
>to remove the empty folders.

Thanks for your help, Herman, but I think I may not have
made myself clear.

In this case, I have a directory stored in the variable
name %MYDIR%. In %MYDIR% there exists a bunch of files,
and some directories, who's names I do not know.

I need to delete all the subdirectories in %MYDIR%,
and all the files in those subdirectories (ie. the
entire directory tree of each of these directories),
BUT NOT ANY OTHER FILE in %MYDIR%.

So, if I've got:

%MYDIR%\myFile1.txt
%MYDIR%\myFile2.cmd
%MYDIR%\myDir1\file1.txt
%MYDIR%\myDir2\abc

Then the directory trees %MYDIR%\myDir1 and %MYDIR%\myDir2
should be deleted, but nothing else.

Any way you can think of doing this in a batch file?

- Tim


--

Spammay Blockay

unread,
Jul 31, 2003, 3:02:16 PM7/31/03
to
In article <bgboev$bnp$1...@wildfire.prairienet.org>,

<p...@prairienetX.org> wrote:
>Isn't that what "RMDIR /s <path>" does?

Nope, RMDIR doesn't recognize wildcards -- witness:

C:\>mkdir TESTDIR

C:\>cd TESTDIR

C:\TESTDIR>mkdir A

C:\TESTDIR>mkdir B

C:\TESTDIR>dir
Volume in drive C is Local Disk
Volume Serial Number is 7CD1-879E

Directory of C:\TESTDIR

07/31/2003 12:00p <DIR> .
07/31/2003 12:00p <DIR> ..
07/31/2003 12:00p <DIR> A
07/31/2003 12:00p <DIR> B
0 File(s) 0 bytes
4 Dir(s) 25,187,280,384 bytes free

C:\TESTDIR>rmdir /s *
*, Are you sure (Y/N)? y
The filename, directory name, or volume label syntax is incorrect.

C:\TESTDIR>

Any other ideas?

- Tim

>Bryan
>
>Spammay Blockay <SPAMB...@blockedtoavoidspam.com> wrote:
>> I notice that the DOS rmdir doesn't allow you to give it
>> the '*' wildcard as an argument. What does one do when one
>> needs to remove a bunch of directories if one doesn't know
>> their name, and do it in DOS?
>
>> - Tim
>
>> --
>>


--

Andreas Koch

unread,
Jul 31, 2003, 3:19:38 PM7/31/03
to
Spammay Blockay wrote:

> I need to delete all the subdirectories in %MYDIR%,
> and all the files in those subdirectories (ie. the
> entire directory tree of each of these directories),
> BUT NOT ANY OTHER FILE in %MYDIR%.

Mhhh... is backing up the files, deleting files
and directories, then copying the files back
an option?

Is Windows Scripting Host (should be in 2K) an
Option?

Spammay Blockay

unread,
Jul 31, 2003, 3:22:28 PM7/31/03
to
In article <bgbq2s$a86$06$1...@news.t-online.com>,

Andreas Koch <ma...@kochandreas.com> wrote:
>Spammay Blockay wrote:
>
>> I need to delete all the subdirectories in %MYDIR%,
>> and all the files in those subdirectories (ie. the
>> entire directory tree of each of these directories),
>> BUT NOT ANY OTHER FILE in %MYDIR%.
>
>Mhhh... is backing up the files, deleting files
>and directories, then copying the files back
>an option?

I tried using MOVE to move the directories to a known
place, and even that didn't understand wildcards
the way I need it to. Is that what you're suggesting?

>Is Windows Scripting Host (should be in 2K) an
>Option?

Don't know that -- does that require writing in VBScript?

- Tim

--

p...@prairienetx.org

unread,
Jul 31, 2003, 4:35:22 PM7/31/03
to
Actually, I was suggesting for your example "rmdir /s TESTDIR", but I see
in a leter post you need to preserve TESTDIR and any files in TESTDIR. My
suggestion gets rid of them as well. Sotty, that's all I have.

Spammay Blockay

unread,
Jul 31, 2003, 4:37:42 PM7/31/03
to
In article <bgbuia$djs$3...@wildfire.prairienet.org>,

<p...@prairienetX.org> wrote:
>Actually, I was suggesting for your example "rmdir /s TESTDIR", but I see
>in a leter post you need to preserve TESTDIR and any files in TESTDIR. My
>suggestion gets rid of them as well. Sotty, that's all I have.
>
>Bryan

Oh well, thanks anyway -- I hope someone out there knows the
answer (or if there IS an answer :-) ).

- Tim


--

Phil Robyn

unread,
Jul 31, 2003, 10:29:16 PM7/31/03
to
Spammay Blockay wrote:

> In article <bgbuia$djs$3...@wildfire.prairienet.org>,
> <p...@prairienetX.org> wrote:
>
>>Actually, I was suggesting for your example "rmdir /s TESTDIR", but I see
>>in a leter post you need to preserve TESTDIR and any files in TESTDIR. My
>>suggestion gets rid of them as well. Sotty, that's all I have.
>>
>>Bryan
>
>
> Oh well, thanks anyway -- I hope someone out there knows the
> answer (or if there IS an answer :-) ).
>
> - Tim

- - - - - - - - - - begin screen capture - - - - - - - - - -
<Win2000> c:\cmd>tree d:\junkdir
Directory PATH listing for volume MICRON
Volume serial number is 0012FC94 784B:893D
D:\JUNKDIR
????profiles
? ????user1
? ????user2
? ????user3
? ????user4
????suba
????subb
????subc

<Win2000> c:\cmd>for /f "tokens=*" %a in ('dir /ad /b d:\junkdir') do @echo remove d:\junkdir\%a /s /q
remove d:\junkdir\profiles /s /q
remove d:\junkdir\suba /s /q
remove d:\junkdir\subb /s /q
remove d:\junkdir\subc /s /q
- - - - - - - - - - end screen capture - - - - - - - - - -

If this appears to do what you want, then change '@echo remove' to 'rd'.
You may also have to add a pair of double quotes if any of your directory
names have spaces.


--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l

Spalls Hurgenson

unread,
Jul 31, 2003, 10:48:05 PM7/31/03
to


If I'm understanding you correctly, you want to keep all the files in
the folder and just delete all the directories. If you don't want to
delete all the directories, or only want to delete directories that
have certain letters in their name (e.g., delete \PORN001, \PORN002,
\P0RN003 but not \LETTERSTOGRANDMA ;) then this would be a different
story...

So, how about a simple batch file where you move the files to a
temporary location, then DELTREE the remaining contents of the folder?

E.g.,

MD C:\TEMPCOPY
XCOPY C:\MYDIR\*.* C:\TEMPCOPY
DELTREE C:\MYDIR\
XCOPY C:\TEMPCOPY\*.* C:\MYDIR

(if you don't have DELTREE, you could go use instead:

CD..
RMDIR C:\MYDIR
MD C:\MYDIR
CD C:\MYDIR

Of course, this is not an optimal solution; if you have a large number
of files the copies could take a long time, and various file
attributes (hidden, system, readonly) might cause problems... but it
doesn't seem to be the simplest.

>
>- Tim

Spammay Blockay

unread,
Aug 1, 2003, 1:32:07 AM8/1/03
to
In article <2pkjivco6hcgce1u7...@4ax.com>,

Spalls Hurgenson <there> wrote:
>If I'm understanding you correctly, you want to keep all the files in
>the folder and just delete all the directories. If you don't want to
>delete all the directories, or only want to delete directories that
>have certain letters in their name (e.g., delete \PORN001, \PORN002,
>\P0RN003 but not \LETTERSTOGRANDMA ;) then this would be a different
>story...
>
>So, how about a simple batch file where you move the files to a
>temporary location, then DELTREE the remaining contents of the folder?
>
>E.g.,
>
>MD C:\TEMPCOPY
>XCOPY C:\MYDIR\*.* C:\TEMPCOPY
>DELTREE C:\MYDIR\
>XCOPY C:\TEMPCOPY\*.* C:\MYDIR
>
>(if you don't have DELTREE, you could go use instead:
>
>CD..
>RMDIR C:\MYDIR
>MD C:\MYDIR
>CD C:\MYDIR
>
>Of course, this is not an optimal solution; if you have a large number
>of files the copies could take a long time, and various file
>attributes (hidden, system, readonly) might cause problems... but it
>doesn't seem to be the simplest.

I don't know if you've read my other postings, but the solution
requires that the deleting mechanism be able to use environment
variables and wildcards. DELTREE can do this, but I'm using Windows 2000,
which doesn't come with it.

And you can't do:

rmdir %MYVARIABLE%\*

Just try it under Windows 2000, and you'll see that rmdir doesn't
understand wildcards.

- Tim

--

Herman D. Knoble

unread,
Aug 1, 2003, 8:33:10 AM8/1/03
to
You can issue:

cd \
cd testdir
attrib +R *.* (make the files i testdir read only.)
del *.* /s /q (this will erase all files in A and B
attrib -R *.* (remove the Read Only bit)

Now A and B are empty and testdir files are ok.
Removing just these children subdirectories IA and B) is another matter.

Assuming that all files in Testdir have a file extension, and that no directries
under c:\testdir have extensions, one could issue:

CD \
cd testdir
dir /b *. > delthem.bat

Now delthem .bat would contain only subdirectoy names for subdirecties
under c:\testdir.

One could then edit this file using edlin (or even notepad) and insert before each line
the three characters 'rd ' (wiithout quotes), and then run: delthem.bat

Crude, but works.

Skip Knoble, Penn State


On Thu, 31 Jul 2003 20:37:42 GMT, SPAMB...@BLOCKEDTOAVOIDSPAM.com (Spammay Blockay)
wrote:

-|In article <bgbuia$djs$3...@wildfire.prairienet.org>,
-| <p...@prairienetX.org> wrote:
-|>Actually, I was suggesting for your example "rmdir /s TESTDIR", but I see
-|>in a leter post you need to preserve TESTDIR and any files in TESTDIR. My
-|>suggestion gets rid of them as well. Sotty, that's all I have.
-|>
-|>Bryan
-|
-|Oh well, thanks anyway -- I hope someone out there knows the
-|answer (or if there IS an answer :-) ).
-|
-|- Tim
-|
-|>Spammay Blockay <SPAMB...@blockedtoavoidspam.com> wrote:
-|>> In article <bgboev$bnp$1...@wildfire.prairienet.org>,
-|>> <p...@prairienetX.org> wrote:
-|>>>Isn't that what "RMDIR /s <path>" does?
-|>
-|>> Nope, RMDIR doesn't recognize wildcards -- witness:
-|>
-|>> C:\>mkdir TESTDIR
-|>
-|>> C:\>cd TESTDIR
-|>
-|>> C:\TESTDIR>mkdir A
-|>
-|>> C:\TESTDIR>mkdir B
-|>
-|>> C:\TESTDIR>dir
-|>> Volume in drive C is Local Disk
-|>> Volume Serial Number is 7CD1-879E
-|>
-|>> Directory of C:\TESTDIR
-|>
-|>> 07/31/2003 12:00p <DIR> .
-|>> 07/31/2003 12:00p <DIR> ..
-|>> 07/31/2003 12:00p <DIR> A
-|>> 07/31/2003 12:00p <DIR> B
-|>> 0 File(s) 0 bytes
-|>> 4 Dir(s) 25,187,280,384 bytes free
-|>
-|>> C:\TESTDIR>rmdir /s *
-|>> *, Are you sure (Y/N)? y
-|>> The filename, directory name, or volume label syntax is incorrect.
-|>
-|>> C:\TESTDIR>
-|>
-|>> Any other ideas?


-|>
-|>> - Tim
-|>

-|>>>Bryan
-|>>>
-|>>>Spammay Blockay <SPAMB...@blockedtoavoidspam.com> wrote:
-|>>>> I notice that the DOS rmdir doesn't allow you to give it
-|>>>> the '*' wildcard as an argument. What does one do when one
-|>>>> needs to remove a bunch of directories if one doesn't know
-|>>>> their name, and do it in DOS?
-|>>>
-|>>>> - Tim
-|>>>
-|>>>> --
-|>>>>
-|>
-|>
-|>> --
-|>>

Spalls Hurgenson

unread,
Aug 1, 2003, 9:52:35 AM8/1/03
to
On Fri, 01 Aug 2003 05:32:07 GMT, SPAMB...@BLOCKEDTOAVOIDSPAM.com
(Spammay Blockay) wrote:
>
>I don't know if you've read my other postings, but the solution

Some, not all (odd propogation on this server ;-)

>requires that the deleting mechanism be able to use environment
>variables and wildcards. DELTREE can do this, but I'm using Windows 2000,
>which doesn't come with it.

Then get it?
http://www.techadvice.cc/files/s29k2/w95b/deltree.exe

>
>And you can't do:
>
> rmdir %MYVARIABLE%\*
>
>Just try it under Windows 2000, and you'll see that rmdir doesn't
>understand wildcards.

Hmm, under WindowsXP,

rmdir %MYDIR%\
or
rmdir %MYDIR%\TEST\

both work. Don't forget the trailing slash. But perhaps 2K's rmdir
does things differently.

Spammay Blockay

unread,
Aug 1, 2003, 1:00:19 PM8/1/03
to
In article <eerkivk7isfnnh2g7...@4ax.com>,

Spalls Hurgenson <there> wrote:
>On Fri, 01 Aug 2003 05:32:07 GMT, SPAMB...@BLOCKEDTOAVOIDSPAM.com
>(Spammay Blockay) wrote:
>>
>>I don't know if you've read my other postings, but the solution
>
>Some, not all (odd propogation on this server ;-)
>
>>requires that the deleting mechanism be able to use environment
>>variables and wildcards. DELTREE can do this, but I'm using Windows 2000,
>>which doesn't come with it.
>
>Then get it?
>http://www.techadvice.cc/files/s29k2/w95b/deltree.exe

I was hoping to not have to add anything... it's supposed to work
without adding any new .exe's.

>>And you can't do:
>>
>> rmdir %MYVARIABLE%\*
>>
>>Just try it under Windows 2000, and you'll see that rmdir doesn't
>>understand wildcards.
>
>Hmm, under WindowsXP,
>
>rmdir %MYDIR%\
>or
>rmdir %MYDIR%\TEST\
>
>both work. Don't forget the trailing slash. But perhaps 2K's rmdir
>does things differently.

Did you try:

rmdir %MYDIR%\*

??

I'll see if adding a trailing slash makes a difference, tho'.

- Tim

--

Andreas Koch

unread,
Aug 1, 2003, 1:53:53 PM8/1/03
to
Spammay Blockay wrote:

>>>I need to delete all the subdirectories in %MYDIR%,
>>>and all the files in those subdirectories (ie. the
>>>entire directory tree of each of these directories),
>>>BUT NOT ANY OTHER FILE in %MYDIR%.
>>
>>Mhhh... is backing up the files, deleting files
>>and directories, then copying the files back
>>an option?
>
>
> I tried using MOVE to move the directories to a known
> place, and even that didn't understand wildcards
> the way I need it to. Is that what you're suggesting?

No.

copy \a\b\c\*.* c:\temp
deltree /y a\b\c
mkdir a\b\c
copy c:\temp\*.* \a\b\c

>>Is Windows Scripting Host (should be in 2K) an
>>Option?
>
> Don't know that -- does that require writing in VBScript?

I think WSH and VBS are two different things, but
it is a sort of programming language, yes.

Spammay Blockay

unread,
Aug 1, 2003, 2:31:11 PM8/1/03
to
In article <rpmkivo2bfbgku6de...@4ax.com>,

Herman D. Knoble <h...@psu.edu> wrote:
>You can issue:
>
>cd \
>cd testdir
>attrib +R *.* (make the files i testdir read only.)
>del *.* /s /q (this will erase all files in A and B
>attrib -R *.* (remove the Read Only bit)
>
>Now A and B are empty and testdir files are ok.
>Removing just these children subdirectories IA and B) is another matter.
>
>Assuming that all files in Testdir have a file extension, and that no directries
>under c:\testdir have extensions, one could issue:
>
>CD \
>cd testdir
>dir /b *. > delthem.bat
>
>Now delthem .bat would contain only subdirectoy names for subdirecties
>under c:\testdir.
>
>One could then edit this file using edlin (or even notepad) and insert before each line
> the three characters 'rd ' (wiithout quotes), and then run: delthem.bat
>
>Crude, but works.

Aaaaaaaaarrrgh! I've been spoiled by being an old unix hack!

Bad, BAD DOS! Need to be able to do: rm -fr $mydir/pattern*

Grrr. :-)

- Tim

--

Spalls Hurgenson

unread,
Aug 1, 2003, 11:04:47 PM8/1/03
to
On Fri, 01 Aug 2003 17:00:19 GMT, SPAMB...@BLOCKEDTOAVOIDSPAM.com
(Spammay Blockay) wrote:

>Did you try:
>
> rmdir %MYDIR%\*

Yes. Specifically, I created and filled the folder C:\TEMP\TEST with a
number of garbage files and directories. I then SET MYDIR=C:\TEMP\TEST

This I followed with the command RMDIR /S /Q %MYDIR%\ . This
successfully deleted C:\TEMP\TEST and all its contents. The /S command
is required to delete all contents, the /Q makes it work quietly,
without asking for confirmation. Without the /S command, RMDIR will
complain that the directory is not empty

As I said, this was in WinXP (Pro, SP1); it's possible RMDIR works
differently in Win2K.

Of course, after RMDIR the directory, you'd have to recreate it and
then copy the files you wanted to save back from the temp folder you
made...


>I'll see if adding a trailing slash makes a difference, tho'.

Be sure to let us know how it turns out, if only to confirm whether or
not Win2K's RMDIR works differently.

Denrael

unread,
Oct 26, 2005, 2:02:53 AM10/26/05
to
You should be able to do this using a deltree on the directories in
mydir.

set your location to %mydir%
for /d %d in (*) do deltree "%d"

Check the switching on deltree, you might need some

0 new messages