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:
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).
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:
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 ]
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没商量。
考虑问题复杂化,解决问题简洁化。
考虑问题复杂化,解决问题简洁化。

