|
onlykier
初级用户
 
积分 50
发帖 23
注册 2006-11-3
状态 离线
|
『楼 主』:
如何给dir结果中的文件名加上完整路径
使用 LLM 解释/回答一下
假设一个目录下有子目录,子目录下又有子目录,用如下命令
dir /s /a-d /tc|sort|find "-"|find /v "序列" 结果类此如下
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
我想在文件名之前显示该文件的目录名,结果像这样,请问该如何做?
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
──────────────── 版务记录 ────────────────
执行:namejm
原标题:问个dir的问题
修改后标题:如何给dir结果中的文件名加上完整路径
说明:因标题过于模糊,不便于论坛的搜索和管理,在本提示发表三日后,楼主尚未修改
标题,现由本人强制修改标题,并扣除楼主2点积分。
──────────────── 版务记录 ────────────────
Last edited by namejm on 2007-2-3 at 08:38 PM ]
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 ]
|
|
2006-11-7 23:22 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
|
2006-11-7 23:27 |
|
|
onlykier
初级用户
 
积分 50
发帖 23
注册 2006-11-3
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
日期 和文件名要,其他要不要都行
Dates and file names are needed, others are optional
|
|
2006-11-7 23:33 |
|
|
onlykier
初级用户
 
积分 50
发帖 23
注册 2006-11-3
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
dir /s /a-d /tc|sort|find "-"|find /v "序列"
这句话的意思是
/s 显示当前目录及子目录的文件
/a-d 结果中去除文件夹的信息
/tc 按创建时间显示
|sort 是对dir /s/a-d/tc 的结果进行排序,按创建时间从早到晚排序
|find "-" 对dir /s /a-d /tc|sort 的结果进行筛选,只显示带有"-"的行
|find /v "序列" 的作用是,由于dir /s /a-d /tc|sort|find "-"显示的结果中有一行结果是不相要的,所以对dir /s /a-d /tc|sort|find 的结果中去除带有"序列"的那行
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
|
|
2006-11-7 23:35 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
bat里用:
FOR /F "tokens=*" %%i IN ('dir/s/b/od') DO @echo %%~ti %%i
CMD里用
FOR /F "tokens=*" %i IN ('dir/s/b/od') DO @echo %~ti %i
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
|

我的网络U盘 我的网络第2个U盘
论坛软件下载链接
灵雨飘零论坛
论坛新手必读,所有人的基本行为准则
刷QQ空间人气、留言的小软件 |
|
2006-11-8 00:31 |
|
|
onlykier
初级用户
 
积分 50
发帖 23
注册 2006-11-3
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
我要文件的目录位置。请楼上的兄弟看下1楼的原意
I need the directory location of the file. Please let the brother upstairs see the original meaning of floor 1.
|
|
2006-11-8 01:57 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
5F的只满足了3F的其中一个需求,而对另一个需求不做处理,在5F的基础上得到如下代码:
@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 ]
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-8 02:09 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
|
2006-11-8 04:40 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
|
2006-11-8 04:45 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
Originally posted by 不得不爱 at 2006-11-7 16:40:
可是我测试了下7楼的代码和5楼的代码是一样的输出啊
sorry,没有对5F和7F的代码做对比测试,导致我主观地认为5F的代码和7F的代码有所差别,实际上它们显示的结果是一样的。
不过,用 for 语句中的 %%~ti 扩展只能探测到文件的最后改动时间,而不能探测到文件的创建时间。要提取创建时间的话,还是需要用 dir 里的 /tc。请楼主测试以下代码,看是不是你所需要的:
@echo off
for /f "tokens=1-3*" %%i in ('dir /a-d /tc /s^|findstr /b ""^|sort') do echo %%i %%j %%~dpnxl
pause
发现用 %%~dpnxl 来扩展文件的完整路径的时候,居然是直接在文件名前简单地加上批处理所在的路径,看来这个问题还真的有点棘手,可能得通过产生临时文件的方法才能完全满足楼主的要求。
Last edited by namejm on 2006-11-7 at 08:26 PM ]
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-8 07:03 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
找到了一个解决方案,美中不足的是:产生了两个临时文件。
@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"
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-8 11:23 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
效率较低,不过不会生成临时文件
@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
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
|
|
2006-11-8 13:11 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
楼上的无法运行!
@echo off
for /f "tokens=1-3*" %%a in ('dir/s/a-d/tc^|sort^|find "-"') do echo %%a|find "序列号">nul||echo %%a %%b %%~dpnxd
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
|

我的网络U盘 我的网络第2个U盘
论坛软件下载链接
灵雨飘零论坛
论坛新手必读,所有人的基本行为准则
刷QQ空间人气、留言的小软件 |
|
2006-11-8 23:07 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
我在10F说过,for 语句的 %%~ti 只能探测到文件的最后改动时间,而 %%~dpnxi 只是简单地在文件名之前加上批处理所在的路径,假设 test.bat 位于 d:\test 下,而test.txt 位于 d:\test\abc\123 下,那么,%%~dpnxi 只会把 test.txt 扩展成 d:\test\test.txt 而不是 d:\test\abc\123\test.txt ,所以,同时使用 %%~ti 和 %%~dpnxi(或者%%~fi) 的做法是不正确的。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-11-9 01:47 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
我 12F 的测试过,可以运行,只不过LZ的后一段:findstr "-"^|find /v "序列" 测试时我用的是^|findstr ".mmf"(mmf为手机铃声文件)
for /f "delims=" %%a in ('dir/b/s "%%l"') do set Name=%%~fa
这一行是再搜索一次"%%l"的文件,%%~fa获得正确路径,因为再次dir/s搜索,所以效率降低了
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
|
|
2006-11-9 04:51 |
|