VBScript可以确定特定文件夹中最近修改(或添加)的文件吗?

时间:2010-03-15 21:29:02

标签: file vbscript copy

我有一个脚本(或者更确切地说会有一个脚本)来检查文件夹并将文件从这个文件夹复制到另一个位置(每天运行一次)。但是,我要复制的fileName会根据日期而更改。

基本上,不是将“strFilePath”设置为“C:\ somePath \ somePath2 \ myFile.txt”,我想简单地采用最近修改过的(或添加 - 这是否会对脚本产生影响? ?)在“somePath2”文件夹中并将其复制到目的地。

奖金(但不是完全必要的)是检查脚本是否在过去24小时内修改/添加了文件,并且只在这种情况下将其复制。

感谢您的帮助!

2 个答案:

答案 0 :(得分:5)

尝试一下:

option explicit

dim fileSystem, folder, file
dim path 

path = "C:\temp"

Set fileSystem = CreateObject("Scripting.FileSystemObject")
Set folder = fileSystem.GetFolder(path)

for each file in folder.Files    
    if file.DateLastModified > dateadd("h", -24, Now) then
        'whatever you want to do to process'
        WScript.Echo file.Name & " last modified at " & file.DateLastModified
    end if
next

答案 1 :(得分:3)

怎么样:

Dim f, fl, fs 
Dim filedate, filename

Set fs = CreateObject("Scripting.FileSystemObject")
Set fl = fs.GetFolder("C:\Docs")

For Each f In fl.Files
    If IsNull(filedate) Or f.DateCreated > filedate Then
        filedate = f.DateCreated
        filename = f.Name
    End If
Next

''Most recent file and date are contained in filename, filedate