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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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