|
本帖最后由 9zhmke 于 2017-8-10 20:55 编辑
根据A文件夹里的文件来处理B文件夹里的文件(含子目录),想到无忧这里也许还有初学者在精简系统、绿化软件,如果没有称手的工具也许可以用这个试试,使用方法直接修改源代码为需要处理的文件夹,除了前面两行,第3行可以直接写成:comp("文件夹1","文件夹2",1) 。
comp(path1,path2,switch) '比较A和B两个文件夹,0为根据A文件夹中的已有文件删除B文件夹中的文件;1为复制A中比较小的到B;2为复制A中比较大的到B;
比如我下载的网站图片目录是d:\www\data,用FSResizer压缩后的目录是d:\temp,那我就把第3行改成:comp("d:\temp","d:\www\data",1) ,对只在一个文件夹中有的文件不作处理,于是变成这样:
Set WshShell=WScript.CreateObject("WScript.Shell") '第一行不变
Set objFSO = CreateObject("Scripting.FileSystemObject") '第二行不变
comp("d:\temp","d:\www\data",1)
Function......后面不变了
事因自己网站硬盘小空间满了,照片有100G左右, 不想换硬盘只好把照片下载来压缩,使用的是FSResizer3.8,明明有个功能是如果比源文件大就不处理,但貌似选项无效。大多照片是比之前小了,但仍有不少比以前大,无奈写了段VBS来实现,高手请直接忽视。
完整代码:请使用下面的复制功能而不要直接用鼠标复制,鼠标复制到文件里粘贴时会有编号,造成程序无法运行,那你也可以直接从后面下载。
- Set WshShell=WScript.CreateObject("WScript.Shell")
- Set objFSO = CreateObject("Scripting.FileSystemObject")
- comp("C:\新建文件夹\month1004_process","C:\新建文件夹\month1004_sr",1)
- 'comp("文件夹1","文件夹2",1)
- 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
- 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
复制代码
代码和附件内容相同 |
|