Is there a VBscript equivalent to the php glob fun

2019-08-05 12:40发布

问题:

I have this one line of code in php that returns an array of file names something like this;

1 => folderName/elev-a.jpg
1 => folderName/elev-b.jpg
1 => folderName/elev-c.jpg
1 => folderName/elev-d.jpg

.. this is the code

$elev = glob($folderName.'/elev-[a-z].jpg');

I now need to use this in a program in ASP. From what I've read so far it sounds like I need to use a dictionary object, a file system object, a Regex object, and some looping. That seems like maybe I'm missing something in VBscript that I may not know about. Would it really be that difficult to replicate that one function? I'm lost.

回答1:

I've not done a whole lot of server-side scripting, but I had something lying around similar to what you're looking for:

Function GetFileNames(Directory, Pattern)
Dim FileNames(), AfterPattern()
Dim FolderObject, fso, f
Dim i
i = 0

Set fso = CreateObject("Scripting.FileSystemObject")
Set FolderObject = fso.GetFolder(Directory)
ReDim FileNames(FolderObject.Files.Count)
ReDim AfterPattern(FolderObject.Files.Count)
For Each f In FolderObject.Files
    FileNames(i) = f.Path
    i = i + 1
Next
'Pattern Portion
Dim RegExp
Set RegExp = CreateObject("VBScript.RegExp")
RegExp.Pattern = Pattern
RegExp.Global = False
Dim x
x = -1
For i = LBound(FileNames()) To UBound(FileNames())
    If RegExp.Test(FileNames(i)) = True Then
        x = x + 1
        AfterPattern(x) = FileNames(i)

    End If
Next
ReDim Preserve AfterPattern(x+1)
GetFileNames = AfterPattern()


Set fso = Nothing
Set FolderObject = Nothing
Set RegExp = Nothing

End Function

The second argument is a Regular Expression pattern as you mentioned in your question. :-)

Cheers, LC



回答2:

Try the following:
$elev = glob($folderName.'/elev-'.'*');



标签: vbscript glob