Rob van der Woude's Scripting Pages
Powered by GeSHi

VBScript Scripting Techniques > Working with Files > Handle DOS Wildcards

Handle DOS Wildcards

 

RegExp: GetFiles
List matching file names, absolute or relative path allowed in input filespec, output contains fully qualified paths
VBScript Code: Download 💾
  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
 
Requirements:
Windows version: any
Network: any
Script Engine: any
Summarized: Works in all Windows versions with all scripting engines.
 
[Back to the top of this page]

 

RegExp: GetFilesLE
GetFiles Light Edition: file names only for input (no path), output contains matching file names (and extensions) only
VBScript Code: Download 💾
  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
 
Requirements:
Windows version: any
Network: any
Script Engine: any
Summarized: Works in all Windows versions with all scripting engines.
 
[Back to the top of this page]

page last modified: 2022-10-26; loaded in 0.0060 seconds