2012-02-21 36 views
1

即時通訊正在遍歷共享上的文件夾,如果文件夾或子文件夾包含超過90天的文件,則複製整個結構。 (如果文件夾包含所述老化文件,則移動比所述日期更早的文件 - 複製結構)Vbscript移動文件和複製文件夾90天或以上的文件夾結構

我得到了一個我在網上找到的腳本,我改變了它以正確使用DateAdd函數,它似乎移動文件但它不復制結構。

例子。包含文件(這個心不是確切的結構,但mearly爲例)

2分享位置

Source 
1. \\Share1\folder 
2.   \Folder\Files 
3.    \Folder\Files 
4.   \Folder 

1. \\Share2\folder 
2.   \Folder\Files 
3.    \Folder\Files 
4.   \Folder 
Destination 
1. \\Share2\folder 
2.   \Folder\Files 
3.    \Folder\Files 
4.   \Folder 

1. \\Share2\folder 
2.   \Folder\Files 
3.    \Folder\Files 
4.   \Folder 

Dim objFSO, ofolder, objStream 

Set objShell = CreateObject("WScript.Shell") 
Set objFSO = CreateObject("scripting.filesystemobject") 
Set objNet = CreateObject("WScript.NetWork") 
Set FSO = CreateObject("Scripting.FileSystemObject") 
set outfile = fso.createtextfile("Move-Result.txt",true) 
SPath = "Y:\test" 
Sdest = "Y:\Archive\" 

ShowSubfolders FSO.GetFolder(spath) 

Sub ShowSubFolders(Folder) 
For Each Subfolder in Folder.SubFolders 
CheckFolder(subfolder) 
ShowSubFolders Subfolder 
Next 
End Sub 

'CheckFolder(objFSO.getFolder(SPath)) 

Sub CheckFolder(objCurrentFolder) 
Dim strTempL, strTempR, strSearchL, strSearchR, objNewFolder, objFile 
Const OverwriteExisting = TRUE 
currDate = Date 
dtmDate = DateAdd("d",-90,Now) 
strTargetDate = ConvDate(dtmDate) 
For Each objFile In objCurrentFolder.Files 
FileName = objFile 
'WScript.Echo FileName 
'strDate = ConvDate(objFile.DateCreated) 
strDate = ConvDate(objFile.DateLastModified) 
If strDate < strTargetDate Then 
objFSO.MoveFile FileName, Sdest 
outfile.writeline Filename 
End If 
Next 
End Sub 

Function ConvDate (sDate) 'Converts MM/DD/YYYY HH:MM:SS to string YYYYMMDD 
strModifyDay = day(sDate) 
If len(strModifyDay) < 2 Then 
strModifyDay = "0" & strModifyDay 
End If 
strModifyMonth = Month(sDate) 
If len(strModifyMonth) < 2 Then 
strModifyMonth = "0" & strModifyMonth 
End If 
strModifyYear = Year(sDate) 
ConvDate = strModifyYear & strModifyMonth & strModifyDay 
End Function 

回答

0

我寫了一個類來處理文件同步了一段時間後,你是受歡迎的適應滿足您的需求。

這裏是鏈接到文檔中的Primary ClassIndividual File Object Class

在頁面的底部有一個按鈕來查看/保存標記的源代碼「積極發展科」

包括在你的項目中你可以像這樣完成你的目標。

Dim oSyncAgent, sUpdateSource, sUpdateTarget, dMinAge, aStagedAFiles, iIterator 
Set oSyncAgent = New SynchronizationAgent 

sUpdateSource = "Y:\test" 
sUpdateTarget = "Y:\Archive" 
dMinAge = DateAdd("d",-90,Now) 

'This will only proceed if the SetLocations() function is successful 
If oSyncAgent.SetLocations(sUpdateSource,sUpdateTarget) Then 

    'Get a working copy of the files staged for sync from location A to B 
    aStagedAFiles = oSyncAgent.LocationAStaged 

    'Loop over files staged for synchronization and cancel any that were created in the last 90 days 
    For iIterator = 0 To UBound(aStagedAFiles) 
    If aStagedAFiles(iIterator).DateCreated >= dMinAge Then 
     aStagedAFiles(iIterator).Process = False 
    End If 
    Next 'SyncFile 

    'Set the modified list of files to be synced back to the agent 
    oSyncAgent.LocationAStaged = aStagedAFiles 

    'Execute the sync ignoring the files we set above 
    oSyncAgent.AbSync(False) 'Don't mirror location A 

End If 

Set oSyncAgent = Nothing 
+0

感謝您的迴應,沒有人認爲有人會迴應。我得到以下(9,8)Microsoft VBScript編譯錯誤:無效的'退出'語句http://hetland.ws/blog/synchronizationagent-class-reference/ – k3eper 2012-02-21 20:54:36

+0

在這個例子中,它假定這是一個函數。它是拋出錯誤的「退出函數」行,如果你在全局上下文中運行它並想死,你可以使用WScript.Exit()來代替。我編輯了代碼示例以更改檢查方式是否應繼續執行 – JoshHetland 2012-02-21 21:51:33

+0

是否爲您工作? – JoshHetland 2012-02-22 19:09:58

相關問題