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-06-21 03:26
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Methods to detect whether a folder is an empty directory View 3,718 Replies 11
Original Poster Posted 2006-12-05 11:42 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Some netizens asked me for a piece of code to detect which sub - directories under a certain folder are empty. I originally thought it would be very simple to solve it with dir, but when I tested it, I found that things were not that simple. During this period, I changed several ideas, and it actually took me two hours to solve the problem. The following is my problem - solving process:

At first, I thought I could first use dir /ad /b /s to get the paths of all sub - directories, and then judge whether the sub - directory is empty by getting whether the output of "dir /a sub - directory" is empty. The code is as follows:

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /ad /b /s') do (
for /f %%j in ('dir /a /b "%%i"') do if "%%j"=="" echo "%%i" has no files
)
pause


However, no matter how I changed the environment for testing, I could never detect the empty directory. I couldn't figure out why, so I gave up the idea of detecting empty values to detect empty directories.

I changed to the following code to view test.txt and found that the result of dir for the empty directory took 9 lines (including blank lines).

@echo off
cd.>test.txt
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /a /b /s') do >>test.txt dir /a "%%i"
start test.txt


Then I thought of using the method of detecting the number of lines in the dir result to detect whether the folder is empty, and got the following code:

@echo off
set num=0
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /ad /b /s') do (
for /f %%j in ('dir /a "%%i"') do set /a num+=1
if!num! lss 8 echo "%%i" has no files
set num=0
)
pause


So far, the problem has been solved.

Later, after discussing in the group, I found that the above code can also be optimized, and there are several other schemes for detecting whether the folder directory is empty, such as the rd scheme, the copy scheme. For the sake of time, the principle will not be explained too much for the time being.

[ Last edited by namejm on 2007 - 1 - 6 at 12:22 AM ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 2 Posted 2006-12-05 13:42 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
TO namejm, the code segment 1 is detected, and the possible reasons are as follows:
for /f %%j in ('dir /a /b "%%i"') do if "%%j"=="" echo "No files under %%i"
The above statement cannot execute normally when %%i is an empty value, because 'dir /a /b "%%i"' is empty, that is, there is no %%j at all, so the desired result cannot be achieved.

When %%i is empty, it is similar to the following code.
Test statement:
for /f %i in () do if "%i"=="" (echo ok ) else echo no
When the above code is executed, nothing is displayed. Is it unexpected?

Using findstr can avoid this situation:

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /ad /b /s') do (
dir /a /b "%%i"|findstr .>nul||echo %%i
)
pause


[ Last edited by youxi01 on 2006-12-5 at 01:45 PM ]
Floor 3 Posted 2006-12-06 00:01 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Using findstr to detect is indeed a method. However, compared with the code in the top post, it is slightly less efficient. The problem lies in the use of the pipe symbol. In addition, the statement to enable variable delay with 2F can be removed.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 4 Posted 2007-01-06 11:35 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Based on the statement in building 2, a more concise and efficient code is obtained:


@echo off
set flag=
for /f %%i in ('dir /a /b "Target folder"') do set flag=1
if defined flag (echo The folder is not empty) else echo This is an empty folder
pause


Thanks to the reminder from the downstairs, an error was modified.

[ Last edited by namejm on 2007-1-6 at 12:17 AM ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 5 Posted 2007-01-06 12:39 ·  中国 江西 南昌 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
There is a vulnerability in the code of floor 4. If the judgment of "target folder" does not exist is wrong. If there is only a file under "target folder", the judgment is wrong.
Floor 6 Posted 2007-01-06 13:16 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Hehe, there is indeed a flaw. I was too quick when writing the code and added an extra parameter d.

As for the situation where an error occurs because the "target folder" does not exist, it should not be classified as a flaw because it is to detect the target folder. The default prerequisite is that this target folder exists, which belongs to implicit declaration rather than explicit declaration. Hehe, I used a term, which is like showing off in front of Guan Gong.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 7 Posted 2007-01-07 07:35 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Today when I was looking through old posts, I found that I once wrote a more concise code for this problem (please click here to view), I actually forgot it myself, heh, unexpected gain. After modifying a few characters, I will post it again:

@echo off
dir /a /b "target folder"|findstr .>nul&& echo has files || echo no files
pause
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +5 2007-01-07 07:38
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 8 Posted 2007-01-07 08:00 ·  中国 广东 广州 天河区 电信
高级用户
★★★
潜水修练批处理
Credits 788
Posts 366
Joined 2006-12-31 02:43
19-year member
UID 75048
Gender Male
Status Offline
Some files have no file extensions...
Floor 9 Posted 2007-01-07 08:09 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
In the findstr command, the dot is not an ordinary character but a special symbol in regular expressions, meaning any character, which has the same function as? or * in the dir command: wildcard. Files without extensions can also be processed correctly.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 10 Posted 2007-01-07 08:16 ·  中国 广东 广州 天河区 电信
高级用户
★★★
潜水修练批处理
Credits 788
Posts 366
Joined 2006-12-31 02:43
19-year member
UID 75048
Gender Male
Status Offline
一般很少用findstr,还以为那个“.”是后缀的那个:P
Floor 11 Posted 2007-01-08 06:02 ·  中国 黑龙江 哈尔滨 联通
新手上路
Credits 19
Posts 10
Joined 2006-04-29 13:39
20-year member
UID 54663
Gender Male
Status Offline
Floor 12 Posted 2007-03-07 13:13 ·  中国 广东 深圳 龙岗区 电信
高级用户
★★★
Credits 793
Posts 312
Joined 2004-09-02 00:00
21-year member
UID 31104
Gender Male
Status Offline
Originally posted by namejm at 2007-1-7 07:35 AM:
  When I was flipping through old posts today, I found that I once wrote a more concise code for this problem (please click here

This is really a gem.
Forum Jump: