Call to Powershell dir function not allowing more than 1 wildcard

easperhe29

New Member
Joined
May 11, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
All,

I can't get the power shell dir function to work with more than one wildcard through excel vba. If I have this directory structure:

C:\Test1\Test2\Sample1.xlsm
C:\Test1\Test2\Sample2.xlsm

This VBA call to power shell will return 2 files

fileRegExp = "C:\Test\Test2\*.xlsm"
files = Split(CreateObject("WScript.Shell").Exec("cmd /c dir " & """" & fileRegExp & """" & " /b").StdOut.ReadAll, vbCrLf)


But all these other options don't return any files

fileRegExp = "C:\Test\*\*.xlsm"
fileRegExp = "C:\Test\Test?\*.xlsm"
files = Split(CreateObject("WScript.Shell").Exec("cmd /c dir " & """" & fileRegExp & """" & " /b").StdOut.ReadAll, vbCrLf)

For somereason, it seems like only one wildcard is accepted. But in powershell itself, all of these work fine. Any ideas??
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
The cmd in that code runs the old DOS shell (command prompt), not PowerShell. DOS allows wildcards only in the file name, not the folder name.

The PowerShell version would be:

VBA Code:
    files = Split(CreateObject("WScript.Shell").Exec("powershell.exe -command dir """ & fileRegExp & """").StdOut.ReadAll, vbCrLf)
But note I've omitted the /b (bare listing) switch because it isn't supported in PS - you'll have to find out the equivalent.
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top