【问题标题】:How to delete all files in a directory tree older than 10 days using a VBS script?如何使用 VBS 脚本删除目录树中超过 10 天的所有文件?
【发布时间】:2014-08-01 13:21:52
【问题描述】:

我有一个服务器,在C:\data 中有很多文件夹。我谈论大约 5000 个子文件夹,其中每个文件夹都有一个随机名称,例如 sgshVSHsXx.wjwuhHHS

每个文件夹都包含一个名为 DB 的子文件夹,每个 DB 文件夹都包含一些数据库文件,还包含随机文件名和随机文件扩展名。

我需要浏览所有DB 文件夹并删除所有超过 10 天的文件。

我想我可以为此使用一些 VBS,但没有太多经验。有人可以解释一下这个问题吗?

谢谢

【问题讨论】:

  • 您具体在哪个部分遇到了问题?

标签: vbscript


【解决方案1】:

当然批处理或命令提示符适合这个

forfiles /p "c:\data" /m * /s /d -10 /c "cmd /c del @path"

并且是一行。

【讨论】:

    【解决方案2】:

    将以下内容另存为 .vbs 文件

    set args = wscript.arguments
    if args.count <> 2 then
        wscript.echo "Syntax: " & wscript.scriptname & " <path> <days>" 
        wscript.quit
    end if
    
    path = args(0)
    killdate = date() - args(1)
    
    arFiles = Array() 
    set fso = createobject("scripting.filesystemobject") 
    
    SelectFiles path, killdate, arFiles, true 
    
    nDeleted = 0 
    for n = 0 to ubound(arFiles) 
    
      on error resume next 'in case of 'in use' files... 
      arFiles(n).delete true 
      if err.number = 0 then 
         nDeleted = nDeleted + 1 
      end if 
      on error goto 0 
    next 
    
    sub SelectFiles(sPath,vKillDate,arFilesToKill,bIncludeSubFolders) 
      on error resume next 
      set folder = fso.getfolder(sPath) 
      set files = folder.files 
    
      for each file in files 
        dtlastmodified = null 
        on error resume Next 
        dtlastmodified = file.datelastmodified 
        on error goto 0 
        if not isnull(dtlastmodified) Then 
          if dtlastmodified < vKillDate then 
            count = ubound(arFilesToKill) + 1 
            redim preserve arFilesToKill(count) 
            set arFilesToKill(count) = file 
          end if 
        end if 
      next 
    
      if bIncludeSubFolders then 
        for each fldr in folder.subfolders 
          SelectFiles fldr.path,vKillDate,arFilesToKill,true 
        next 
      end if 
    end sub
    

    运行: .vbs ""

    示例: c:\delete.vbs "c:\test 文件夹\" 10

    确保从管理员命令提示符运行

    # 来源:http://community.spiceworks.com/scripts/show/282-delete-old-files-with-recursion

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-31
      • 2012-11-09
      • 2022-07-06
      • 2012-12-01
      • 1970-01-01
      • 2020-09-25
      相关资源
      最近更新 更多