返回列表 上一主題 發帖

刪除資料夾內的檔案

回復 1# man65boy
Kill "d:\*.xls"
Ps : Kill 來刪除一個已開啟的檔案,則會產生錯誤

TOP

回復 6# oobird
2003版是可以的,其他版我不知道 .

TOP

回復 8# linsurvey2005
小心使用, 可刪除一般資料夾中所指定的檔名.
  1. Option Explicit
  2. Dim xlDelName As String
  3. Dim xF As Object
  4. Sub Ex()
  5.     Dim FS As Object, d As Object, e As Object
  6.     xlDelName = "\*練習*"
  7.     On Error GoTo rr   '2003 繁體中:如目錄有簡體字會有錯誤
  8.     For Each d In CreateObject("Scripting.FileSystemObject").Drives  '磁碟機
  9.         If d.IsReady Then  '磁碟機已準備好
  10.             Set FS = CreateObject("Scripting.FileSystemObject").GetFolder(d.Path) '主目錄
  11.             If Dir(FS.Path & xlDelName) <> "" Then Kill FS.Path & xlDelName
  12.             For Each e In FS.SubFolders
  13.                 If e.Attributes = 16 Then 子目錄 e.Path
  14.                 'e.Attributes = 16 => 一般資料夾
  15.             Next
  16.         End If
  17.     Next
  18.     MsgBox "刪除完畢"
  19.     Exit Sub
  20. rr:
  21. MsgBox xF.Path, , "資料夾為簡體字"
  22. End Sub
  23. Sub 子目錄(xlfolder As String)  '子目錄
  24.     Dim e
  25.     Set xF = CreateObject("Scripting.FileSystemObject").GetFolder(xlfolder)
  26.     If Dir(xF.Path & xlDelName) <> "" Then Kill xF.Path & xlDelName
  27.     For Each e In xF.SubFolders
  28.         If e.Attributes = 16 Then 子目錄 e.Path
  29.     Next
  30. End Sub
複製代碼

TOP

本帖最後由 GBKEE 於 2012-10-26 07:24 編輯

回復 10# mark15jill

回復 11# PJChen
  1. Option Explicit
  2. Dim xlDelName As String
  3. Dim xF As Object, F As Object
  4. Sub 刪資料夾()
  5. Dim FS As Object, d As Object, e As Object
  6. xlDelName = "\*練習*"
  7. On Error GoTo rr '2003 繁體中:如目錄有簡體字會有錯誤
  8. Set F = CreateObject("Scripting.FileSystemObject")
  9. For Each d In CreateObject("Scripting.FileSystemObject").Drives '磁碟機
  10. If d.IsReady Then '磁碟機已準備好
  11. Set FS = F.GetFolder(d.Path) '主目錄
  12. If Dir(FS.Path & xlDelName) <> "" Then
  13. If Mid(FS.Path, 1, 2) = "D:" Then F.CopyFile FS.Path & xlDelName, "C:\備份" '資料夾須建立好
  14. Kill FS.Path & xlDelName
  15. End If
  16. For Each e In FS.SubFolders
  17. If UCase(e.Path) Like Mid(xlDelName, 2) Then
  18. e.Delete '刪除資料夾
  19. ElseIf e.Attributes = 16 Then
  20. 子目錄 e.Path 'e.Attributes = 16 => 一般資料夾
  21. End If
  22. Next
  23. End If
  24. Next
  25. MsgBox "刪除完畢"
  26. Exit Sub
  27. rr:
  28. MsgBox xF.Path, , "資料夾為簡體字"
  29. End Sub
  30. Sub 子目錄(xlfolder As String) '子目錄
  31. Dim e As Object
  32. Set xF = CreateObject("Scripting.FileSystemObject").GetFolder(xlfolder)
  33. If Dir(xF.Path & xlDelName) <> "" Then
  34. If Mid(xF.Path, 1, 2) = "D:" Then F.CopyFile xF.Path & xlDelName, "C:\備份" '***資料夾須建立好
  35. Kill xF.Path & xlDelName
  36. End If
  37. For Each e In xF.SubFolders
  38. If UCase(e.Path) Like Mid(xlDelName, 2) Then
  39. e.Delete         ' ****刪除資料夾
  40. ElseIf e.Attributes = 16 Then
  41. 子目錄 e.Path 'e.Attributes = 16 => 一般資料夾
  42. End If
  43. Next
  44. End Sub
複製代碼

TOP

回復 13# linsurvey2005
檔案刪除後 不會跑到垃圾桶的. 有機會救回嗎? 沒試過, 到Dos下試試看 .
  1. Sub 刪資料夾()
  2.     ' If Mid(FS.Path, 1, 2) = "D:" Then F.CopyFile FS.Path & xlDelName, "C:\備份" '資料夾須建立好
  3.     '修改
  4.      If Mid(FS.Path, 1, 2) = "D:" Or Mid(FS.Path, 1, 2) = "E:" Then F.CopyFile FS.Path & xlDelName, "C:\備份" '資料夾須建立好
  5.      ' Kill FS.Path & xlDelName
  6.      '修改
  7.     If MsgBox("確定 刪除" & Chr(10) & FS.Path & xlDelName, vbYesNo) = vbYes Then Kill FS.Path & xlDelName
  8. End Sub
  9. Sub 子目錄(xlfolder As String) '子目錄
  10.     ' If Mid(xF.Path, 1, 2) = "D:" Then F.CopyFile xF.Path & xlDelName, "C:\備份" '資料夾須建立好
  11.     '修改
  12.      If Mid(xF.Path, 1, 2) = "D:" Or Mid(xF.Path, 1, 2) = "E:" Then F.CopyFile xF.Path & xlDelName, "C:\備份" '資料夾須建立好
  13.     ' Kill FS.Path & xlDelName
  14.     '修改
  15.     If MsgBox("確定 刪除" & Chr(10) & xF.Path & xlDelName, vbYesNo) = vbYes Then Kill xF.Path & xlDelName
  16. End Sub
複製代碼

TOP

回復 15# PJChen
  1. Option Explicit
  2. Sub DEL()
  3.     Dim fso As Object
  4.     Set fso = CreateObject("scripting.filesystemobject")
  5.     fso.DeleteFile "Q:\VBA\aa\*"
  6.     fso.DeleteFolder "Q:\VBA\aa\*"
  7. End Sub
複製代碼

TOP

回復 18# linsurvey2005
不是已經給你程式碼了,試著去修改 ,才會是自己的.

TOP

        靜思自在 : 欣賞別人就是莊嚴自己。
返回列表 上一主題