在指定日期范围之间复制文件 [英] copy files between a specified date range

查看:26
本文介绍了在指定日期范围之间复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 VBS 将文件从一个文件夹复制到另一个位于特定日期范围内的文件夹.

I want to copy files from one folder to another which falls between a specific date range using VBS.

例如我想将文件从 06/11/2009 复制到 06/12/2010.我怎样才能在 VB 脚本中做到这一点.

for example i want to copy files from 06/11/2009 to 06/12/2010. How can I do that in VB script.

推荐答案

WMI 是一种选择吗?如果是这样,这里的示例脚本基于 嘿,脚本专家! 文章 如何删除早于指定日期的所有文件?:

Is WMI an option? If so, here's a sample script based on the one from the Hey, Scripting Guy! article How Can I Delete All Files Older Than a Specified Date?:

strComputer = "." 

strFolder = "C:\FromFolder"
strNewFolder = "C:\ToFolder"

strDateFrom = "20090611000000.000000+00" ' 06/11/2009
strDateTo   = "20100612000000.000000+00" ' 06/12/2010


Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set colFiles = oWMI.ExecQuery _ 
    ("ASSOCIATORS OF {Win32_Directory.Name='" & strFolder & "'} WHERE " _ 
        & "ResultClass = CIM_DataFile")

For Each oFile in colFiles
    If oFile.CreationDate > strDateFrom And oFile.CreationDate < strDateTo Then
        'WScript.Echo "Full path:     " & oFile.Name
        'WScript.Echo "Creation date: " & oFile.CreationDate

        oFile.Copy strNewFolder & "\" & oFile.FileName & "." & oFile.Extension
        oFile.Delete
    End If
Next

这是一个稍微不同的变体,其中日期检查包含在 WMI 查询中:

Here's a slightly different variant where date checks are included in the WMI query:

strComputer = "."
strDateFrom = "20090611000000.000000+00" ' 06/11/2009
strDateTo   = "20100612000000.000000+00" ' 06/12/2010
strNewFolder = "C:\ToFolder"
iFlags = 48

Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set colFiles = oWMI.ExecQuery( _
    "SELECT * FROM CIM_DataFile" & _
    " WHERE Drive = 'C:' AND Path = '\\FromFolder\\'" & _
    " AND CreationDate >= '" & strDateFrom & "'" & _
    " AND CreationDate <= '" & strDateTo & "'" _
    ,,iFlags)

For Each oFile in colFiles
    'WScript.Echo "Full path:     " & oFile.Name
    'WScript.Echo "Creation date: " & oFile.CreationDate

    oFile.Copy strNewFolder & "\" & oFile.FileName & "." & oFile.Extension
    oFile.Delete
Next

一些注意事项:

  • 该脚本是非递归的,也就是说,它仅从源文件夹本身而不是其子文件夹中移动文件.
  • 日期以 UTC 格式指定.有关此格式的更多信息,请参见我链接到的文章.
  • WMI 不包括移动文件和文件夹的方法,因此脚本复制然后删除文件.

这篇关于在指定日期范围之间复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆