Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file - Multiple files to a command at once  (Read 7091 times)

0 Members and 1 Guest are viewing this topic.

gregory

    Topic Starter


    Beginner

    Thanked: 1
    • Experience: Experienced
    • OS: Linux variant
    Batch file - Multiple files to a command at once
    « on: September 23, 2008, 01:18:16 AM »
    Hello everyone, I am new to this forum.

    My question involves batch files, please bear in mind this is only my second batch file, and I am almost completely novice.

    I was using the FOR command "FOR %%variable IN (set) DO command [command parameters]" to send all files in a sole directory to an external program. I got this to work just fine. Problem is, each file was being sent one at a time. I need to find a way to send all the files in the directory to the program at once. Any ideas?

    An example of the code i was using.

    Code: [Select]
    FOR %%X IN (*.wav) DO "G:\Program Files\flac\flac.exe" "%%X"

    Secondly, is there a way to "DO" multiple commands within the FOR?

    Thanks for the help. :)

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Batch file - Multiple files to a command at once
    « Reply #1 on: September 23, 2008, 09:43:27 AM »
    Not sure what you mean by all at once. You could try something like this:

    Code: [Select]
    "G:\Program Files\flac\flac.exe" *.wav

    Doubtful that would work. You can try stringing the file names on the command line, something like this:

    Code: [Select]
    @echo off
    setlocal
    for %%x in (*.wav) do (
    call set strCmd=%%strCmd%%,"%%x"
    )
    set strCmd=%strCmd:~1%

    "g:\program files\flac\flac.exe" %strCmd%
    endlocal

    That could possibly work. Depends more on how the program accepts input. Use parenthesis to "DO" multiple commands within the FOR. The snippet above shows how.

    Good luck.  8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    Dias de verano

    • Guest
    Re: Batch file - Multiple files to a command at once
    « Reply #2 on: September 23, 2008, 11:42:59 AM »
    I think the separator for multiple input files is white space not a comma, and that implies that filenames which contain spaces need quote marks before and after.

    Secondly, is there a way to "DO" multiple commands within the FOR?

    FOR %%variable IN (set) DO (
        command1 [command parameters]
        command2 [command parameters]
        commandn [command parameters]
        )


    Beware of setting variables within the loop, you will need to use delayed expansion, or they won't work!

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Batch file - Multiple files to a command at once
    « Reply #3 on: September 24, 2008, 04:14:42 AM »
    I think the separator for multiple input files is white space not a comma, and that implies that filenames which contain spaces need quote marks before and after.

    Not only white space, but commas, semi-colons and equal symbols are perfectly acceptable to the interpreter for use as separators.

    To guard against space embedded file names, the original code brackets each file name with quotes:

    Code: [Select]
    @echo off
    setlocal
    for %%x in (*.wav) do (
    call set strCmd=%%strCmd%%,"%%x"
    )
    set strCmd=%strCmd:~1%

    "g:\program files\flac\flac.exe" %strCmd%
    endlocal

     8)

    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    Dias de verano

    • Guest
    Re: Batch file - Multiple files to a command at once
    « Reply #4 on: September 24, 2008, 10:06:25 AM »
    Not only white space, but commas, semi-colons and equal symbols are perfectly acceptable to the interpreter for use as separators.

    I'm not talking about "the interpreter", I'm referring to flac.exe

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Batch file - Multiple files to a command at once
    « Reply #5 on: September 24, 2008, 11:35:49 AM »
    Oops! From what I could find out the OP's original code was correct. I didn't find any examples of how to send multiple files to flac.exe other then iterating a directory.

    Examples

    Flac FAQ

     8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    Dias de verano

    • Guest
    Re: Batch file - Multiple files to a command at once
    « Reply #6 on: September 24, 2008, 11:46:46 AM »
    Oops! From what I could find out the OP's original code was correct. I didn't find any examples of how to send multiple files to flac.exe other then iterating a directory.

    Indeed.

    This suggestion from the FAQ, in answer to the question "why don't wildcards work?"...

    Quote
    for %F in (*.wav) do flac "%F"

    That is a command line example - double up the % signs if including in a batch.

    This implies that flac can only do 1 input file at a time, and that the answer to the OP's question, which was...

    Quote
    Problem is, each file was being sent one at a time. I need to find a way to send all the files in the directory to the program at once.

    ...is, "Sorry, there isn't a way to do that, because flac.exe won't accept multiple file names"

    I'd be interested to know why the OP wants to send multiple filenames all at once to flac.exe, since even if it could accept them, it would still process them one after the other, and the result would be exactly the same as the solutions outlined above.

    gregory

      Topic Starter


      Beginner

      Thanked: 1
      • Experience: Experienced
      • OS: Linux variant
      Re: Batch file - Multiple files to a command at once
      « Reply #7 on: September 25, 2008, 02:03:33 AM »
      Thanks for all the help you two, it is much appreciated.  :)

      Indeed flac.exe can accept multiple files. White spaces are the seperators, each file name wrapped in quotes if it contains spaces. File names are to be listed after flac.exe and some options.
      Code: [Select]
      flac [various options] "filename1" "filename2" "filename3"
      As to why I want to do this; flac supports something called "replay gain" http://replaygain.hydrogenaudio.org/. Each file will be encoded separately, however flac.exe must know which files are considered an album/group to properly calculate replay gain values for each individual track.

      I think sidewinders idea/code of stringing the file names together should work. I don't fully understand what exactly the code is doing, but I will try it out and report back.

      Thanks again.

      -edit-
      grammatical
      « Last Edit: September 25, 2008, 06:29:28 PM by gregory »

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: Batch file - Multiple files to a command at once
      « Reply #8 on: September 25, 2008, 03:38:02 AM »
      Based on your latest post, this will give you white space separators on the command line:

      Code: [Select]
      @echo off
      for %%x in (*.wav) do (
      call set strCmd=%%strCmd%% "%%x"
      )
      "g:\program files\flac\flac.exe" [various options] %strCmd%

      Be sure to put in the various options before running the code. The code is designed as a batch file.
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      gregory

        Topic Starter


        Beginner

        Thanked: 1
        • Experience: Experienced
        • OS: Linux variant
        Re: Batch file - Multiple files to a command at once
        « Reply #9 on: September 29, 2008, 02:57:47 PM »
        Simply reporting back as I said I would. The code does indeed work.

        Thanks again.