使用正则表达式或简单的 FileExists 来查找文件会更好吗?

发布于 2024-10-04 02:41:26 字数 803 浏览 3 评论 0原文

我需要检查目录中是否存在文件。我想到的两个选项是使用正则表达式(vbscript.regexp)来确定文件是否存在。另一种选择是使用 FileSystemObject 的 FileExists 方法。

使用正则表达式的缺点意味着我需要循环遍历目录中的每个文件并针对每个文件名测试正则表达式。其次,正则表达式方法将来可能更难维护(正则表达式)。但是,我需要测试两个正则表达式,因为同一文件可能有不同版本。对于其中一个文件,文件名的描述部分经常更改(好吧,这是一个谎言 - 它会在某个随机周期中更改),因此使用正则表达式将有助于匹配文件,而不管这部分如何 = 更健壮的代码。

使用 FileExists,我可以同时测试两个文件,并根据每次检查返回的布尔值,处理可以继续。使用 FileExists 的相关缺点是不支持通配符匹配,因此在考虑带有描述的文件名时,始终需要更新它以反映新的描述 = 更多维护麻烦。然而,它更容易掌握,也更容易改变。

文件名格式:

Data_Sheet_<yyyymmdd>_D.xlsx // the normal file
Data_Sheet_<yyyymmdd>_D_<some description>.xlsx // the alternate file

文件名的 部分将由文件所在的当前文件夹确定。

问题:使用正则表达式或简单的 FileExists 来查找会更好吗文件?

我将在 Excel vba 宏中使用上述任一方法。

I need to check if a file exists within a directory. The two options I thought of are using a regex (vbscript.regexp) to determine if the file is present. The other option being to use the FileSystemObject's FileExists method.

The cons of using the regex implies that I will need to loop through every file in the directory and test the regex against each filename. Secondly, a regex approach may be harder to maintain (the regex) in the future. However, I need to test two regex's since there may be a different version of the same file. For one of the files, there is a description part of the filename which changes frequently(ok, that's a lie - it changes on some random cycle), so using a regex will assist in matching the files regardless of this part = more robust code.

Using FileExists, I can test for both files simultaneously and depending on the boolean value returned for each check, processing can continue. The related downside for using FileExists is that wildcard matching is not supported, so when considering the filename with a description, this will always need to be updated to reflect the new description = more maintenance headaches. However, it is simpler to grasp and easier to change.

Format of filenames:

Data_Sheet_<yyyymmdd>_D.xlsx // the normal file
Data_Sheet_<yyyymmdd>_D_<some description>.xlsx // the alternate file

The <yyyymmdd> part of the filename will be determined by the current folder the file is located in.

The question: Would it be better to use a regex or a simple FileExists to find files?

I will using the either of the above methods within an Excel vba macro.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄖ落Θ余辉 2024-10-11 02:41:26

你看过 Dir() 吗?您可以向它传递通配符,例如 my*file.pd? 也许这可能是一种妥协的方法。当然,您不会获得正则表达式的全部功能,但您可以限制您的文件,然后实际测试以查看存在的内容。

就我个人而言,我会使用 FileExists() 而不是循环目录并测试每个文件。如果目录变得很大,那么循环可能会永远持续下去。不过,如果您使用 Dir() 您可能会限制需要迭代的文件数量。另外 FileExists() 恰好是一行/语句并且更干净...不过您必须确保脚本运行时包含在您的项目中

http://techonthenet.com/excel/formulas/dir.php
http://www.blueclaw-db.com/listbox-directory.htm
http://www.hobbub.com/vba-vb-vsto/iteating-over-a-directory-with-a-dir-loop-loop-through-directory-vb/

第二个和第三个链接有更好的目录迭代示例。

Have you looked at Dir() ? You can pass it wildcards Like my*file.pd? Perhaps it might be a way to compromise. Sure you wouldn't get the full power of a regex, but you could limit your files and then actually test to see what exists.

Personally I would use FileExists() rather than looping over a directory and testing every file. If the directory got huge than your loop could take forever. Though if you use Dir() you could potentially limit the number of files you need to iterate over. Plus FileExists() is exactly one line/statement and cleaner... though then you have to make sure the Scripting Runtime is included in your project

http://techonthenet.com/excel/formulas/dir.php
http://www.blueclaw-db.com/listbox-directory.htm
http://www.hobbub.com/vba-vb-vsto/iterating-over-a-directory-with-a-dir-loop-loop-through-directory-vb/

Second and third links have better examples of iterating over the directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文