vbscript - 递归访问文件夹内的子文件夹文件

标签 vbscript directory

我编写了此代码来访问文件夹中的 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/

相关文章:

sql-server - VBS第三方程序(如SQL)中的错误处理

python - 检索文件路径的尾端

java - 从 android 函数名称或变量而不是硬编码获取 Assets 目录

vbscript - 创建新的 Word 文档而不调用 Word

vbscript - 如何使用 WScript.Shell SendKeys 发送数字键盘击键?

powershell - 在文本文件中查找数字并使用命令行更改其行数据的符号

c# - 如何从完整的目录文件名中获取文件名?

python - 在 VBS 中使用 WshShell.Run 运行 Python 脚本不会生成输出文件

ios - swift 中 iOS 应用程序中文件的绝对路径

android-studio - Android Studio 项目文件夹未显示