如何从Dos命令Dir的输出中提取文件名 ( 积分: 100 )

  • 主题发起人 主题发起人 catcatdogdog
  • 开始时间 开始时间
C

catcatdogdog

Unregistered / Unconfirmed
GUEST, unregistred user!
我想从Dir的输出把文件名提取出来放到一个列表里。仅仅文件名和扩展名,不要时间、大小。<br>用子串处理好像有难度,因为不同操作系统下文件名在一行中的位置还不一样。在/w参数下一行显示的多少也不一样。<br><br>Dir以后大概是这个样子:<br><br> F:/ 的目录<br><br>2004-12-18 &nbsp;15:13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 15,474,760 angz.exe<br>2005-01-18 &nbsp;07:34 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1,111,072 assist4.exe<br>2005-01-16 &nbsp;22:18 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5,276,659 ayda601.exe<br>2005-01-31 &nbsp;16:56 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;542,976 ChinaClawbd.exe<br>2005-02-19 &nbsp;12:01 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;504,320 daemon347.exe<br>2005-02-19 &nbsp;16:42 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;9,411,439 Delphi7.1 Update.exe<br>2005-02-19 &nbsp;12:38 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;685,010,944 delphi7.iso<br>2003-05-14 &nbsp;16:00 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;297,528 directx90Awebsetup.exe<br>2005-04-30 &nbsp;13:04 &nbsp; &nbsp; &nbsp; &lt;DIR&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Driver<br>2005-04-01 &nbsp;10:23 &nbsp; &nbsp; &nbsp; &lt;DIR&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;EditPlus.v2.12<br>2005-01-31 &nbsp;11:13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1,668,159 fg165.exe<br>2003-12-17 &nbsp;08:40 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;683,132 flashplayer7installer.exe<br>2005-01-24 &nbsp;06:23 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4,648,151 IPguardSCN.zip<br>2005-01-18 &nbsp;04:26 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 54,450,037 j2sdk-1_4_2_07-windows-i586-p.exe<br>2005-02-19 &nbsp;13:47 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5,556,616 MDAC_TYP.EXE<br>2005-02-19 &nbsp;11:58 &nbsp; &nbsp; &nbsp; &lt;DIR&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nav<br>2005-01-05 &nbsp;04:39 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;242,919 nettools.rar<br>2004-09-07 &nbsp;16:57 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 21,996,877 NPF_Retail.EXE<br>2005-02-19 &nbsp;12:13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;682,854,400 OFFICE2K3_SC.ISO<br>2005-01-31 &nbsp;11:09 &nbsp; &nbsp; &nbsp; &lt;DIR&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RAR<br>2003-07-20 &nbsp;11:46 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 77,172 Reset5.02.rar<br>2005-02-19 &nbsp;12:26 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;710,950,912 SQL2KCN.iso<br>2004-07-18 &nbsp;13:46 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 32,772 winmd5.exe<br>2005-02-04 &nbsp;07:59 &nbsp; &nbsp; &nbsp; &lt;DIR&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WUTemp<br>2005-02-04 &nbsp;09:42 &nbsp; &nbsp; &nbsp; &lt;DIR&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;制作帮助文件<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;19 个文件 &nbsp;2,200,790,845 字节<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6 个目录 22,281,732,096 可用字节<br><br>加上/w参数后得到的是:<br><br> F:/ 的目录<br><br>angz.exe &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;assist4.exe<br>ayda601.exe &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChinaClawbd.exe<br>daemon347.exe &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Delphi7.1 Update.exe<br>delphi7.iso &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; directx90Awebsetup.exe<br>[Driver] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[EditPlus.v2.12]<br>fg165.exe &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flashplayer7installer.exe<br>IPguardSCN.zip &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j2sdk-1_4_2_07-windows-i586-p.exe<br>MDAC_TYP.EXE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[nav]<br>nettools.rar &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NPF_Retail.EXE<br>OFFICE2K3_SC.ISO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[RAR]<br>Reset5.02.rar &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQL2KCN.iso<br>winmd5.exe &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[WUTemp]<br>[制作帮助文件] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;19 个文件 &nbsp;2,200,790,845 字节<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6 个目录 22,281,732,096 可用字节
 
我想从Dir的输出把文件名提取出来放到一个列表里。仅仅文件名和扩展名,不要时间、大小。<br>用子串处理好像有难度,因为不同操作系统下文件名在一行中的位置还不一样。在/w参数下一行显示的多少也不一样。<br><br>Dir以后大概是这个样子:<br><br> F:/ 的目录<br><br>2004-12-18 &nbsp;15:13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 15,474,760 angz.exe<br>2005-01-18 &nbsp;07:34 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1,111,072 assist4.exe<br>2005-01-16 &nbsp;22:18 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5,276,659 ayda601.exe<br>2005-01-31 &nbsp;16:56 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;542,976 ChinaClawbd.exe<br>2005-02-19 &nbsp;12:01 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;504,320 daemon347.exe<br>2005-02-19 &nbsp;16:42 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;9,411,439 Delphi7.1 Update.exe<br>2005-02-19 &nbsp;12:38 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;685,010,944 delphi7.iso<br>2003-05-14 &nbsp;16:00 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;297,528 directx90Awebsetup.exe<br>2005-04-30 &nbsp;13:04 &nbsp; &nbsp; &nbsp; &lt;DIR&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Driver<br>2005-04-01 &nbsp;10:23 &nbsp; &nbsp; &nbsp; &lt;DIR&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;EditPlus.v2.12<br>2005-01-31 &nbsp;11:13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1,668,159 fg165.exe<br>2003-12-17 &nbsp;08:40 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;683,132 flashplayer7installer.exe<br>2005-01-24 &nbsp;06:23 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4,648,151 IPguardSCN.zip<br>2005-01-18 &nbsp;04:26 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 54,450,037 j2sdk-1_4_2_07-windows-i586-p.exe<br>2005-02-19 &nbsp;13:47 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5,556,616 MDAC_TYP.EXE<br>2005-02-19 &nbsp;11:58 &nbsp; &nbsp; &nbsp; &lt;DIR&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nav<br>2005-01-05 &nbsp;04:39 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;242,919 nettools.rar<br>2004-09-07 &nbsp;16:57 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 21,996,877 NPF_Retail.EXE<br>2005-02-19 &nbsp;12:13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;682,854,400 OFFICE2K3_SC.ISO<br>2005-01-31 &nbsp;11:09 &nbsp; &nbsp; &nbsp; &lt;DIR&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RAR<br>2003-07-20 &nbsp;11:46 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 77,172 Reset5.02.rar<br>2005-02-19 &nbsp;12:26 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;710,950,912 SQL2KCN.iso<br>2004-07-18 &nbsp;13:46 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 32,772 winmd5.exe<br>2005-02-04 &nbsp;07:59 &nbsp; &nbsp; &nbsp; &lt;DIR&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WUTemp<br>2005-02-04 &nbsp;09:42 &nbsp; &nbsp; &nbsp; &lt;DIR&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;制作帮助文件<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;19 个文件 &nbsp;2,200,790,845 字节<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6 个目录 22,281,732,096 可用字节<br><br>加上/w参数后得到的是:<br><br> F:/ 的目录<br><br>angz.exe &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;assist4.exe<br>ayda601.exe &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChinaClawbd.exe<br>daemon347.exe &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Delphi7.1 Update.exe<br>delphi7.iso &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; directx90Awebsetup.exe<br>[Driver] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[EditPlus.v2.12]<br>fg165.exe &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flashplayer7installer.exe<br>IPguardSCN.zip &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j2sdk-1_4_2_07-windows-i586-p.exe<br>MDAC_TYP.EXE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[nav]<br>nettools.rar &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NPF_Retail.EXE<br>OFFICE2K3_SC.ISO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[RAR]<br>Reset5.02.rar &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQL2KCN.iso<br>winmd5.exe &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[WUTemp]<br>[制作帮助文件] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;19 个文件 &nbsp;2,200,790,845 字节<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6 个目录 22,281,732,096 可用字节
 
执行Dir &gt;&gt;C:/mytemp.dat<br>再打开C:/mytemp.dat这个文件,分析一下就出来了
 
现在能得到输出,就是需要解决如何分析的问题。<br>且把输出看作是Strings吧,但这个Strings中的String是没有好掌握的规律的。
 
或者..........<br>如果只提文件名,不含目录,还是用FileListBox控件方便.<br>如果要含有目录可以用FindFileFirst,FindFileNext
 
只能从Dir或是别的Dos命令的输出中提取,而不是通过别的方式读文件系统。<br>否则就很简单了。
 
用正则表达式 处理一下
 
sa_daydayup:<br>什么样的正则表达式,能说详细点吗?
 
&gt;&gt;且把输出看作是Strings吧,但这个Strings中的String是没有好掌握的规律的<br>本来想帮你写的.不过没时间.要下周了.<br>这样处理:<br>a=1<br>从左到右取字符,当遇到tab符或空格时之后再遇到非这两个字符时记下这个位置(b),取到一个字符串(copy(str,a,b-a+1)),trim后排除日期,时间,数值,'&lt;DIR&gt;'的情况后就是文件名或目录.从刚才记下的位置(a=b)继续取下个位置(b),再(copy(str,a,b-a+1)),.....循环,只到检查完字符串.
 
多人接受答案了。
 
后退
顶部