简体   繁体   English

遍历目录中的所有数据库

[英]Looping over all databases in a directory

I need to loop over all databases in a certain directory, where the database name is ACPwxyz.mdb , where wxyz is the equivalent to an MMYY value for the period that database was used for. 我需要遍历某个目录中的所有数据库,其中数据库名称为ACPwxyz.mdb ,其中wxyz等于该数据库使用期间的MMYY值。

Eg, the database for July 2017 would be ACP0717.mdb. 例如,2017年7月的数据库为ACP0717.mdb。

I've never written in VB6 before and I totally hate it, but it's an extension to an existing project so I'm stuck with it! 我以前从未用VB6编写过文章,但我完全讨厌它,但这是对现有项目的扩展,因此我坚持使用它!

Is there a way of looping over all files in a directory, checking if the file name follows the format of ACPwxyz.mdb or not, and if it does, then opening a connection to it? 有没有一种方法可以循环遍历目录中的所有文件,检查文件名是否遵循ACPwxyz.mdb的格式,如果符合,则打开与该文件的连接?

I've looked around a bit and see Dir(x, y) , but I'm not sure if I can use this in this situation? 我四处看看,看到了Dir(x, y) ,但是我不确定在这种情况下是否可以使用它?

Any tips would be appreciated. 任何提示将不胜感激。

You can use Dir , yes. 您可以使用Dir ,是的。

If you use something like this: 如果您使用以下内容:

Dim strFile As String
strFile = Dir(yourDBPath, "ACP????.mdb") ' mdb for MS-Access files

Do Until strFile = ""
 If Len(strFile) = 11 Then ' Ensure the DB file name is 11 characters, which yours are

   'Do something // You can also check the file name doesn't = a certain name if needed

 End If

 strFile = Dir
Loop

Dir accepts either an asterisk (*), or a question mark (?) as wildcards in file names, so this will look for any database in the set path that is called ACP followed by 4 characters. Dir接受星号(*)或问号(?)作为文件名中的通配符,因此,它将在设置的路径中查找名为ACP后跟4个字符的任何数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM