1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'Support' started by Ugo, Jan 16, 2014.

  1. Ugo

    Ugo

    Joined:
    Aug 22, 2013
    Messages:
    10
    Likes Received:
    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?
     
  2. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,288
    Likes Received:
    39
    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.
     
    Ugo likes this.
  3. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,885
    Likes Received:
    29
    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. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,885
    Likes Received:
    29
    I thought it was built-in but gave up looking after a couple minutes. :)
    And it only quotes the ones that need quoting.
     
  5. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,288
    Likes Received:
    39
    It is a peculiar name, isn't it? Now, if he'd only given it an intuitive name like @GLOB.... >:-D
     
  6. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,885
    Likes Received:
    29
    The name is fitting but it also fits many other scenarios.
     
  7. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,188
    Likes Received:
    11
    Another alternative would be to use TPIPE with the /replace switch to do the work.
     

Share This Page