Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Delete all (non empty) directory with a specific pattern.

I am using TCC 26.02.42.

I can list all directories I want to delete with:

Code:
dir /A:d ::^[0-9a-z]{8}\.[0-9a-z]{3}$

Now I want to delete all this directories:

Code:
rd /s ::^[0-9a-z]{8}\.[0-9a-z]{3}$

I get the error that the directory name is invalid.
 
I don't think RD was meant to support regexes. But if you can generate a list of names with DIR /F, you could redirect that list to a file, and then use the RD /S @file syntax.

But if you only want to remove non-empty directories, or only empty directories, then you'd need another step: loop through the directories in your @file, and filter out the empty (non-empty) ones. Could get tedious.
 
I think the regex works with RD. The problem is the "/S" with dosen't support wildcards. So I could use the RD command above successfully in my v29.00.17, just without "/S".
 
Last edited:
dir /A:d ::^[0-9a-z]{8}\.[0-9a-z]{3}$
Just an observation: I think your beginning_of_line anchor (^) will be lost because it's not quoted or escaped.

Code:
v:\> dir /k /m /A:d ::^[0-9a-z]{8}\.[0-9a-z]{3}$
2023-03-01  12:10         <DIR>    12345678.123
2023-03-01  12:10         <DIR>    123456789.123
 
I think the regex works with RD. The problem is the "/S" with dosen't support wildcards. So I could use the RD command above successfully in my v29.00.17, just without "/S".

I think you're right. The help page for RD says:

You cannot use wildcards with the /S option.
and also:
Note: Do not use /S with @file lists.

So perhaps the best approach is to create a file list, then loop through it with DO.
 
Just an observation: I think your beginning_of_line anchor (^) will be lost because it's not quoted or escaped.

Code:
v:\> dir /k /m /A:d ::^[0-9a-z]{8}\.[0-9a-z]{3}$
2023-03-01  12:10         <DIR>    12345678.123
2023-03-01  12:10         <DIR>    123456789.123

Yes, should be escaped with another ^ or with quotes - means so ...

Code:
dir /A:d ::^^[0-9a-z]{8}\.[0-9a-z]{3}$

or so ...

Code:
dir /A:d ::"^[0-9a-z]{8}\.[0-9a-z]{3}$"
 
This would be very straightforward. Make a list of fully-qualified names.

Code:
v:\> dir /f /k /m /s /A:d ::"^[0-9a-z]{8}\.[0-9a-z]{3}$"
V:\12345678.123
V:\LocalDumps\12345678.123

You could put the list in a temporary file, but no need ... just RD them.

Code:
v:\> do d in /p dir /f /k /m /s /A:d ::"^[0-9a-z]{8}\.[0-9a-z]{3}$" (echo removing %d ... & rd %d)
removing V:\12345678.123 ...
removing V:\LocalDumps\12345678.123 ...

After that,

Code:
v:\> dir /f /k /m /s /A:d ::"^[0-9a-z]{8}\.[0-9a-z]{3}$"

v:\>
 
Wait a sec.... Are we removing empty directories, or non-empty directories?

And a complication which I had forgotten: If you're going to remove empty directories, you might want to remove them from the innermost out. Sometimes removing directories will cause the parent directory to become empty.
 
Wait a sec.... Are we removing empty directories, or non-empty directories?

And a complication which I had forgotten: If you're going to remove empty directories, you might want to remove them from the innermost out. Sometimes removing directories will cause the parent directory to become empty.

There's a (dangerous) switch for removing non-empty ones. As for non-empty ones, bottom-up, perhaps ...

Make the list I mentioned in post #7 sorted in reverse order. That ought to put them in bottom-up order (yes/no?)

Code:
v:\> dir /f /k /m /s /A:d ::"^[0-9a-z]{8}\.[0-9a-z]{3}$" | sort /r > dirs.txt

v:\> type dirs.txt
V:\12345678.123\12345678.123\12345678.123
V:\12345678.123\12345678.123
V:\12345678.123

v:\> rd @dirs.txt

You could probably get that DIR command inside a DO /P but I suspect that wouldn't be easy.
 
Actually not so hard.

Code:
v:\> do d in /p `dir /f /k /m /s /A:d ::"^[0-9a-z]{8}\.[0-9a-z]{3}$" | sort /R` (echo removing %d & rd %d)
removing V:\12345678.123\12345678.123\12345678.123
removing V:\12345678.123\12345678.123
removing V:\12345678.123
 
That seems to be a decent trick for getting a bottom-up list of directories for any purpose.

Code:
v:\> (global /h /i /q /n echo %_cwd) | sort /r
V:\wt_schemes
V:\watchtest\foo
V:\watchtest
V:\tweakstuff
V:\test
V:\s2g
V:\LocalDumps
V:\junk
V:\ipreg
V:\hold
V:\from_barnyard\vefatica\public_html\penstuff
V:\from_barnyard\vefatica\public_html\ftpfiles
V:\from_barnyard\vefatica\public_html\4plugins
V:\from_barnyard\vefatica\public_html
V:\from_barnyard\vefatica
V:\from_barnyard\untarred\vefatica\News
V:\from_barnyard\untarred\vefatica\mail
V:\from_barnyard\untarred\vefatica\.xfm
V:\from_barnyard\untarred\vefatica\.openoffice
V:\from_barnyard\untarred\vefatica\.lftp
V:\from_barnyard\untarred\vefatica\.gnome\mime-info
V:\from_barnyard\untarred\vefatica\.gnome\gnome-vfs
V:\from_barnyard\untarred\vefatica\.gnome\application-info
V:\from_barnyard\untarred\vefatica\.gnome-desktop
V:\from_barnyard\untarred\vefatica\.gnome
V:\from_barnyard\untarred\vefatica\.emacs.d\auto-save-list
V:\from_barnyard\untarred\vefatica\.emacs.d
V:\from_barnyard\untarred\vefatica
V:\from_barnyard\untarred
V:\from_barnyard
V:\eshelp\everything\sdk
V:\eshelp\everything
V:\eshelp
V:\
 
Thanks for the ideas. It's about folders in my TMP directory.

But doesn't work either

Code:
rd /s @filestodelete

With the /s option I can't use it. It works without, but the directories aren't empty. SO It seams that I need a do loop.
 

Similar threads

Back
Top