Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for wildcards.vbs

(view source code of wildcards.vbs as plain text)

  1. Option Explicit
  2.  
  3. WScript.Echo Join( GetFiles( WScript.Arguments.Unnamed.Item(0) ), vbCrLf )
  4.  
  5.  
  6. Function GetFiles( strFilespec )
  7. ' Name     : GetFiles
  8. ' Function : List all files matching the specified filespec, all DOS wildcards allowed
  9. ' Returns  : An array of fully qualified paths of all matching files, or a single element array with the text "Error"
  10. ' Remarks  : If no folder is specified, the current directory will be assumed.
  11. '            DOS wildcards "*" and "?" are allowed in the FILE name and/or extension, but NOT in the DIRECTORIES.
  12. '            E.g. "D:\folder\*file*_*.??" is allowed, but "D:\folder\*\file*.??" is NOT.
  13. ' Author   : Rob van der Woude, http://www.robvanderwoude.com
  14. ' Version  : 1.00, 2017-01-31
  15.  
  16. 	Dim colFiles, objFile, objFolder, objFSO, objRE, wshShell
  17. 	Dim strFiles, strFolder, strPattern
  18.  
  19. 	GetFiles = Array( "Error" )
  20.  
  21. 	' Return "Error" if no filespec is specified
  22. 	If Trim( strFilespec ) = "" Then Exit Function
  23.  
  24. 	' Handle (unlikely) error in specified UNC path
  25. 	Set objRE = New RegExp
  26. 	objRE.Pattern = "^\\\\" ' Check if filespec starts with double backslash
  27. 	If objRE.Test( strFilespec ) Then
  28. 		' Check if filespec is a valid UNC path: \\server\share\relativepath
  29. 		objRE.Pattern = "^\\\\[\w-]+\\[\w\$-]+\\[^\\]"
  30. 		If Not objRE.Test( strFilespec ) Then
  31. 			Set objRE = Nothing
  32. 			Exit Function
  33. 		End If
  34. 	End If
  35.  
  36. 	Set wshShell = CreateObject( "WScript.Shell" )
  37. 	Set objFSO   = CreateObject( "Scripting.FileSystemObject" )
  38.  
  39. 	If InStr( strFilespec, "\" ) And Len( strFilespec ) > 1 Then
  40. 		' If filespec starts with single backslash, prefix it with current directory's drive
  41. 		If Left( strFilespec, 1 ) = "\" And Not Left( strFilespec, 2 ) = "\\" Then
  42. 			strFilespec = objFSO.GetDriveName( wshShell.CurrentDirectory ) & strFilespec
  43. 		End If
  44. 		' Split filespec into parent directory and actual FILE spec
  45. 		strFolder   = Mid( strFilespec, 1, InStrRev( strFilespec, "\" ) )
  46. 		strFilespec = Mid( strFilespec, InStrRev( strFilespec, "\" ) + 1 )
  47. 	End If
  48. 	' Assume current directory if no parent directory is specified
  49. 	If strFolder = "" Then strFolder = wshShell.CurrentDirectory
  50. 	' Quit if folder does not exist
  51. 	If Not objFSO.FolderExists( strFolder ) Then
  52. 		Set objRE    = Nothing
  53. 		Set objFSO   = Nothing
  54. 		Set wshShell = Nothing
  55. 		Exit Function
  56. 	End If
  57.  
  58. 	' Convert DOS wildcards to regex pattern
  59. 	objRE.Pattern    = "([\.\(\)\[\]\{\}\$])"
  60. 	objRE.Global     = True
  61. 	objRE.IgnoreCase = True
  62. 	strPattern       = objRE.Replace( strFilespec, "\$1" )
  63. 	strPattern       = Replace( strPattern,  "?", "[^\\]"  )
  64. 	strPattern       = Replace( strPattern,  "*", "[^\\]*" )
  65. 	objRE.Pattern    = "(^|\\)" & strPattern & "$"
  66.  
  67. 	' Get a collection of files
  68. 	Set objFolder = objFSO.GetFolder( strFolder )
  69. 	Set colFiles  = objFolder.Files
  70. 	strFiles = ""
  71. 	' Iterate through the list of files
  72. 	For Each objFile In colFiles
  73. 		' Check if the file name matches filespec
  74. 		If objRE.Test( objFile.Path ) Then
  75. 			' Add the file to the list
  76. 			strFiles = strFiles & ";" & objFile.Path
  77. 		End If
  78. 	Next
  79.  
  80. 	' Return the list of files as an array
  81. 	GetFiles = Split( Mid( strFiles, 2 ), ";" )
  82.  
  83. 	' Cleanup
  84. 	Set colFiles  = Nothing
  85. 	Set objFolder = Nothing
  86. 	Set objRE     = Nothing
  87. 	Set objFSO    = Nothing
  88. 	Set wshShell  = Nothing
  89. End Function
  90.  

page last modified: 2024-04-16; loaded in 0.0205 seconds