1

Possible Duplicate:
How do you loop through each line in a text file using a windows batch file?

Need to use a large list of file names to construct an argument that goes to an application that is called from the Windows command line. I am trying to do this all from within a Windows Batch file.

So far I have this:

dir C:\javascripts /a-d /b /s >FileListing.txt

This creates a file called "FileListing.txt" whos contents look like this:

C:\javascripts\index.js
C:\javascripts\other_javascripts.js
...
C:\javascripts\helper.js

The next thing I need to do is call an executable that uses these file names as arguments in this way:

java -jar compiler.jar --js=C:\javascripts\index.js 
                       --js=C:\javascripts\other_javascripts.js 
                       ... 
                       --js_output_file=compiled_javascripts.js

This is for use with the google javascript compiler. The objective is to take all these javascripts and minify and compile them into one file.

How do I get these file names into the command line arguments?

Thanks so much!

Community
  • 1
  • 1
Chris Dutrow
  • 48,402
  • 65
  • 188
  • 258

1 Answers1

3

Here you go:

@echo off
setlocal enabledelayedexpansion
set JAVA_CMD=java -jar compilar.jar
for /f "tokens=*" %%f in (FileListing.txt) do set JAVA_CMD=!JAVA_CMD! --js=%%f
set JAVA_CMD=!JAVA_CMD! --js_output_file=compiled_javascripts.js

:: Invoke the command
!JAVA_CMD!


Suggestion for improvement:
Lose the text file, and invoke the dir command from within the loop (note the omitted /s), like so:

for /f "tokens=*" %%f in ('dir C:\javascripts /a-d /b') do set JAVA_CMD=!JAVA_CMD! --js=%%f

... the rest of the script stays the same.

Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • 2
    @ChrisDutrow Glad to help! Notice that I added the `"tokens=*"` to pull the entire line from the file. Otherwise it'll pull just the first word from each line (which is OK too, unless you have spaces in the filenames). – Eitan T Jul 29 '12 at 18:12