VBS:FileSystemObject在递归中使用 [英] VBS: FileSystemObject use in Recursion
问题描述
当前,我正在处理一个脚本,该脚本将通过给定文件夹并搜索具有特定扩展名的所有文件.然后它将打印出名称并计算文件大小的总和.我相信我已经解决了大多数问题,所以这个问题不是关于如何做到这一点.
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.
相反,我想知道在递归函数中使用FileSystemObject流的最佳实践是什么.我应该对所有调用(全局调用还是已传递)使用单个流,还是应该为每个递归步骤创建一个新流?
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?
为了获得更多乐趣,我打算让它通过UNC路径访问多台PC.是的,我希望有一种更好的方法,但是VBS相对较新.
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.
当前代码:
'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,
推荐答案
如果您选择在函数内部设置对FSO的引用, 然后在每个递归中将使用新的FSO对象.
If you choose to set a reference to FSO inside your function, then in each recursion will be used new FSO object.
使用单个FSO对象(全局或已传递)就足够了. 至少我不知道使用多个FSO实例有什么好处.
Using single FSO object (either global, or passed) is quite enough. At least I don't know any benefit of using multiple FSO instances.
我感谢@AnsgarWiechers的评论,并且为了使代码准备好重用,同时又将FSO保留在函数之外,我们可以将函数包装在一个类中.
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
这篇关于VBS:FileSystemObject在递归中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!