Remove empty folders log

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
drgt
Posts: 116
Joined: 21 Sep 2010 02:22

Remove empty folders log

#1 Post by drgt » 02 Oct 2021 04:32

Hi

I would like to modify the following .bat file to make a txt file of the folders (with the path) that just removed.

(If possible, please show the code of another bat file that would make a txt file of the folders (with the path) that WOULD be removed).

Code: Select all

for /f "delims=" %%d in ('dir /s /b /ad ^| sort /r') do rd "%%d"
Thank you very much

aGerman
Expert
Posts: 4289
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Remove empty folders log

#2 Post by aGerman » 02 Oct 2021 05:51

IS removed:

Code: Select all

>"rd.log" (for /f "delims=" %%d in ('dir /s /b /ad ^| sort /r') do (>nul 2>&1 rd "%%~d") && echo "%%~d")
WOULD get removed:

Code: Select all

>"rd.log" (for /f "delims=" %%d in ('dir /s /b /ad ^| sort /r') do (>nul 2>&1 dir /s /b /a-d "%%~d") || echo "%%~d")
Steffen

drgt
Posts: 116
Joined: 21 Sep 2010 02:22

Re: Remove empty folders log

#3 Post by drgt » 02 Oct 2021 22:52

Thank you Steffen

The WOULD log includes all folders, not just empty ones

The list format is:
C:\Documents and Settings\user\Desktop\New Folder (5)>(dir /s /b /a-d "C:\Documents and Settings\user\Desktop\New Folder (5)\zip" 1>nul 2>&1 ) || echo "C:\Documents and Settings\user\Desktop\New Folder (5)\zip"(space&LineFeed not present in every entry)
"C:\Documents and Settings\user\Desktop\New Folder (5)\zip"
Line feed before next entry

Did not run the DID remove log...

aGerman
Expert
Posts: 4289
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Remove empty folders log

#4 Post by aGerman » 03 Oct 2021 08:29

D:\dirBase\test.bat

Code: Select all

@echo off &setlocal
echo *** before: ***
tree /f
>"rdWOULD.log" (for /f "delims=" %%d in ('dir /s /b /ad ^| sort /r') do (>nul 2>&1 dir /s /b /a-d "%%~d") || echo "%%~d")
>"rdIS.log" (for /f "delims=" %%d in ('dir /s /b /ad ^| sort /r') do (>nul 2>&1 rd "%%~d") && echo "%%~d")
echo(
echo *** after: ***
tree /f
pause
Output:

Code: Select all

*** before: ***
Auflistung der Ordnerpfade für Volume DATA
Volumeseriennummer : E0FC-CC23
D:.
│   test.bat
│
├───dirA1
│   └───dirA2
│       └───dirA3
└───dirB1
    └───dirB2
        │   file.txt
        │
        └───dirB3

*** after: ***
Auflistung der Ordnerpfade für Volume DATA
Volumeseriennummer : E0FC-CC23
D:.
│   test.bat
│   rdWOULD.log
│   rdIS.log
│
└───dirB1
    └───dirB2
            file.txt

Drücken Sie eine beliebige Taste . . .
So, dirB2 contains a file and shall not be removed, right? Of course the parent folders of dirB2 must also persist and can't be considered to be empty.

rdWOULD.log:

Code: Select all

"D:\dirBase\dirB1\dirB2\dirB3"
"D:\dirBase\dirA1\dirA2\dirA3"
"D:\dirBase\dirA1\dirA2"
"D:\dirBase\dirA1"
rdIS.log

Code: Select all

"D:\dirBase\dirB1\dirB2\dirB3"
"D:\dirBase\dirA1\dirA2\dirA3"
"D:\dirBase\dirA1\dirA2"
"D:\dirBase\dirA1"

drgt
Posts: 116
Joined: 21 Sep 2010 02:22

Remove empty folders log

#5 Post by drgt » 03 Oct 2021 10:30

Hi Steffen

The reason in my try the WOULD log contained more than the empty folders list, was that my .bat file had the code

Code: Select all

>"rd.log" (for /f "delims=" %%d in ('dir /s /b /ad ^| sort /r') do (>nul 2>&1 dir /s /b /a-d "%%~d") || echo "%%~d")
instead of

Code: Select all

@echo off
>"rd.log" (for /f "delims=" %%d in ('dir /s /b /ad ^| sort /r') do (>nul 2>&1 dir /s /b /a-d "%%~d") || echo "%%~d")
The
(space&LineFeed not present in every entry)
I mentioned in my previous post, (did not check one by one but) I guess is present only in the empty folder entry.

The rest was just echo!

THANK YOU VERY MUCH !!!

Post Reply