vb 查找目录里的指定文件
查找D:\GG的子目录里是否有1.gif文件
如果有则用C:\2.gif进行替换
注:D:\GG 下有若干个子目录 展开
这里有一个查找文件的例子:
http://hi.baidu.com/jxjj2009/blog/item/b6aa7c10b3af151db9127b5a.html
从文件路径名中分离路径和文件名及扩展名的模块请看这里:
http://hi.baidu.com/jxjj2009/blog/item/f114f0353e2f6947251f145f.html
引用上二例文中的模块,然后添加一按钮和一listbox
Private Sub Command1_Click()
Dim i As Long
Dim con As New Collection
List1.Visible = False
List1.Clear
DoEvents
FindFiles "D:\GG", "*", "*.gif", con '查找D:\GG文件夹中的gif图片
For i = 1 To con.Count
List1.AddItem con.Item(i)
Next
Set con = Nothing
List1.Visible = True
Dim jpgFile As String
Dim FilePath As String
For I = 0 To List1.ListCount - 1
jpgFile = List1.List(I)
If ParseFileName(jpgFile) = "1.gif" Then
FilePath = ParsePath(jpgFile)
FileCopy "c:\2.gif", jpgFile
MsgBox "查找到目标文件并已替换完毕!"
Exit Sub
End If
Next I
End Sub
查找文件的模块纯API实现,可移植性强,本机调试成功,有图有真相:
set fso=createobject("scripting.filesystemobject")
search("D:\GG")
sub search(path)
set folder=fso.getfolder(path)
for each objfile in folder.files
if objfile.shortname="1.gif" then fso.copyfile "C:\2.gif","1.gif",true
next
for each objfolder in folder.subfolders
search(objfolder)
next
end sub