- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
4#
發表於 2014-9-11 16:28
| 只看該作者
回復 1# li_hsien
試試看- Option Explicit
- Sub SF_collection_Click()
- Dim 目的目錄 As String, 搜尋目錄 As String, T As Date, Fs As Object, Sf As Object, f As Object
- Dim i As Integer, 檔名 As String, 副檔名 As String, 檔名_計數 As Integer, MyDir As String
- 目的目錄 = "D:\"
- 搜尋目錄 = "C:\test"
- T = Time
- Set Fs = CreateObject("Scripting.FileSystemObject")
- Set Sf = Fs.GetFolder(搜尋目錄).SubFolders
- For Each f In Sf
- With Application.FileSearch
- .FileType = msoFileTypeExcelWorkbooks
- .LookIn = f '傳回大寫的資料夾名稱
- .Filename = "*.*"
- .Execute
- For i = 1 To .FoundFiles.Count
- 檔名 = Fs.GetBaseName(.FoundFiles(i))
- 副檔名 = Fs.GetExtensionName(.FoundFiles(i))
- 檔名_計數 = 0
- MyDir = Dir(目的目錄 & 檔名 & "*." & 副檔名, vbDirectory)
- Do While MyDir <> ""
- 檔名_計數 = 檔名_計數 + 1
- MyDir = Dir
- Loop
- If 檔名_計數 > 0 Then
- 檔名 = 目的目錄 & 檔名 & "(" & 檔名_計數 & ")." & 副檔名
- Else
- 檔名 = 目的目錄 & 檔名 & "." & 副檔名
- End If
- FileCopy .FoundFiles(i), 檔名
- Next
- End With
- Next
- Debug.Print "經過時間: " & DateDiff("n", T, Time) & "分"
- End Sub
複製代碼 |
|