Board logo

标题: 对比删除多余的文件、文件夹 [打印本页]

作者: cqruirui     时间: 2008-5-20 14:01    标题: 对比删除多余的文件、文件夹

dim sdir,ddir
'远程目录
sdir="d:\netgames\魔域(测试用)"
'本地目录
ddir="E:\123"
function comparefile(sdir,ddir)
   dim Fso,dFol,dfs,sf1,f1
   set Fso=CreateObject("Scripting.FileSystemObject")
   if not(Fso.folderexists(sdir)) then
      msgbox chr(34) &sdir &chr(34) &"文件夹不存在,请确认!",64
      exit function
   end if
   if not(Fso.folderexists(ddir)) then
      msgbox chr(34) &ddir &"""文件夹不存在,请确认!",64
      exit function
   end if
   if right(sdir,1)<>"\" then sdir=sdir &"\"

   set dFol=fso.getfolder(ddir)
   set dfs=dfol.files

   for each f1 in dfs
       if fso.fileexists(sdir & f1.name)  then
           set sf1=fso.GetFile(sdir & f1.name)
           if f1.DateLastModified <>sf1.DateLastModified or f1.size<>sf1.size then
               f1.delete
           end if
        else
           f1.Delete(true)
        end if
    next
   dim fols
   set fols=dfol.subfolders
   for each f1 in fols
       if not fso.folderexists(sdir &f1.name) then
            f1.delete true
       else     
          comparefile sdir & f1.name,f1.path
       end if
   next
end function
comparefile sdir,ddir

这个脚本能删除多余的文件及文件夹。但是不能删除像12..    ,1_2..之类的特殊文件夹还有就是没有权限的文件夹也不能删除,用dos的"RD /s /q'命令就能全部删除,希望各位能够融合下,
作者: huahua0919     时间: 2008-5-20 14:20

Dim oShell
Set oShell = WScript.CreateObject ("WSCript.shell")
oShell.run "cmd /c rd /s/q path"
Set oShell = Nothing
判断后用or再加上面的语句也许可以达到你想要的效果
作者: cqruirui     时间: 2008-5-20 14:39
感觉那些特殊的文件名根本没有检测出来,是不是方法错误哟
作者: huahua0919     时间: 2008-5-20 14:53
是没列出来,与cmd不一样,刚才错了!
抱歉

[ Last edited by huahua0919 on 2008-5-20 at 03:14 PM ]