vbscript,在文件名中查找匹配项

vbscript,在文件名中查找匹配项,vbscript,Vbscript,我是VBScript新手,刚收到一项任务,要求我在文件名中找到6个具有匹配字符串的文件,以便将这些文件移动到其他目录。我使用正则表达式模式“\d{8}-\d{6}”来定位文件名中的所有字符串 如何在目录中进行搜索并检查文件名中是否有6个具有匹配字符串的文件,以便将它们存储到数组中,然后将文件移动到另一个目录 到目前为止,我写的剧本是: Set objFS = CreateObject("Scripting.FileSystemObject") strShareDirectory = "in\"

我是VBScript新手,刚收到一项任务,要求我在文件名中找到6个具有匹配字符串的文件,以便将这些文件移动到其他目录。我使用正则表达式模式“\d{8}-\d{6}”来定位文件名中的所有字符串

如何在目录中进行搜索并检查文件名中是否有6个具有匹配字符串的文件,以便将它们存储到数组中,然后将文件移动到另一个目录

到目前为止,我写的剧本是:

Set objFS = CreateObject("Scripting.FileSystemObject")

strShareDirectory = "in\"
strDumpStorageDir = "out\"

Set objFolder = objFS.GetFolder(strShareDirectory)
Set colFiles = objFolder.Files

Set re = New RegExp
re.Global     = True
re.IgnoreCase = False
re.Pattern    = "-\d{8}-\d{6}"

Dim curFile, matchValue
Dim i: i = 0

For Each objFile in colFiles
   bMatch = re.Test(objFile.Name)
   curFile = objFile.Name

   If bMatch Then
      ReDim preserve matches(i)
      Matches(i) = curFile
      i = (i + 1)

      For Each objFile1 in colFiles
        If objFile1.Name <> objFile.Name Then
            For each match in re.Execute(objFile1.Name)
                matchValue = match.Value
                Exit For
            Next
            If (Instr(curFile, matchValue) > 0) Then
                matchCount = 1
                For Each match1 in re.Execute(objFile1.Name)
                    curFile1 = objFile1.Name
                    matchValue1 = match1.Value
                    Exit For
                    'If  Then

                Next
                'msgbox(curFile1)
            End If
     End If
    Next
   End If
Next
Set objFS=CreateObject(“Scripting.FileSystemObject”)
strShareDirectory=“in\”
strDumpStorageDir=“out\”
Set objFolder=objFS.GetFolder(strsharediredirectory)
设置colFiles=objFolder.Files
Set re=New RegExp
re.Global=True
re.IgnoreCase=False
re.Pattern=“-\d{8}-\d{6}”
Dim curFile,matchValue
尺寸i:i=0
对于colFiles中的每个objFile
bMatch=re.Test(objFile.Name)
curFile=objFile.Name
如果匹配,那么
ReDim保存匹配(i)
匹配(i)=curFile
i=(i+1)
对于colFiles中的每个objFile1
如果objFile1.Name objFile.Name,则
对于re.Execute(objFile1.Name)中的每个匹配项
matchValue=match.Value
退出
下一个
如果(Instr(curFile,matchValue)>0,则
匹配计数=1
对于re.Execute(objFile1.Name)中的每个match1
curFile1=objFile1.Name
matchValue1=match1.Value
退出
”“那么
下一个
'msgbox(curFile1)
如果结束
如果结束
下一个
如果结束
下一个
下面是我正在使用的示例目录。 啊,现在我明白了。 因此:如果至少有6个文件具有相同的匹配子字符串,则需要所有与模式匹配的文件名。可以然后,是的,我知道你可能会在下一个循环中被勒死。如果出现这种情况,我建议将一些代码放入额外的函数中。
在这个解决方案中,我使用字典做一些工作要容易得多(例如,对“exists”的每次调用都是对其所有元素的另一个嵌套迭代,以及每个赋值)。
此示例将忽略一个文件名中的多个匹配项

option explicit

dim objFS : dim strShareDirectory : dim strDumpStorageDir : dim objFolder : dim colFiles : dim re : dim objFile

dim dictResults ' dictionary of [filename] -> [matching substring]
dim dictResultsCount ' dictionary of [matching substring] -> [count]
dim dictResultsFinal ' only the valid entries from dictResults
dim keyItem 
dim strMatch

set dictResultsFinal = CreateObject("Scripting.Dictionary")
set dictResults = CreateObject("Scripting.Dictionary")
set dictResultsCount = CreateObject("Scripting.Dictionary")

Set objFS = CreateObject("Scripting.FileSystemObject")

strShareDirectory = "in\"
strDumpStorageDir = "out\"

Set objFolder = objFS.GetFolder(strShareDirectory)
Set colFiles = objFolder.Files

Set re = New RegExp
re.Global     = True
re.IgnoreCase = False
re.Pattern    = "-\d{8}-\d{6}"

Dim curFile, matchValue
Dim i: i = 0

For Each objFile in colFiles
    ' test if the filename matches the pattern
    if re.test(objFile.Name) then
        ' for now, collect all matches without further checks
        strMatch = re.execute(objFile.Name)(0)
        dictResults(objFile.Name) = strMatch
        ' and count
        if not dictResultsCount.Exists(strMatch) then
            dictResultsCount(strMatch) = 1
        else
            dictResultsCount(strMatch) = dictResultsCount(strMatch) +1
        end if
    end if
next

' for testing: output all filenames that match the pattern
msgbox join(dictResults.keys(), vblf)

' now copy only the valid entries into a new dictionary
for each keyItem in dictResults.keys()
    if dictResultsCount.Exists( dictResults(keyItem) ) then
        if dictResultsCount( dictResults(keyItem) ) >= 6 then
            dictResultsFinal(keyItem) = 1
        end if
    end if
next

' test output the final result
msgbox join(dictResultsFinal.keys(), vblf)
---我的第一个答案

我应该问问你试过什么,但是。。。这是你的例子。 这应该给你足够的开始(我忽略了你提到的“6”要求)。问你是否需要更多的解释

Option explicit
dim a
a = findFiles("G:\",  "\d{8}-\d{6}")
msgbox join(a, vblf)

function findFiles(path, pattern)
    dim rx
    dim fso
    dim fsoFolder
    dim fsoFiles
    dim results
    dim item
    set rx = new regexp
    rx.pattern =  pattern
    set results = CreateObject("Scripting.Dictionary")
    set fso = CreateObject("Scripting.FileSystemObject")
    set fsoFolder = fso.GetFolder(path)
    set fsoFiles = fsoFolder.Files
    for each item in fsoFiles
        if rx.test(item.name) then results(item.name) = 1
    next
    set fso = nothing
    set fsoFolder = nothing
    set fsoFiles = nothing
    findFiles = results.keys()
end function
啊,现在我明白了。 因此:如果至少有6个文件具有相同的匹配子字符串,则需要所有与模式匹配的文件名。可以然后,是的,我知道你可能会在下一个循环中被勒死。如果出现这种情况,我建议将一些代码放入额外的函数中。
在这个解决方案中,我使用字典做一些工作要容易得多(例如,对“exists”的每次调用都是对其所有元素的另一个嵌套迭代,以及每个赋值)。
此示例将忽略一个文件名中的多个匹配项

option explicit

dim objFS : dim strShareDirectory : dim strDumpStorageDir : dim objFolder : dim colFiles : dim re : dim objFile

dim dictResults ' dictionary of [filename] -> [matching substring]
dim dictResultsCount ' dictionary of [matching substring] -> [count]
dim dictResultsFinal ' only the valid entries from dictResults
dim keyItem 
dim strMatch

set dictResultsFinal = CreateObject("Scripting.Dictionary")
set dictResults = CreateObject("Scripting.Dictionary")
set dictResultsCount = CreateObject("Scripting.Dictionary")

Set objFS = CreateObject("Scripting.FileSystemObject")

strShareDirectory = "in\"
strDumpStorageDir = "out\"

Set objFolder = objFS.GetFolder(strShareDirectory)
Set colFiles = objFolder.Files

Set re = New RegExp
re.Global     = True
re.IgnoreCase = False
re.Pattern    = "-\d{8}-\d{6}"

Dim curFile, matchValue
Dim i: i = 0

For Each objFile in colFiles
    ' test if the filename matches the pattern
    if re.test(objFile.Name) then
        ' for now, collect all matches without further checks
        strMatch = re.execute(objFile.Name)(0)
        dictResults(objFile.Name) = strMatch
        ' and count
        if not dictResultsCount.Exists(strMatch) then
            dictResultsCount(strMatch) = 1
        else
            dictResultsCount(strMatch) = dictResultsCount(strMatch) +1
        end if
    end if
next

' for testing: output all filenames that match the pattern
msgbox join(dictResults.keys(), vblf)

' now copy only the valid entries into a new dictionary
for each keyItem in dictResults.keys()
    if dictResultsCount.Exists( dictResults(keyItem) ) then
        if dictResultsCount( dictResults(keyItem) ) >= 6 then
            dictResultsFinal(keyItem) = 1
        end if
    end if
next

' test output the final result
msgbox join(dictResultsFinal.keys(), vblf)
---我的第一个答案

我应该问问你试过什么,但是。。。这是你的例子。 这应该给你足够的开始(我忽略了你提到的“6”要求)。问你是否需要更多的解释

Option explicit
dim a
a = findFiles("G:\",  "\d{8}-\d{6}")
msgbox join(a, vblf)

function findFiles(path, pattern)
    dim rx
    dim fso
    dim fsoFolder
    dim fsoFiles
    dim results
    dim item
    set rx = new regexp
    rx.pattern =  pattern
    set results = CreateObject("Scripting.Dictionary")
    set fso = CreateObject("Scripting.FileSystemObject")
    set fsoFolder = fso.GetFolder(path)
    set fsoFiles = fsoFolder.Files
    for each item in fsoFiles
        if rx.test(item.name) then results(item.name) = 1
    next
    set fso = nothing
    set fsoFolder = nothing
    set fsoFiles = nothing
    findFiles = results.keys()
end function

由于@KekuSemau的建议没有解决文件分组的(子)问题,因此dweebles没有给出完整的故事(为什么是数组?为什么坚持要有一个完整的(子)文件集?)和数字(文件名中由6个、3/4个部分组成的组)与基本任务无关-根据部分文件名将一组文件分发到文件夹中-我声称解决该任务的方法是摆脱所有数组、字典和regexp的幻想,并保持简单:

之前:

tree /A /F ..\data
+---in
|       B-2
|       B-1
|       A-3
|       A-2
|       B-3
|       A-1
|
\---out
代码:

之后:

tree /A /F ..\data
+---in
\---out
    +---3
    |       A-3
    |       B-3
    |
    +---1
    |       B-1
    |       A-1
    |
    \---2
            B-2
            A-2
p.S.
这可以用同样的方法来解决。

由于@KekuSemau的建议没有解决文件分组的(子)问题,dweebles没有给出完整的故事(为什么是数组?为什么坚持要有完整的(子)文件集?)和数字(文件名中由6、3/4部分组成的组)与基本任务无关-根据部分文件名将一组文件分发到文件夹中-我声称解决该任务的方法是摆脱所有数组、字典和regexp的幻想,并保持简单:

之前:

tree /A /F ..\data
+---in
|       B-2
|       B-1
|       A-3
|       A-2
|       B-3
|       A-1
|
\---out
代码:

之后:

tree /A /F ..\data
+---in
\---out
    +---3
    |       A-3
    |       B-3
    |
    +---1
    |       B-1
    |       A-1
    |
    \---2
            B-2
            A-2
p.S.
这可以用同样的方法来解决。

1。)我有点困惑应该是什么6?是否要检查一个文件名中的模式是否匹配六次?很抱歉造成混淆,如果文件名中有6个数字相同的文件,我想将文件发送出去。是的,这会更清楚;-)非常感谢!:)我真的需要一个指导来弄清楚我将如何着手解决这个问题1)我有点困惑应该是什么6?是否要检查一个文件名中的模式是否匹配六次?很抱歉造成混淆,如果文件名中有6个数字相同的文件,我想将文件发送出去。是的,这会更清楚;-)非常感谢!:)我真的需要一个指导来弄清楚我将如何着手解决这个问题^非常正确,我真的需要一个更简单有效的方法来解决这个问题。谢谢没错,我真的需要一个更简单有效的方法来解决这个问题。谢谢