2013-02-18 56 views
0

來源:網絡位置UNC路徑 目的地:遠程服務器並希望在此服務器上運行腳本。VB腳本將文件名從一個位置回顯到另一個位置,包括子文件夾

我一直在尋找很多腳本,但無法匹配我的確切要求。下面是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 

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

回答

0

您可以使用DateDiff函數與「d」作爲時間間隔(對於日)insted「h」(對於小時)。而問題的其餘部分是如何使這個遞歸函數,對吧?

MoveByDate 「C:\源」, 「\服務器\目的地\」,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 
+0

是的,我想這是個遞歸函數。 我試着檢查在本地機器上運行腳本。 這似乎並沒有在我的最後。我正嘗試從命令提示符執行並且不能正常工作。 沒有錯誤信息。它什麼也沒做。 – user1926332 2013-02-18 10:21:28

+0

你是否檢查你傳遞給MoveByDate的參數?第3天是不同的天數,在這裏的示例代碼是7(1周)。如果沒有那麼舊的文件,那麼顯然沒有什麼可移動的。 – 2013-02-18 13:13:59

相關問題