Vbscript移动文件和复制文件夹的结构90天或以上

时间:2012-02-21 00:57:24

标签: windows scripting vbscript administration

我希望遍历共享中的文件夹,如果文件夹或子文件夹包含超过90天的文件,则复制整个结构。 (如果文件夹包含所述老化文件,则移动早于所述天数复制结构的文件)

我有一个我在网上找到的脚本,我已将其改为正确使用DateAdd功能,它似乎将文件移动但不会复制结构。

实施例

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

1 个答案:

答案 0 :(得分:0)

我曾经写过一个用于处理文件同步的课程,欢迎您根据自己的需要进行调整。

此处是指向Primary ClassIndividual File Object Class

的文档的链接

在页面底部有一个按钮,用于查看/保存标有“Active Development Branch”的源代码

在项目中包含该课程,您就可以完成目标。

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