有网友问我要一个探测某个文件夹下哪些子目录为空的代码,原以为用 dir 会很简单地解决掉,结果一测试,才发现原来事情并非那么简单。其间变换了几种思路,前前后后竟然花费了两个小时才把问题解决好。以下是我的解题过程:
最开始,我以为可以先用 dir /ad /b /s 来获取所有子目录的路径,然后再通过获取 "dir /a 子目录" 的输出是否为空来判断子目录是否为空,代码如下:
@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"下没有文件
)
pause
可是,无论我如何变换环境测试,始终不能检测到空目录,百思不得其解,于是放弃了通过检测空值来探测空目录的想法。
改成下面的代码来查看test.txt,发现dir空目录的结果占了9行(包括空行)。
@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
于是想到了通过探测dir结果中信息行数的方法来探测文件夹是否为空,得到如下代码:
@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" 下没有文件
set num=0
)
pause
至此,问题得到解决。
后来,在群里又讨论了一番,发现上面的代码还可以优化,探测文件夹目录是否为空的方案还有好几种,比如 rd 方案,copy 方案。时间关系,其中的原理暂时不多说。
Last edited by namejm on 2007-1-6 at 12:22 AM ]
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 ]