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-06-25 06:34
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to add the full path to the file names in the dir result View 5,359 Replies 17
Original Poster Posted 2006-11-07 23:22 ·  中国 内蒙古 呼和浩特 电信
初级用户
Credits 50
Posts 23
Joined 2006-11-03 23:05
19-year member
UID 69459
Gender Male
Status Offline
Suppose there are subdirectories under a directory, and there are subdirectories under the subdirectories. Use the following command
dir /s /a-d /tc|sort|find "-"|find /v "Sequence" The result is similar to the following
2006-10-01 19:49 123,392 asdfasdfasd060921-060927.xls
2006-10-01 19:49 123,904 asdfasdfasd060914-060920.xls
2006-10-01 19:49 161,280 asdfasdfasd060831-060906.xls
2006-10-01 19:49 167,936 asdfasdfasd060907-060913.xls
2006-10-07 19:08 160,768 asdfasdfasd060824-060830.xls
2006-10-07 19:13 124,928 asdfasdfasd060928-061004.xls
2006-10-12 00:17 124,416 asdfasdfasd061005-061011.xls
2006-10-19 08:18 124,928 asdfasdfasd061012-061018.xls
2006-10-26 12:22 124,928 asdfasdfasd061019-061025.xls

I want to display the directory name of the file before the file name, and the result is like this. How to do it?
2006-10-01 19:49 123,392 d:\x1\asdfasdfasd060921-060927.xls
2006-10-01 19:49 123,904 d:\x2\asdfasdfasd060914-060920.xls
2006-10-01 19:49 161,280 d:\x2\1\asdfasdfasd060831-060906.xls
2006-10-01 19:49 167,936 d:\x2\2\asdfasdfasd060907-060913.xls
2006-10-07 19:08 160,768 d:\x2\3\asdfasdfasd060824-060830.xls
2006-10-07 19:13 124,928 d:\x4\asdfasdfasd060928-061004.xls
2006-10-12 00:17 124,416 d:\x5\asdfasdfasd061005-061011.xls
2006-10-19 08:18 124,928 d:\x4\1\asdfasdfasd061012-061018.xls
2006-10-26 12:22 124,928 d:\x4\3\asdfasdfasd061019-061025.xls


──────────────── Moderation Record ────────────────
Performed by: namejm
Original title: Ask a dir question
Modified title: How to add the full path to the file name in the dir result
Explanation: The title is too vague, which is not convenient for forum search and management. Three days after this prompt is posted, the original poster has not modified the title. Now, the title is forcibly modified by me, and 2 points of points are deducted from the original poster.
──────────────── Moderation Record ────────────────


[ Last edited by namejm on 2007-2-3 at 08:38 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
namejm -2 2007-02-04 09:43
Floor 2 Posted 2006-11-07 23:27 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 3 Posted 2006-11-07 23:33 ·  中国 内蒙古 呼和浩特 电信
初级用户
Credits 50
Posts 23
Joined 2006-11-03 23:05
19-year member
UID 69459
Gender Male
Status Offline
Dates and file names are needed, others are optional
Floor 4 Posted 2006-11-07 23:35 ·  中国 内蒙古 呼和浩特 电信
初级用户
Credits 50
Posts 23
Joined 2006-11-03 23:05
19-year member
UID 69459
Gender Male
Status Offline
The meaning of this sentence is:
/s displays files in the current directory and subdirectories
/a-d removes folder information from the results
/tc displays by creation time
|sort sorts the results of dir /s/a-d/tc, sorting from earliest to latest by creation time
|find "-" filters the results of dir /s /a-d /tc|sort to only show lines with "-"
|find /v "序列" has the function that since there is a line of result in the result of dir /s /a-d /tc|sort|find "-" that is not wanted, so it removes the line with "序列" from the result of dir /s /a-d /tc|sort|find
Floor 5 Posted 2006-11-08 00:31 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
In the bat file, use:
FOR /F "tokens=*" %%i IN ('dir/s/b/od') DO @echo %%~ti %%i
In the CMD, use:
FOR /F "tokens=*" %i IN ('dir/s/b/od') DO @echo %~ti %i
Floor 6 Posted 2006-11-08 01:57 ·  中国 内蒙古 呼和浩特 电信
初级用户
Credits 50
Posts 23
Joined 2006-11-03 23:05
19-year member
UID 69459
Gender Male
Status Offline
I need the directory location of the file. Please let the brother upstairs see the original meaning of floor 1.
Floor 7 Posted 2006-11-08 02:09 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The 5F only satisfied one of the requirements of the 3F, and did not handle the other requirement. Based on the 5F, the following code is obtained:

@echo off
for /f "delims=" %%i in ('dir /a-d /b /s /od') do echo %%~ti %%~dpnxi
pause


[ Last edited by namejm on 2006-11-7 at 02:15 PM ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 8 Posted 2006-11-08 04:40 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 9 Posted 2006-11-08 04:45 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 10 Posted 2006-11-08 07:03 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by Have to Love at 2006-11-7 16:40:
But I tested and the output of the code on floor 7 is the same as that on floor 5.


  Sorry, I didn't compare and test the code of 5F and 7F, which led me to subjectively think that there were differences between the code of 5F and 7F. In fact, the results they display are the same.

  However, the %%~ti extension in the for statement can only detect the last modification time of the file, but not the creation time. To extract the creation time, you still need to use /tc in dir. Please test the following code by the landlord to see if it is what you need:

@echo off
for /f "tokens=1-3*" %%i in ('dir /a-d /tc /s^|findstr /b ""^|sort') do echo %%i %%j %%~dpnxl
pause

  I found that when using %%~dpnxl to extend the full path of the file, it was simply adding the path where the batch file is located in front of the file name. It seems that this problem is really a bit tricky. Maybe the method of generating a temporary file is needed to fully meet the landlord's requirements.

[ Last edited by namejm on 2006-11-7 at 08:26 PM ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 11 Posted 2006-11-08 11:23 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Found a solution, but the drawback is: two temporary files are generated.

@echo off
cd.>"%tmp%\tmp.txt"
setlocal enabledelayedexpansion
for /f "tokens=1-3*" %%i in ('dir /a-d /tc /s^|findstr ":"') do (
set var=%%i%%j%%k%%l
if "!var:~1,1!"==":" set route=!var!
if not "!route!"=="!var!" if not "%%j"=="" echo %%i %%j !route:~0,-3!\%%l>>"%tmp%\tmp.txt"
)
sort<"%tmp%\tmp.txt">"%tmp%\list.txt"
del "%tmp%\tmp.txt"
start "" "%tmp%\list.txt"
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 12 Posted 2006-11-08 13:11 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Low efficiency, but no temporary files will be generated
@echo off
cd.>list.txt
setlocal EnableDelayedExpansion
for /f "tokens=1-3* delims= " %%i in ('dir/a-d/tc/s ^|sort^|findstr "-"^|find /v "序列"') do (
for /f "delims=" %%a in ('dir/b/s "%%l"') do set Name=%%~fa
echo %%i %%j %%k !Name!>>list.txt
)
exit
Floor 13 Posted 2006-11-08 23:07 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
The one upstairs can't run!
@echo off
for /f "tokens=1-3*" %%a in ('dir/s/a-d/tc^|sort^|find "-"') do echo %%a|find "serial number">nul||echo %%a %%b %%~dpnxd
Floor 14 Posted 2006-11-09 01:47 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
I mentioned in 10F that the %%~ti in the for statement can only detect the last modification time of the file, and %%~dpnxi simply adds the path where the batch file is located before the file name. Suppose test.bat is located under d:\test, and test.txt is located under d:\test\abc\123, then %%~dpnxi will only expand test.txt to d:\test\test.txt instead of d:\test\abc\123\test.txt. Therefore, using %%~ti and %%~dpnxi (or %%~fi) at the same time is incorrect.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 15 Posted 2006-11-09 04:51 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
I tested it at 12F and it can run. However, for the latter part of the LZ: findstr "-"^|find /v "序列" When testing, I used ^|findstr ".mmf" (mmf is a mobile phone ringtone file)

for /f "delims=" %%a in ('dir/b/s "%%l"') do set Name=%%~fa This line is to search for files of "%%l" again. %%~fa gets the correct path. Because it searches dir/s again, so the efficiency is reduced
Forum Jump: