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

编程入门 行业动态 更新时间:2024-10-28 00:27:20
本文介绍了递归访问文件夹内的子文件夹文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已编写此代码来访问文件夹中的Excel文件:

I have written this code to access Excel files inside a folder:

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文件。

Now I have to create some subfolders and put some .xls files in those.

我应该对我的代码进行哪些修改以搜索主文件夹和所有其他子文件夹(子文件夹中也有一些文件夹)中的文件?

What modification should I do in my code for searching files in main folder and all other subfolders (there are also some folders inside subfolders)?

推荐答案

这实际上是一个已解决的问题。递归意味着您创建了一个自引用函数(一个自我调用的函数)。在您的情况下,您可以使函数针对当前文件夹的每个子文件夹进行调用。

This is actually a well-solved problem. Recursion means that you create a self-referencing function (a function that calls itself). In your case you'd make the function call itself for each subfolder of the current folder.

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

更多推荐

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

本文发布于:2023-10-31 16:49:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546690.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   文件夹   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!