Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Occasionally, the cmd shell's inability to expand wildcard paths can really be an inconvenience. I had to pass 100 files in a directory to a program, and couldn't type *.ext. Instead, I used mingw's 'ls' to dump the list to a file, then replaced newlines with spaces, copied and pasted into cmd. Quite a nightmare.

I suspect the answer will be no, but has anyone dealt with this or come up with any way to make this easier?

share|improve this question
    
Have you considered using something like Powershell instead? –  user3463 Aug 12 '12 at 6:33
    
I suspect the answer will be no, but has anyone dealt with this or come up with any way to make this easier? Actually, I’m having the opposite problem, I am trying to figure out a way to get the command-interpreter to treat its list as strings and prevent it from inreptreting them as wildcards. For example, for %i in (foobar baz really?) do @echo %i will treat the last item (really?) as a filename wildcard, and skip it if there are no files named really1, reallyz, etc. ☹ –  Synetech Feb 4 '13 at 18:19

3 Answers 3

DOS, and consequently Windows' cmd.exe, never supported wildcard expansion by the shell. It was always up to the application to do the expansion.

This means you will always have to find another route if you're using an application that doesn't support expansion. You could:

  • Use a FOR loop to run some commands against all the files you're interested in
  • Use dir /b > list.txt to use the dir command to perform the expansion and put the list of files into a text file (you could then use something like an Excel formula if you're really desperate to produce a list of commands to paste into cmd, or you could use Excel to transpose the cells so all the filenames are on the same line.)
share|improve this answer
    
He explained that it would be a nightmare to output results of dir into a file and process filtering and copying manually. wmz's answer and of course mine would work without manually filtering a files content. –  wullxz Aug 13 '12 at 2:51
    
@wullxz: It is a nightmare doing it manually. But if you know some basic Excel, it is surprisingly easy to do something like this very quickly. The OP's "replacing newlines with spaces" for example is a copy-paste-transpose that takes a couple of seconds. –  Malvineous Aug 14 '12 at 0:09
    
right, but it still takes up too much time IMHO. –  wullxz Aug 14 '12 at 4:43
    
@wullxz: Perhaps, but for someone unfamiliar with Powershell wanting to do this as a one-off, it could be considerably faster - and the idea behind this site is to provide useful solutions for people of all skill levels to refer to now and in the future, so really there's no need to tell me off because you think my answer isn't as good as yours. –  Malvineous Aug 14 '12 at 8:07

This will give you a list and also put it in variable named expanded_list. Put it in batch file and run with myBatchFile name myPattern. Enclose pattern with quotation marks if it includes spaces. Matches files, no dirs. Run without parameters matches all.

@echo off
set expanded_list=
for /f "tokens=*" %%F in ('dir /b /a:-d "%~1"') do call set expanded_list=%%expanded_list%% "%%F"

echo expanded_list is: 
echo %expanded_list%

You can then run you command with my_command_exec %expanded_list%

WARNING! Maximum size of cmd variable is 8191 characters (XP onwards), so it's possible to overflow it! You cannot count on the utility to always give you complete list. On the other hand, maximum cmd line length is also 8191 so you would not be able to execute it anyway.

share|improve this answer
    
Note if no arguments are specified then expanded_list will be set to all files in the current directory which may or may not be what is wanted so might want add check for no arguments (e.g. if "%1"=="" ...) –  JasonM1 Feb 27 at 19:04

Just use Powershell, which is preinstalled on Windows 7. Powershell is capable of running cmd commands and does understand wildcards at any place in a path.

To start Powershell just type "powershell" in your start menu search box and hit enter.


In case the application expects a string with all filenames in it this is the right script:

$delimiter = " "
[string]$files = $nothing ; ls *.txt | % { $files += $_.fullname + $delimiter } ; application.exe $files

Change $delimiter = " " to $delimiter = "," if your application expects a comma separated list of file names.

Explanation of code:

  • [string]$files = $nothing - creates an empty variable of type
  • string ; - is a separator for multiple commands, not a pipeline!
  • ls *.txt | % { $files += $_.fullname + $delimiter } - gets a list of all text files and creates a string with all filenames separated by the delimiter
  • application.exe $files - calls the application and passes the file list to it

You can even search for a file pattern recursively by adding -recurse to ls *.txt so the complete code would look like this:

$delimiter = " "
[string]$files = $nothing ; ls *.txt -recurse | % { $files += $_.fullname + $delimiter } ; application.exe $files

Edit:
To avoid irritations, ls and dir are aliases of Get-ChildItem and % is an alias to ForEach-Object. I keep my code with aliases used because it's shorter.

share|improve this answer
    
Alhough PS is certainly capable to fulfill the reqest and the advice to familiarize with it is sound - it's not as simple as you suggest. Running start notepad++ *.txt (with intention to open all textfiles - used notepad++ as it does handle multiple names) will get you nowhere –  wmz Aug 12 '12 at 12:43
1  
You could workaround that with dir *.txt | % { notepad++.exe $_ }. I tried that with simple notepad, because I don't have notepad++, and it worked. The command receives a list of all txt files in the current directory, passes the list on to the next command which loops through the list and executes notepad++ with the filename as parameter. –  wullxz Aug 12 '12 at 12:49
    
yes, but this is not equivalent. Think about grep "a b c" to find any of listed strings, where "a b c" would be expanded list –  wmz Aug 12 '12 at 13:42
1  
Running a command with a list of items as parameter is not equivalent to running command multiple times with each element from a list as a parameter. Another example: copy a+b+c result and copy a result copy b result copy c result. (and grep like utility do exists in PS, it's called select-string) –  wmz Aug 12 '12 at 14:14
1  
upvoted, btw it's a pity that OP does not care to chime in to explain... –  wmz Aug 13 '12 at 9:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.