Difference in behavior of RMDIR vs. CMD.EXE?

Jun 11, 2014
3
0
#1
Hi there,

I've always believed and found that 4DOS/4NT/TCC have worked as per Microsoft's shells (COMMAND.COM then later CMD.EXE) where there is a corresponding command/option combination.

However, our production department just partially obliterated a server (deleting all files older than 30 days) and it seems they fell foul of a behaviour difference, which I'm trying to understand.

The production manager, as he is prone to do, Googled a problem: he wanted to delete all empty directories on the system over 30 days old. He found a one-line CMD.EXE script using FORFILES and which crucially used RMDIR.

FORFILES /p f:\ /S /D -30 /C "cmd /c IF @isdir == TRUE RMDIR /S /Q @path"

Now, he also Googled RMDIR and found sites commenting on the fact RMDIR won't delete directories with directories and indeed my later tests seemed to bear this out.

However, in practice running in TCC 16 without any aliases (he assures me I haven't been able to verify that yet) this did delete all directories older than 30 days irrespective of their containing files or not.

Right now I am trying to understand if my previous implicit assumption that TCC is 100% comparable to CMD.EXE where such comparability exists is plain wrong.

As a follow-up, does anyone have a suggestion as to how this requirement (delete empty directories older than 30 days) can be achieved safely?

Thanks.
 
#2
I suggest something like (tested only partially):
Code:
ffind/s /a:d /b /[!d-30] * | for %d in ( @con: ) if %@files[/h,%@quote[%d]]==0 rmdir %@quote[%d]
- The %@quote[] function is useful (only) if you directory names contain spaces.
- TCC's RMDIR is in fact documented to remove files and subdirectories.
- You don't state exactly what you mean with "empty" ("no files" or "no files neither directories") and "older than 30 days" (does this concern the directory alone or also its subdirectories); you may have to write more complex logic.
- Compatibilty to CMD is an issue often discussed in the forum; sadly it cannot be 100% because of incompatibility between versions of CMD. See for example https://jpsoft.com/forums/threads/date-and-cmd-compatibility.5646/#post-33490, search for "compatibility" in the forum.
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,506
46
Albuquerque, NM
prospero.unm.edu
#3
The production manager, as he is prone to do, Googled a problem: he wanted to delete all empty directories on the system over 30 days old. He found a one-line CMD.EXE script using FORFILES and which crucially used RMDIR.

FORFILES /p f:\ /S /D -30 /C "cmd /c IF @isdir == TRUE RMDIR /S /Q @path"
Since your script is spawning CMD.EXE to do the removal, I don't really see how TCC's implementation matters...?
 
May 26, 2008
488
4
#4
FORFILES is an external command, not to mention you're running CMD to do the directory removal. This has nothing to do with Take Command as far as I can tell.

I think the mistake is that the RMDIR (RD) command has /S on it. That's going to remove the directory whether or not it's empty.
 

samintz

Scott Mintz
May 20, 2008
1,245
11
Solon, OH, USA
#5
Robin,

As Christian mentioned you weren't clear on what "empty" means. There are a number of ways to do this quite easily in TCC. You can use date ranges to specify only files and directories older than 30 days. You can use file exclusion ranges to select only directories or only files. You can also use FOR or DO to iterate just like FORFILES does.

To look at only top-level directories that do not contain any subdirectories or files older than 30 days:
Code:
DO d in /[!d-30] /a:d * (if %@files[/h "%d"] == 0 RD "%d")