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-04 12:10
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » A bug when using dir /a-d /b /s to search the root directory of a partition and all its subdirectories View 2,332 Replies 11
Original Poster Posted 2007-03-09 04:24 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Suppose: I want to list the complete paths of all text files named test.txt under drive E. I used the following code:


@echo off
for /f "delims=" %%i in ('dir /a-d /b /s e:\test.txt') do echo "%%~dpi"
pause


If there happens to be a folder named test.txt under the root directory of drive E, then there will be a ruckus: the path E:\test.txt will be displayed as many times as there are files under E:\test.txt! If there are no files under E:\test.txt, the path E:\test.txt will not be displayed at all. If the folder test.txt exists in a subfolder of drive E, such a situation will not occur.

A little analysis reveals that the reason is this: the name test.txt is ambiguous: it can represent both a text file named test and a folder named test.txt; and the dir command for searching for a specified file in the entire partition must be written in the format dir /a-d /s e:\test.txt. In this way, because of the ambiguity of test.txt, dir will search for all types of files under the folder e:\test.txt and also search for all text files named test under drive E.

The solution is: check the result after dir. The code can be modified as:


@echo off
for /f "delims=" %%i in ('dir /a-d /b /s e:\test.txt') do (
if /i "%%~nxi"=="test.txt" echo "%%~dpi"
)
pause


[ Last edited by namejm on 2007-3-8 at 03:52 PM ]
Recent Ratings for This Post ( 6 in total) Click for details
RaterScoreTime
lxmxn +4 2007-03-09 04:30
redtek +20 2007-03-09 04:56
ccwan +11 2007-03-09 05:04
NaturalJ0 +15 2007-03-09 05:30
huzixuan +2 2007-03-11 01:25
zouzhxi +4 2007-04-03 07:25
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 2 Posted 2007-03-09 05:09 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Hehe,Ifollowthetrainofthoughtoftheownertowriteasectionofcodebar,thiscodetrainofthoughtistofirstextractalltxt,thencheckthefilename,andthejmcodeistofirstextractalltest.txtandE:\test.txtallfiles(includingsubdirectoriesinthedirectory),thencheckthefilename
@echo off
for /f "delims=" %%i in ('dir /a-d /b /s e:\*.txt') do (
if /i "%%~ni"=="test" do echo "%%~dpi"
)
pause


If the number of txt files is huge, the jm code will be a bit faster
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
namejm +4 2007-03-09 05:11
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 3 Posted 2007-03-09 23:20 ·  中国 安徽 马鞍山 电信
中级用户
★★
Credits 493
Posts 228
Joined 2007-02-16 00:38
19-year member
UID 79596
Gender Male
From 安徽
Status Offline
If there are several test.txt files,
How to find the one of a certain date?
Floor 4 Posted 2007-03-10 07:42 ·  中国 安徽 阜阳 电信
初级用户
Credits 42
Posts 19
Joined 2006-12-28 01:20
19-year member
UID 74752
Gender Male
Status Offline
Got it, I've learned something. As a newbie, I got some inspiration from this. The moderator is really awesome!
Floor 5 Posted 2007-03-10 07:46 ·  中国 安徽 阜阳 电信
初级用户
Credits 42
Posts 19
Joined 2006-12-28 01:20
19-year member
UID 74752
Gender Male
Status Offline
The following is the translation:

if /i "%%~nxi"=="test.txt" I don't understand what this sentence means. As a result, I removed this sentence and it can also work. Hehe
@echo off
for /f "delims=" %%i in ('dir /a-d /b /s 'dir /a-d /b /s e:\test.txt') do (
echo "%%~dpi"
)
Floor 6 Posted 2007-03-11 01:06 ·  中国 安徽 马鞍山 电信
中级用户
★★
Credits 493
Posts 228
Joined 2007-02-16 00:38
19-year member
UID 79596
Gender Male
From 安徽
Status Offline
Originally posted by shuaigeya at 2007-3-9 18:46:
I don't understand the meaning of the sentence "if /i "%%~nxi"=="test.txt"". As a result, I removed this sentence and it still works. Hehe
@echo off
for /f "delims=" %%i in ('dir /a-d /b /s 'd ...


for /?



In addition, the replacement of FOR variable references has been enhanced. You can now use the following
option syntax:

~I - Remove any quotation marks ("), expand %I
%~fI - Expand %I to a fully qualified pathname
%~dI - Expand %I to only a drive letter
%~pI - Expand %I to only a path
%~nI - Expand %I to only a file name
%~xI - Expand %I to only a file extension
%~sI - The expanded path only contains the short name
%~aI - Expand %I to the file attributes of the file
%~tI - Expand %I to the date/time of the file
%~zI - Expand %I to the size of the file
%~$PATH:I - Find the directory listed in the path environment variable, and expand %I
to the first fully qualified name found. If the environment variable name
is not defined, or the file is not found, this combination key will expand to
empty string

Multiple results can be obtained by combining modifiers:

%~dpI - Expand %I to only a drive letter and path
%~nxI - Expand %I to only a file name and extension
%~fsI - Expand %I to a complete pathname with a short name only
%~dp$PATH:i - Find the directory listed in the path environment variable, and expand %I
to the first drive letter and path found.
%~ftzaI - Expand %I to a DIR-like output line
Floor 7 Posted 2007-03-11 01:26 ·  中国 安徽 芜湖 电信
高级用户
★★
Credits 537
Posts 219
Joined 2006-10-31 21:08
19-year member
UID 69036
Gender Male
From 芜湖
Status Offline
So it is!

if /i "%%~nxi"=="test.txt" echo "%%~dpnxi"

Used cleverly, used well!

@echo off
for /f "delims=" %%i in ('dir /a-d /b /s e:\test.txt') do (
if /i "%%~nxi"=="test.txt" echo "%%~dpnxi"
)
pause
江湖远
碧空长
路茫茫

一个人漫无目的的奔跑,风,刺骨的冷....
Floor 8 Posted 2007-03-14 22:08 ·  中国 江苏 南京 电信
初级用户
Credits 76
Posts 39
Joined 2007-03-09 06:54
19-year member
UID 81169
Gender Male
Status Offline
Got it, it's a bit clear now. The explanation given by xycoordinate on the 6th floor is really good, saving me from looking it up myself.......
狐狸喜欢狡猾。。
Floor 9 Posted 2007-04-03 00:18 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
Actually, this is also okay.

@echo off
for /r e:\ %%i in (*.txt) do (
if /i "%%~nxi"=="test.txt" echo "%%~dpi"
)
pause
Floor 10 Posted 2007-04-27 09:47 ·  中国 广东 惠州 电信
新手上路
Credits 10
Posts 5
Joined 2006-12-01 12:21
19-year member
UID 72277
Gender Male
Status Offline
Floor 11 Posted 2007-04-30 04:00 ·  中国 广东 茂名 电信
新手上路
Credits 12
Posts 6
Joined 2007-04-21 13:21
19-year member
UID 85989
Gender Male
Status Offline
I'm dizzy. You are all experts. I'm new here. I hope for your care.... Even the DIR command I have a lot of don't understand. Depressed
Floor 12 Posted 2007-05-02 15:28 ·  中国 云南 昆明 电信
初级用户
Credits 57
Posts 29
Joined 2006-12-27 09:36
19-year member
UID 74694
Gender Male
Status Offline
Upvoted. All are experts. Amazing!
Forum Jump: