我编写了此代码来访问文件夹中的 Excel 文件:
strPath="C:\Test\"
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder (strPath)
Set objExcel= CreateObject("Excel.Application")
objExcel.Visible= False
For Each objFile In objFolder.Files
If objFso.GetExtensionName(objFile.Path) = "xls" Then
现在我必须创建一些子文件夹并在其中放置一些 .xls 文件。
我应该在我的代码中做哪些修改来搜索主文件夹和所有其他子文件夹中的文件(子文件夹中也有一些文件夹)?
最佳答案
这其实是一个很好解决的问题。递归意味着您创建了一个自引用函数(一个调用自身的函数)。在您的情况下,您可以为当前文件夹的每个子文件夹调用函数本身。
TraverseFolders objFso.GetFolder(strPath)
Function TraverseFolders(fldr)
' do stuff with the files in fldr here, or ...
For Each sf In fldr.SubFolders
TraverseFolders sf '<- recurse here
Next
' ... do stuff with the files in fldr here.
End Function
关于vbscript - 递归访问文件夹内的子文件夹文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14950475/