1.

Solve : Batch file - Multiple files to a command at once?

Answer»

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. 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. 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.

Quote from: gregory on September 23, 2008, 01:18:16 AM

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!
Quote from: Dias de verano 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.

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



Quote from: Sidewinder on September 24, 2008, 04:14:42 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 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

Quote from: Sidewinder 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.

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.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-
grammaticalBased 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. Simply reporting back as I said I would. The code does indeed work.

Thanks again.


Discussion

No Comment Found