中国DOS联盟论坛

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-08-04 13:12
48,038 topics / 350,123 posts / today 0 new / 48,251 members
DOS批处理 & 脚本技术(批处理室) » A bug when using dir /a-d /b /s to search the root directory of a partition and all its subdirectories
Printable Version  2,333 / 11
Floor1 namejm Posted 2007-03-09 04:24
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Suppose: I want to list the complete paths of all text files named test.txt under drive E. I used the following code:



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:



[ Last edited by namejm on 2007-3-8 at 03:52 PM ]
Floor2 ccwan Posted 2007-03-09 05:09
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
Hehe,Ifollowthetrainofthoughtoftheownertowriteasectionofcodebar,thiscodetrainofthoughtistofirstextractalltxt,thencheckthefilename,andthejmcodeistofirstextractalltest.txtandE:\test.txtallfiles(includingsubdirectoriesinthedirectory),thencheckthefilename


If the number of txt files is huge, the jm code will be a bit faster
Floor3 xycoordinate Posted 2007-03-09 23:20
中级用户 Posts 228 Credits 493 From 安徽
If there are several test.txt files,
How to find the one of a certain date?
Floor4 shuaigeya Posted 2007-03-10 07:42
初级用户 Posts 19 Credits 42
Got it, I've learned something. As a newbie, I got some inspiration from this. The moderator is really awesome!
Floor5 shuaigeya Posted 2007-03-10 07:46
初级用户 Posts 19 Credits 42
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"
)
Floor6 xycoordinate Posted 2007-03-11 01:06
中级用户 Posts 228 Credits 493 From 安徽
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
Floor7 huzixuan Posted 2007-03-11 01:26
高级用户 Posts 219 Credits 537 From 芜湖
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
Floor8 hxmupdata Posted 2007-03-14 22:08
初级用户 Posts 39 Credits 76
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.......
Floor9 wudixin96 Posted 2007-04-03 00:18
银牌会员 Posts 931 Credits 1,928
Actually, this is also okay.

@echo off
for /r e:\ %%i in (*.txt) do (
if /i "%%~nxi"=="test.txt" echo "%%~dpi"
)
pause
Floor10 senjieg Posted 2007-04-27 09:47
新手上路 Posts 5 Credits 10
Floor11 taozicici Posted 2007-04-30 04:00
新手上路 Posts 6 Credits 12
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
Floor12 ccuu668 Posted 2007-05-02 15:28
初级用户 Posts 29 Credits 57
Upvoted. All are experts. Amazing!
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023