Remove empty folders log

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

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: 4654
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: 158
Joined: 21 Sep 2010 02:22
Location: Greece

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: 4654
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: 158
Joined: 21 Sep 2010 02:22
Location: Greece

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 !!!

drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Remove empty folders log

#6 Post by drgt » 19 Oct 2021 04:17

Off Topic.
This code is credited for pointing out an empty subfolder of a folder sitting on my desktop that after I delete it it gets recreated the next time I start windows.
I looked by name and path in registry, startup, msconfig, autoexec, searched drives and I cannot find what creates this subfolder. I even used Process Monitor and (if I used it correctly) found nothing.
I put this batch in the startup folder and finds that folder, which means it is created before the batch file is executed.

Any ideas are very welcome.

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

Re: Remove empty folders log

#7 Post by aGerman » 19 Oct 2021 06:00

Well, that's crystal ball gazing. I mean, a subfolder of a folder on you desktop is really kind of no information at all. What are the names? Do you know to what the parent folder belongs where the subfolder is created?

What if you set the subfolder read-only in its properties? Do you get any message or crashing process?
Maybe try out Autoruns where you can turn auto-starting processes off and on in order to find the culprit ¯\_(ツ)_/¯
https://docs.microsoft.com/en-us/sysint ... s/autoruns

Steffen

drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Remove empty folders log

#8 Post by drgt » 19 Oct 2021 11:04

Image Image Image

Post Reply