我以前写了功能模块。给你自己按需要增减功能吧,我没那么多时间了:- Function comp(path1,path2,switch) '比较A和B两个文件夹,0为根据A文件夹中的已有文件删除B文件夹中的文件;1为复制A中比较小的到B;2为复制A中比较大的到B;
- path1=trim(path1):path2=trim(path2):comp="":tmp2="":tmp3="" '去掉前后空格、初使化临时变量
- if instr(path1,"%") then path1=WshShell.ExpandEnvironmentStrings(path1)'还原带环境变量的
- if instr(path2,"%") then path2=WshShell.ExpandEnvironmentStrings(path2)
- if path1="" then comp=comp & path1 & "文件夹不能为空. "
- if path2="" then comp=comp & path2 & "文件夹不能为空. "
- if not objFSO.FolderExists(path1) then comp=comp & "找不到" & path1 & "文件夹. "
- if not objFSO.FolderExists(path2) then comp=comp & "找不到" & path2 & "文件夹. "
- if right(path1,1)=":" then path1=path1 & "\"
- if right(path2,1)=":" then path2=path2 & "\"
- if instr(path1,"?") or instr(path1,"*") then comp="请不要使用通配符:" & path1 & ". "
- if instr(path2,"?") or instr(path2,"*") then comp="请不要使用通配符:" & path2 & ". "
- if path1=path2 then comp="comp不能处理同一文件夹:" & path1 & "和:" & path2
- if comp<>"" then Exit Function '以上为保证实施条件,以下为正式程序
- Set oFolder = objFSO.GetFolder(path1)
- Set oSubFolders = oFolder.SubFolders
- Set oFiles = oFolder.Files
- For Each oFile In oFiles
- tmp2=path2 & replace(oFile.Path,path1,"")
- if objFSO.FileExists(tmp2) then
- if switch=0 then objFSO.deletefile tmp2,true
- set objFile2 = objFSO.GetFile(tmp2) 'tmp1.WriteLine "删除:" & tmp2
- if oFile.Size > objFile2.Size and switch=2 then tmp3="replace"
- if oFile.Size < objFile2.Size and switch=1 then tmp3="replace"
- if tmp3="replace" then
- 'tmp1.WriteLine tmp2 & "替换成:" & oFile
- count=count+1
- set objFile2 = nothing
- objFSO.CopyFile oFile, tmp2,True '替换B目录文件
- end if
- end if
- Next
- For Each oSubFolder In oSubFolders
- tmp2=path2 & replace(oSubFolder.Path,path1,"") '处理下面的子目录
- comp oSubFolder.Path,tmp2,switch '递归每个文件夹,并把子目录和参数传递下去
- Next
- Set oFolder = Nothing
- Set oSubFolders = Nothing
- comp="已顺利完成."
- End Function
复制代码
|