![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-14 18:03 |
47,812 topics / 349,917 posts / today 0 new / 48,268 members |
| DOS批处理 & 脚本技术(批处理室) » Methods to detect whether a folder is an empty directory |
| Printable Version 3,902 / 11 |
| Floor1 namejm | Posted 2006-12-05 11:42 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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 ] |
|
| Floor2 youxi01 | Posted 2006-12-05 13:42 |
| 高级用户 Posts 247 Credits 846 From 湖南==》广东 | |
|
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: [ Last edited by youxi01 on 2006-12-5 at 01:45 PM ] |
|
| Floor3 namejm | Posted 2006-12-06 00:01 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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.
|
|
| Floor4 namejm | Posted 2007-01-06 11:35 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
Based on the statement in building 2, a more concise and efficient code is obtained:
Thanks to the reminder from the downstairs, an error was modified. [ Last edited by namejm on 2007-1-6 at 12:17 AM ] |
|
| Floor5 qzwqzw | Posted 2007-01-06 12:39 |
| 银牌会员 Posts 636 Credits 2,343 | |
|
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.
|
|
| Floor6 namejm | Posted 2007-01-06 13:16 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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. |
|
| Floor7 namejm | Posted 2007-01-07 07:35 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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:
|
|
| Floor8 dikex | Posted 2007-01-07 08:00 |
| 高级用户 Posts 366 Credits 788 | |
|
Some files have no file extensions...
|
|
| Floor9 namejm | Posted 2007-01-07 08:09 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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.
|
|
| Floor10 dikex | Posted 2007-01-07 08:16 |
| 高级用户 Posts 366 Credits 788 | |
|
一般很少用findstr,还以为那个“.”是后缀的那个:P
|
|
| Floor11 ocnzhao | Posted 2007-01-08 06:02 |
| 新手上路 Posts 10 Credits 19 | |
| Floor12 willsion | Posted 2007-03-07 13:13 |
| 高级用户 Posts 312 Credits 793 | |
Originally posted by namejm at 2007-1-7 07:35 AM: |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |