使用目录找到没有“AAA”的文件 [英] Use Dir to find file without "AAA"

查看:117
本文介绍了使用目录找到没有“AAA”的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些名为Team-(Random Number).txt的文件。

 目录(Team-&*&.txt)


由于Dir只返回1个文件,这样做是否有一种获取文件Team-(Random Number).txt的方法?



如果dir以正常的顺序返回结果,但显然它是随机的,那么应该没问题。



我以为排除了-AAA部分,但不知道语法应该如何。或者以不太有效的方式获取所有文件并将其排序在数组中,但是使用10 - 200个文件不是非常有效。



现在我希望能给我我的问题排除部分或其他解决方法的语法谢谢!

解决方案

我会说正式表达式。

  Private Sub TeamTxtExists()

Dim Path As String,Pattern As String,FileFound As String
Dim REGEX As Object,Matches As Object
Dim oFO As Object,oFolder As Object,oFile As Object

Path =D:\Personal\Stack Overflow\必要时进行修改。
Pattern =(Team-(\d +)。txt)
设置REGEX = CreateObject(VBScript.RegExp)
使用REGEX
.Pattern = Pattern
.Global = True
.IgnoreCase = True
结束

设置oFSO = CreateObject(Scripting.FileSystemObject)
设置oFolder = oFSO.GetFolder路径)
对于每个oFile在oFolder.Files
设置匹配= REGEX.Execute(oFile.Name)
匹配中的每个匹配
Debug.Print Match.Value
下一个比赛
下一个oFile

End Sub

在您的立即窗口(VB-G中的Ctrl-G)打印文件名中没有 AAA 等文本文件的所有名称。试用和测试。


I have some files that are called Team-(Random Number).txt.

Dir("Team-" & "*" & ".txt")

But when there have been changes there could be text files called Team-(Random Number)-AAA.txt, Team-(Random Number)-AAB.txt and so but the most recent file is always called Team-(Random Number).txt.

Since Dir only returns 1 file and does this randomly is there a way to get the file Team-(Random Number).txt?

It should be no problem if dir returned the result in a normal order but apparently it does it randomly.

I've thought of excluding the -AAA part but don't know what the syntax should. Or in a less efficient way to get all files and sort it in an array but with 10 - 200 files it's not very efficient.

Now I'm hoping could give me the syntax of excluding the part or other workaround for my problem thanks!

解决方案

I'd say go for Regular Expressions.

Private Sub TeamTxtExists()

    Dim Path As String, Pattern As String, FileFound As String
    Dim REGEX As Object, Matches As Object
    Dim oFSO As Object, oFolder As Object, oFile As Object

    Path = "D:\Personal\Stack Overflow\" 'Modify as necessary.
    Pattern = "(Team-(\d+).txt)"
    Set REGEX = CreateObject("VBScript.RegExp")
    With REGEX
        .Pattern = Pattern
        .Global = True
        .IgnoreCase = True
    End With

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder(Path)
    For Each oFile In oFolder.Files
        Set Matches = REGEX.Execute(oFile.Name)
        For Each Match In Matches
            Debug.Print Match.Value
        Next Match
    Next oFile

End Sub

This will print in your immediate window (Ctrl-G in the VBE) all the names of text files that don't have AAA or the like in their filenames. Tried and tested.

这篇关于使用目录找到没有“AAA”的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆