VBS: FileSystemObject use in Recursion

六月ゝ 毕业季﹏ 提交于 2019-12-08 07:03:03

问题


Currently I'm working on a script that will go through a given folder and search for all files with a specific extension. It will then print out the name and sum the file size. I believe i have most of the issues sorted out, so this question isn't about how to do this.

Instead, I want to know what would be the best practice for using FileSystemObject streams in a recursive function. Should I be using a single stream for all calls (either global, or passed), or should I be creating a new stream for each recursive step?

For extra fun, I'm planning on having this access multiple PCs, and over UNC path. And yes, I expect there's a better way of doing this, but I'm relatively new with VBS.

Current code:

'Recursive Function handles search of files in folder and subfolders.
Function UNCSearchFolder(strUNCRootPath, strUNCNextFolder)  
 Dim objFSOUNCSearchFolder, objFSOUNCSearchFolder2, colFolderList, objFolder, strCurrentFolder, strSubFolder

 'Get list of Subfolders in folder: <Rootpath>\<Nextfolder>
 strCurrentFolder = strUNCRootPath & "\" & strUNCNextFolder & "\"
 Set objFSOUNCSearchFolder = CreateObject("Scripting.FileSystemObject")
 Set objFSOUNCSearchFolder2 = objFSOUNCSearchFolder.GetFolder(strCurrentFolder)
 Set colFolderList = objFSOUNCSearchFolder2.SubFolders

 'Subfolder dive
 For Each objFolder in colFolderList
  strSubFolder = objFolder.name
  'REMOVE THIS ECHO LATER
  wscript.echo strSubFolder
  UNCSearchFolder(strCurrentFolder, strSubFolder)
 Next

 'Search for files here


 'GC on File Streams
 Set objFSOUNCSearchFolder2 = Nothing
 Set objFSOUNCSearchFolder = Nothing
End Function

So, should one filestream be used for all accesses or should each step use one separately? Is it a moot point? Will this cause multiple connections to each system or should it only use one? Basically I want the script to work without disrupting users, or causing weird responses (ie, running out of active connections). The script will only be used a couple times for an audit we're doing, but may eventually be repurposed for future audits.

Let me know what you think. Thanks for any help,


回答1:


If you choose to set a reference to FSO inside your function, then in each recursion will be used new FSO object.

Using single FSO object (either global, or passed) is quite enough. At least I don't know any benefit of using multiple FSO instances.

[EDIT] I appreciate @AnsgarWiechers comment, and to make the code ready for re-using, while kept the FSO out of the function, we can wrap our function in a class.

With New FileInfo
    WScript.Echo .FileSize("C:\temp", "txt", True)
End With


Class FileInfo

    Private m_oFSO

    Public Function FileSize(sRootDir, sExtension, bRecursive)
        Dim oFolder, oFile, sFExt
        sFExt = LCase(sExtension)
        Set oFolder = m_oFSO.GetFolder(sRootDir)
        For Each oFile In oFolder.Files
            If LCase(m_oFSO.GetExtensionName(oFile.Name)) = sFExt Then
                FileSize = FileSize + oFile.Size
            End If
        Next
        If bRecursive Then
            Dim oSubFolder
            For Each oSubFolder In oFolder.SubFolders
                FileSize = FileSize + FileSize(oSubFolder, sExtension, True)
            Next
        End If
    End Function

    Private Sub Class_Initialize
        Set m_oFSO = CreateObject("Scripting.FileSystemObject")
    End Sub

    Private Sub Class_Terminate
        Set m_oFSO = Nothing
    End Sub

End Class


来源:https://stackoverflow.com/questions/14903372/vbs-filesystemobject-use-in-recursion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!