0

来源:网络位置 UNC 路径 目标:远程服务器并希望在此服务器上运行脚本。

我一直在寻找许多脚本,但无法满足我的确切要求。下面是看起来更接近我的要求的 VBscript,但这不适用于子文件夹,它看起来是特定于几个小时的,我正在寻找特定于多种文件类型的天数。任何帮助深表感谢 ?

提前感谢您帮助我!

我可以接受任何其他脚本,但必须满足我的要求。

==================================================== ====

Option Explicit 

On Error Resume Next

Dim fso, FileSet, Path, File, DDiff, Date1, Date2, DestPath

Path = "C:\source"
DestPath = "\\server\destination\" 
'DestPath must end with \
FileSet = GetDirContents(Path) 

For each File in FileSet 
Set File = fso.GetFile(Path & "\" & File)
Date1 = File.DateLastModified 
'.DateCreated if you want 24hrs of life, this example is 24hrs since last written
Date2 = Now()

DDiff = Abs(DateDiff("h", Date1, Date2))

If DDiff >= 168 Then
If Not fso.FileExists(DestPath & File.Name) Then
File.Move DestPath
'wscript.echo File.Name
Else
wscript.echo "Unable to move file [" & File.Name & "]. A file by this name already exists in the target directory."
End If
End If
Next 

Function GetDirContents(FolderPath) 
Dim FileCollection, aTmp(), i 
Set fso = CreateObject("Scripting.FileSystemObject") 
Set FileCollection = fso.GetFolder(FolderPath).Files 

Redim aTmp(FileCollection.count - 1) 
i = -1 

For Each File in FileCollection 
i = i + 1 
aTmp(i) = File.Name 
Next 

GetDirContents = aTmp 
End Function

==================================================== =======================

4

1 回答 1

0

您可以使用DateDiff带有“d”的函数作为间隔(代表天),替换为“h”(代表小时)。剩下的问题是如何制作这个递归函数,对吧?

MoveByDate "C:\source", "\server\destination\", 7

Sub MoveByDate(SrcPath, DestPath, DaysDiff)
    Dim oFSO, oFile, oFolder, oSubFolder, Date1, Date2
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder(SrcPath)
    Date1 = Now

    For Each oFile In oFolder.Files
        Date2 = oFile.DateLastModified
        If Abs(DateDiff("d", Date1, Date2)) >= DaysDiff Then
            If Not oFSO.FileExists(DestPath & oFile.Name) Then
                oFile.Move DestPath
            End If
        End If
    Next

    For Each oSubFolder In oFolder.SubFolders
        MoveByDate oSubFolder, DestPath, DaysDiff
    Next
End Sub
于 2013-02-18T09:06:08.487 回答