发新话题
打印

删除某个目录下预定义天数文件的脚本

删除某个目录下预定义天数文件的脚本

信息来源:77169
复制内容到剪贴板
代码:
<?xml version="1.0" encoding="gb2312" ?>
<?job error="true" debug="false"?>

<package id="RemoveFilesByDate">

<job id="Main">
  <runtime>
    <description>
      FileName: RemoveFilesByDate.wsf
      这个脚本删除预定义天数以前的文件.

      Version: 1.0
      Created: [email]icuc88@hotmail.com[/email]
      Last Modify: Feb 18th, 2004
      All right reserved.

    </description>
    <named
      name="Driver"
      helpstring="文件所在逻辑驱动器盘符"
      type="string"
      required="true"
    />
    <named
      name="Path"
      helpstring="文件所在路径"
      type="string"
      required="true"
    />
    <named
      name="OlderBy"
      helpstring="删除多少天以前的文件,缺省为3天。"
      type="string"
      required="false"
    />
    <example>
    Example:
      1. 删除3天以前的文件
      RemoveFilesByDate.wsf /Driver:C: /Path:\\test\\

      2. 删除5天以前的文件
      RemoveFilesByDate.wsf /Driver:C: /Path:\\test\\ /OlderBy:5
    </example>
  </runtime>

  <script language="VBScript">
  <![CDATA[
  
    Option Explicit
    On Error Resume Next

    If WScript.Arguments.Count < 2 Then
      WScript.Arguments.ShowUsage
      WScript.Quit
    Else
      ' 从指定的磁盘目录上面查找符合条件的文件
      Dim strComputer
      Dim objWMIService
      Dim colFiles
      Dim objFSO
      strComputer = "." '在本地计算机上执行脚本
      Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" _
      & strComputer & "\root\cimv2")
      Dim WMISql
      WMISql = "Select * from CIM_DataFile where Path = '" _
      & WScript.Arguments.Named.Item("Path") & "' AND Drive= '" _
      & WScript.Arguments.Named.Item("Driver") & "'"

      Set colFiles = objWMIService.ExecQuery(WMISql)
      
      ' 删除满足条件的文件
      ' 该脚本指搜索当前路径下面的文件,不搜索子目录
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      
      Dim strWMIDate
      Dim ODate
      Dim OlderBy
      Dim objFile
      For Each objFile in colFiles
      strWMIDate = objFile.CreationDate
      ODate = WMIDateStringToDate(strWMIDate)
      If WScript.Arguments.Named.Item("OlderBy") <> "" Then
        OlderBy = CInt(WScript.Arguments.Named.Item("OlderBy"))
      Else
        OlderBy = 3
      End If
      If (Date - OlderBy) >= ODate Then
        objFSO.DeleteFile(objFile.Name)     
      End If
      Next
    End If

    Function WMIDateStringToDate(dtmInstallDate) '转换WMI格式的日期类型到DateTime类型
    WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & _
      Mid(dtmInstallDate, 7, 2) & "/" & Left(dtmInstallDate, 4) _
      & " " & Mid (dtmInstallDate, 9, 2) & ":" & _
      Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, _
      13, 2))
    End Function
  ]]>
  </script>
</job>
</package>

TOP

发新话题