How to use file expansion (e.g., *.*) with any command?

Ugo

Aug 22, 2013
10
0
#1
Short version
Compare
dir *.txt
(list of all the files with extension txt)
and
echo *.txt
(just print *.txt)
How to make echo behave like dir with the * symbol?

Long version - A real case

Figure out this:
There is a directory with thousands of files names *.txt, each of them with numbers using the comma as a decimal separator. We want to convert all the commas inside the files into dots.
I know how to do it with perl, running just this single command on a shell like bash:
perl -pi -e "s/,/./g" *.txt

Now, do not focus on the perl script and options, just look at the trailing part of the command, that is
<command> <options> *.txt
Look at *.txt: I was assuming that it is expanded into a list of all the files with extension *.txt by the shell, so that the real command, in this case "perl", does not see *.txt: it should receieve directly the expanded list of existing files in the current directory.

Now this is true for shells like bash, but it does not happen on take command nor on cmd.
On the other hand, on take command some specific commands, such as dir, do accept the * symbol and it is their own responsibility to replace the pattern with a list of existing files.

How can I pass to an external command a list of existing files matching a given pattern?
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,659
46
Albuquerque, NM
prospero.unm.edu
#2
Check out the @EXPAND function. (But be aware that if there are a large number of matching files, it's possible to overflow the command line buffer.)

Often, instead of passing a long list of filenames to a command, it makes more sense to call the command repeatedly using a DO or FOR loop.
 
Likes: Ugo
#3
You could construct the list fairly easily.
Code:
v:\> unset filelist

v:\> do f in *.txt ( set filelist=%filelist "%f" )

v:\> echo %filelist
 "folderids.txt" "f_server.txt" "test.txt"
Better ... you could make a function to create the list for you. Quite probably mine can be improved upon.
Code:
v:\> function filelist `%@execstr[do f in %$ ( echos "%%f"^^^^s )]`

v:\> echo %@filelist[*.txt]
"folderids.txt" "f_server.txt" "test.txt"

v:\> echo %@filelist[*.txt;*.zip]
"folderids.txt" "f_server.txt" "test.txt" "alljp.zip" "ssmon.zip"
 
#4
Check out the @EXPAND function. (But be aware that if there are a large number of matching files, it's possible to overflow the command line buffer.)

Often, instead of passing a long list of filenames to a command, it makes more sense to call the command repeatedly using a DO or FOR loop.
I thought it was built-in but gave up looking after a couple minutes. :)
And it only quotes the ones that need quoting.