Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

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

Aug
10
0
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?
 
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.
 
  • Like
Reactions: Ugo
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"
 
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.
 
Another alternative would be to use TPIPE with the /replace switch to do the work.
 

Similar threads

Back
Top