本帖最后由 香川群子 于 2015-2-10 11:28 编辑 Dos版 加入Dir各种参数以后的完整代码: Sub ListFilesDos() myMode& = Val(InputBox("Search Mode:-3 To 3", "Find File", 0)) '

Dos版 加入Dir各种参数以后的完整代码:Sub ListFilesDos()myMode& = Val(InputBox("Search Mode:-3 To 3", "Find File", 0)) '指定Dos Dir的查找开关、返回模式'奇数为不含子文件夹、偶数为含子文件夹 / 负数为目录、正数为文档 / >1为文档及目录If myMode > -3 ThenmyFile$ = InputBox("Part of Filename or Filetype as "".xl""", "Find File", ".xl")'输入指定关键字,可以是文件(文档和目录)名称中的任意部分,或指定文件类型如 ".xl"Set myFolder = CreateObject("Shell.Application").BrowseForFolder(0, "GetFolder", 0)If Not myFolder Is Nothing Then myPath$ = myFolder.Items.Item.path Else MsgBox "Folder not Selected": Exit Sub'浏览列表指定查找目录End Iftms = TimerWith CreateObject("Wscript.Shell") 'VBA调用Dos命令cmdStr = Choose(myMode + 4, "/? ", "/a:d /b /s ", "/a:d /b ", "/a:a /b /s ", "/a:a /b ", "/b /s ", "/b ", "/a:a /o:e /o:n /s ", "/a:a /o:e /o:n ", "/a:d /o:e /o:n /s ", "/a:d /o:e /o:n ")ar = Split(.exec("cmd /c dir " & cmdStr & Chr(34) & myPath & Chr(34)).StdOut.ReadAll, vbCrLf)'指定Dos中Dir命令的开关然后提取结果 为指定文件夹以及所含子文件夹内的所有文件的含路径全名。s = UBound(ar) & " Files by Search time: " & Format(Timer - tms, " 0.00s") & " in: " & myPathApplication.StatusBar = " Find " & s: tms = Timer '记录Dos中执行Dir命令的耗时 并在Excel状态栏上显示If myFile <> "" Then '如指定了匹配关键字则ar = Filter(ar, myFile) '按指定关键词myFile进行筛选。可筛选文件名或文件类型、然后在Excel状态栏上显示结果Application.StatusBar = Format(Timer - tms, "0.00s") & " Find " & 1 + UBound(ar) & " Files from " & sEnd IfEnd With[a:a] = "": If UBound(ar) > -1 Then [a2].Resize(1 + UBound(ar)) = WorksheetFunction.Transpose(ar)'    清空A列,然后输出结果End Sub复制代码为大家看得清楚明白,把各种开关写成Select形式:Select Case myMode '根据开关模式设置Dos Dir的开关参数Case -3cmdStr = "cmd /c dir /?" '列出Dir各个参数开关的帮助文件Case -2cmdStr = "cmd /c dir /a-a /b /s " & Chr(34) & myPath & Chr(34) '目录不含文档[/a-a]含子文件夹Case -1cmdStr = "cmd /c dir /a-a /b " & Chr(34) & myPath & Chr(34) '目录不含文档[/a-a](不含子文件夹)Case 0cmdStr = "cmd /c dir /a-d /b /s " & Chr(34) & myPath & Chr(34) '文档不含目录[/a-d]含子文件夹Case 1cmdStr = "cmd /c dir /a-d /b " & Chr(34) & myPath & Chr(34) '文档不含目录[/a-d](不含子文件夹)Case 2cmdStr = "cmd /c dir /b /s " & Chr(34) & myPath & Chr(34) '所有文档及目录含子文件夹Case 3cmdStr = "cmd /c dir /a-d /b " & Chr(34) & myPath & Chr(34) '所有文档及目录(不含子文件夹)End Select但实际代码中用Choose语句简化。回复

(0)

相关推荐