China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-12 02:56
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Automatically determine whether the folder is empty. If it is an empty folder, delete it! View 4,322 Replies 16
Original Poster Posted 2008-02-22 00:47 ·  中国 湖北 宜昌 电信
初级用户
Credits 41
Posts 14
Joined 2006-09-30 02:27
19-year member
UID 64100
Status Offline
Automatically determine whether a folder is empty. If it is an empty folder, delete it!

for /l %%a in (1,1,3) do for/r %%i in (.) do rd %%~si

Let me see if there are any errors.

Can it be used in LJ file cleaning?
Floor 2 Posted 2008-02-23 00:53 ·  中国 福建 三明 电信
高级用户
★★
论坛上抢劫的
Credits 551
Posts 246
Joined 2006-09-21 12:35
19-year member
UID 63270
Status Offline
for /f "tokens=*" %%a in ('dir /b /ad /s "l:\"^|sort /r" do rd "%%a" 2>nul
Floor 3 Posted 2008-02-23 09:26 ·  中国 江苏 扬州 江都区 电信
高级用户
★★★
Credits 620
Posts 329
Joined 2007-12-05 15:34
18-year member
UID 104754
Gender Male
Status Offline
Originally posted by lotus516 at 2008-2-23 12:53 AM:
for /f "tokens=*" %%a in ('dir /b /ad /s "l:\"^|sort /r') do rd "%%a" 2>nul


Expert...

Also, may I ask...

How to search and delete all types of files with 0 bytes.
Floor 4 Posted 2008-02-23 11:33 ·  中国 山东 烟台 联通
中级用户
★★
Credits 257
Posts 123
Joined 2008-01-05 11:08
18-year member
UID 107722
Gender Male
From 烟台
Status Offline
Indeed powerful
Learned
Salute
All types of files with 0 bytes
Use %%~z in FOR to expand the file size
%%~zi=0 means 0 bytes i
路 是自己选的
学习 是为了具备解决问题的能力
Floor 5 Posted 2008-02-23 11:40 ·  中国 山东 烟台 联通
中级用户
★★
Credits 257
Posts 123
Joined 2008-01-05 11:08
18-year member
UID 107722
Gender Male
From 烟台
Status Offline
What does "l:\" mean in the following?
I don't understand here, can you give me some guidance?
Thank you very much
路 是自己选的
学习 是为了具备解决问题的能力
Floor 6 Posted 2008-02-23 12:16 ·  中国 江苏 扬州 江都区 电信
高级用户
★★★
Credits 620
Posts 329
Joined 2007-12-05 15:34
18-year member
UID 104754
Gender Male
Status Offline
Originally posted by zyz0304360 at 2008-2-23 11:40 AM:
May I ask
In the line "for /f "tokens=*" %%a in ('dir /b /ad /s "l:\"^|sort /r') do rd "%%a" 2>nul ",
what does the part "l:\" mean?
I don't understand this part here...



Dizzy
It's to delete the folders in the current drive letter.......


The 0-byte one.. you can say it again.....
I didn't understand it..........
Floor 7 Posted 2008-02-23 13:01 ·  中国 北京 华为云
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
It is to use the characteristics of the rd command. When deleting a 0-byte folder, it will not prompt an error.
Floor 8 Posted 2008-02-23 13:05 ·  中国 山东 烟台 联通
中级用户
★★
Credits 257
Posts 123
Joined 2008-01-05 11:08
18-year member
UID 107722
Gender Male
From 烟台
Status Offline
@echo off
for /f "tokens=*" %%i in ('dir /b *.* ^|sort /r') do if %%~zi==0 del /f /q "%%~ni.*"
pause

[ Last edited by zyz0304360 on 2008-2-23 at 01:18 PM ]
路 是自己选的
学习 是为了具备解决问题的能力
Floor 9 Posted 2008-02-23 13:26 ·  中国 山东 烟台 联通
中级用户
★★
Credits 257
Posts 123
Joined 2008-01-05 11:08
18-year member
UID 107722
Gender Male
From 烟台
Status Offline
Originally posted by cad55 at 2008-2-23 12:16 PM:

Wow
It's to delete the current drive letter of the folder.......

Which 0-byte.. you say again.....
Didn't understand..........


It's to delete the current drive letter of the folder.......

I execute
dir /b /ad /s "|:\"
and it will report an error
Cannot find the specified path ah

For example, there is such a file
C:aa\aa\aa
How to delete the drive letter?
Like this?
C:aa\aa\aa |:\

I'm a newbie, currently only understand this
set a=C:aa\aa\aa
set a=%a:\=%

The above is not understood, please give guidance, thank you
路 是自己选的
学习 是为了具备解决问题的能力
Floor 10 Posted 2008-02-23 15:07 ·  中国 江苏 扬州 电信
新手上路
Credits 4
Posts 2
Joined 2008-02-02 13:51
18-year member
UID 110023
Gender Male
Status Offline
Is there anyone who can explain it completely? I still don't quite understand. Thanks a lot!
Floor 11 Posted 2008-02-23 16:50 ·  中国 天津 电信
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
Originally posted by zyz0304360 at 2008-2-23 01:26 PM:

It is to delete the current drive letter of the folder.......

I execute
dir /b /ad /s "|:\"
and it will report an error
Cannot find the specified path.

For example, there is such a file
C:aa\aa\aa
How to delete the drive letter...

Removing "|:\" means the current drive letter.

Replace "|:\" with your own path.
Floor 12 Posted 2008-02-23 17:04 ·  中国 江苏 扬州 江都区 电信
高级用户
★★★
Credits 620
Posts 329
Joined 2007-12-05 15:34
18-year member
UID 104754
Gender Male
Status Offline
Originally posted by duansuo at 2008-2-23 03:07 PM:
Is there anyone who can explain it completely? I still don't understand it very well.

Thanks!



For example: You want to delete empty folders under the C drive:
for /f "tokens=*" %%a in ('dir /b /ad /s "c:\"^|sort /r') do rd "%%a" 2>nul



You want to delete empty folders under the D drive:
for /f "tokens=*" %%a in ('dir /b /ad /s "d:\"^|sort /r') do rd "%%a" 2>nul

[ Last edited by cad55 on 2008-2-23 at 05:14 PM ]
Floor 13 Posted 2008-02-23 17:15 ·  中国 广东 广州 白云区 电信
初级用户
★★
Credits 132
Posts 59
Joined 2007-08-23 21:24
18-year member
UID 95705
Gender Male
From GuangZhou
Status Offline
Why does changing the
for /f "tokens=*" %%a in ('dir /b /ad /s "E:\"^|sort /r') do rd "%%a" 2>nul

to:
for /f "tokens=*" %%a in ('dir /b /ad /s "E:\"^|sort /r') do echo "%%a">>空文件夹菜单.txt

result in all folders being written to the notepad?
I could be the one
http://kanlis.7ta.cn
Floor 14 Posted 2008-02-23 17:18 ·  中国 天津 电信
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
Originally posted by Nickey at 2008-2-23 05:15 PM:
Why change the
for /f "tokens=*" %%a in ('dir /b /ad /s "E:\"^|sort /r') do rd "%%a" 2>nul

to:
for /f "tokens=*" %%a in ( ... 

'dir /b /ad /s "E:\"^|sort /r'

will list all folders. As for why it only deletes empty folders and not non-empty ones

it is because of the characteristic of the rd command. When deleting an empty folder, it does not prompt

When the /Q parameter is not added, the rd command will prompt an error message when deleting a non-empty folder.
Floor 15 Posted 2008-02-23 18:06 ·  中国 山东 烟台 联通
中级用户
★★
Credits 257
Posts 123
Joined 2008-01-05 11:08
18-year member
UID 107722
Gender Male
From 烟台
Status Offline
Originally posted by Nickey at 2008-2-23 05:15 PM:
Why change the
for /f "tokens=*" %%a in ('dir /b /ad /s "E:\"^|sort /r') do rd "%%a" 2>nul

to:
for /f "tokens=*" %%a in ( ... 



'dir /b /ad /s "E:\"^|sort /r'
This command is used to list all folder names (with /ad) under and in subdirectories (/s) of drive E:
/ad specifies to print folder names
Then assign the folder name to %%a
2>nul
means to write the error commands when executing the rd "%%a" command to the null device (that is, no feedback is needed)
Changing it to echo "%%a" >>aaa.txt
Of course, it is to write the system feedback when deleting empty files to aaa.txt
Because %%a is the folder name

[ Last edited by zyz0304360 on 2008-2-23 at 06:07 PM ]
路 是自己选的
学习 是为了具备解决问题的能力
Forum Jump: