Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for wildcardsle.vbs

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

  1. Option Explicit
  2.  
  3. WScript.Echo Join( GetFilesLE( WScript.Arguments.Unnamed.Item(0) ), vbCrLf )
  4.  
  5.  
  6. Function GetFilesLE( strFilespec )
  7. ' Name     : GetFilesLE
  8. ' Function : List all files matching the specified filespec, all DOS wildcards allowed;
  9. '            Light Edition of GetFiles( ) function, but FILE names only, no directories
  10. ' Returns  : An array of all matching file names (no path), or a single element array with the text "Error"
  11. ' Remarks  : Since no folder can be specified, the current directory will be assumed.
  12. '            DOS wildcards "*" and "?" are allowed in the file name as well as in the extension.
  13. ' Author   : Rob van der Woude, http://www.robvanderwoude.com
  14. ' Version  : 1.00, 2017-02-02
  15.  
  16. 	Dim colFiles, objFile, objFSO, objRE, wshShell
  17. 	Dim strFiles, strPattern
  18.  
  19. 	' Return "Error" on missing or invalid filespec
  20. 	GetFilesLE = Array( "Error" )
  21. 	If Trim( strFilespec ) = ""  Then Exit Function
  22. 	If InStr( strFilespec, "\" ) Then Exit Function
  23.  
  24. 	Set wshShell = CreateObject( "WScript.Shell" )
  25. 	Set objFSO   = CreateObject( "Scripting.FileSystemObject" )
  26. 	Set objRE    = New RegExp
  27.  
  28. 	' Convert DOS wildcards to regex pattern
  29. 	objRE.Pattern    = "([\.\(\)\[\]\{\}\$])"
  30. 	objRE.Global     = True
  31. 	objRE.IgnoreCase = True
  32. 	strPattern       = objRE.Replace( strFilespec, "\$1" )
  33. 	strPattern       = Replace( strPattern,  "?", "[^\\]"  )
  34. 	strPattern       = Replace( strPattern,  "*", "[^\\]*" )
  35. 	objRE.Pattern    = "(^|\\)" & strPattern & "$"
  36.  
  37. 	' Get a collection of files
  38. 	Set colFiles  = objFSO.GetFolder( wshShell.CurrentDirectory ).Files
  39. 	strFiles = ""
  40. 	' Iterate through the list of files
  41. 	For Each objFile In colFiles
  42. 		' Check if the file name matches filespec
  43. 		If objRE.Test( objFile.Path ) Then
  44. 			' Add the file to the list
  45. 			strFiles = strFiles & ";" & objFile.Name
  46. 		End If
  47. 	Next
  48.  
  49. 	' Return the list of files as an array
  50. 	GetFilesLE = Split( Mid( strFiles, 2 ), ";" )
  51.  
  52. 	' Cleanup
  53. 	Set colFiles  = Nothing
  54. 	Set objRE     = Nothing
  55. 	Set objFSO    = Nothing
  56. 	Set wshShell  = Nothing
  57. End Function
  58.  

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