把老师的代码贴出来,方便大家分享! Sub CMD获取指定文件夹下文件() Dim exepath As String, txtpath As String, batpath As String,mypath As String, PutPath As String Dim f(1 To 20000), fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFolderPicker) With fd .Title = "请选择要遍历的文件夹:" .AllowMultiSelect =False .InitialFileName =ActiveDocument.Path & "\.\" If .Show <> -1Then Exit Sub mypath =.SelectedItems(1) End With t = Timer 'mypath = "G:\ExtremePro" '末尾不能带反斜杠\ '1、获取文件列表 txtpath = "C:\" & Format(Now(), "yyyymmddhhmmss")& ".txt" batpath = "c:\ListFiles.bat" temppath = "c:\temp.txt" n = FreeFile Open batpath For Output As #n Print #n, "dir/s /d /a:-d /b /w """ & mypath &""">" & txtpath If Dir(temppath)<> "" Then Kill "c:\temp.txt" Print #n,"dir/?>" & temppath Close #n Shell batpath Do While Dir(temppath) = "" DoEvents Loop n = FreeFile Open txtpath For Input As #n Do While Not EOF(n) fnum = fnum + 1 Line Input #n,f(fnum) Loop Close #n Kill txtpath Kill batpath Kill temppath MsgBox "用时" & Format(Timer - t, "0.00") & "秒,共" & fnum & "个文件的路径信息已存入数组!" End Sub
|