【问题标题】:Vbscript Move Files and Copy Structure of folders 90 days old or more than NowVbscript 移动文件和复制文件夹结构 90 天或超过现在
【发布时间】:2012-03-11 08:35:06
【问题描述】:

我希望遍历共享上的文件夹,如果文件夹或子文件夹包含超过 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

【问题讨论】:

    标签: windows scripting vbscript administration


    【解决方案1】:

    不久前我写了一个处理文件同步的类,欢迎你适应你的需要。

    这里是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
    

    【讨论】:

    • 感谢您的回复,没想到有人会回复。我收到以下 (9, 8) Microsoft VBScript 编译错误:Invalid 'exit' statement hetland.ws/blog/synchronizationagent-class-reference
    • 在示例中它假设这是在一个函数中。引发错误的是“退出函数”行,如果您在全局上下文中运行它并且想死,您可以使用 WScript.Exit() 代替。我已经编辑了代码示例以更改它检查是否应该继续的方式
    猜你喜欢
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多