這個腳本實作的功能是這樣的:
舉例說明:
檔案夾a下有三個檔案b.f,c.d,e.txt;以及子檔案夾下的若干檔案
檔案夾b下有很多檔案;
要求洗掉檔案夾b下所有與檔案夾a下的同名檔案包括子檔案夾里的同名檔案(即洗掉檔案夾b下名字為b.f,c.d,e.txt的檔案,可能存在也可能不存在);
需要考慮嵌套子檔案夾的問題
說白了就是我當初拷貝了一些檔案過去,現在后悔了想洗掉!因為這樣的檔案比較多,我一點點對,有點暈,時間也利用補上!
uj5u.com熱心網友回復:
我找了一個這個,不過不能獲取子檔案夾里的檔案串列Dim ofso, File
Dim PathA, PathB
Dim FilesInPathA
Set ofso = CreateObject("Scripting.FileSystemObject")
'獲得路徑A下的檔案串列
Set PathA = ofso.GetFolder("C:\Users\Administrator\Desktop\材質包\meshes")
For Each File In PathA.Files
FilesInPathA = FilesInPathA & "|" & File.Name
Next
'獲得路徑B下的檔案串列
Set PathB = ofso.GetFolder("C:\Users\Administrator\Desktop\meshes")
For Each File In PathB.Files
If InStr(FilesInPathA, File.Name) Then '判斷此檔案在路徑A下是否存在
File.Delete True '如果存在則洗掉
End If
Next
Set fso = Nothing
uj5u.com熱心網友回復:
寫好了,代碼如下:
Set fso = createobject("scripting.filesystemobject")
curdir = fso.getparentfoldername(wscript.scriptfullname)
dirA = fso.buildpath(curdir,"a")
dirB = fso.buildpath(curdir,"b")
Set dict = createobject("scripting.dictionary")
addfilenames fso.getfolder(dirA)
removefiles fso.getfolder(dirB)
msgbox "Done!"
Sub AddFileNames(ByVal objFolder)
For Each objfile In objfolder.files
dict.item(objfile.name) = null
Next
For Each objsubfolder In objfolder.subfolders
addfilenames objsubfolder
Next
End Sub
Sub RemoveFiles(ByVal objFolder)
For Each objfile In objfolder.files
If dict.exists(objfile.name) Then
fso.deletefile objfile.path,True
End If
Next
For Each objsubfolder In objfolder.subfolders
removefiles objsubfolder
Next
End Sub
下載地址:
鏈接:https://pan.baidu.com/s/1nYH7LPdlcPSvaIT8BoP4zw
提取碼:43tr
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/35157.html
標籤:VBA
上一篇:急求大神幫我看一下vb6.0讀取txt檔案該怎么改提示型別不匹配和下標越界 救救孩子
下一篇:VBA
