VB脚本将文件名从一个位置回显到另一个位置,包括子文件夹

时间:2013-02-18 04:50:23

标签: vbscript powershell-v2.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

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

1 个答案:

答案 0 :(得分:0)

你可以使用带有“d”的DateDiff功能作为“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