views:

476

answers:

1

I'm trying to create a batch file that will run Doug Cockford's JSMin against all the .js files in a directory. Here's what i've got:

for /f %%a IN ('dir /b /s *.js') do jsmin <%%a >%%~da%%~pamin\%%~na.min%%~xa

The problem is that the angle brackets (<>) cause the batch file to interpret it as 0< and 1>. Event running just:

jsmin <scripts\script.js >jsmin-stuff.js

in a batch file does the same thing. Escaping the angle brackets with ^ makes jsmin think that the angle brackets are part of the path.

Any ideas? What am I doing wrong?

+1  A: 

First of all, you are using for /f for something where for alone is sufficient. Secondly, I don't recommend doing this recursively, as you are obviously placing the min-ified scripts into a subfolder of the same tree.

for %x in (*.js) do jsmin <"%x" >"min\%~nx-min.js"

(from the command line, not from a batch) does what it should for me here. What you are seeing with 0< and 1> is actually correct, as 0 is the standard input stream and 1 the standard output stream, which you are both redirecting.

For doing this recursively the following line did the trick for me:

for /r %x in (*.js) do jsmin <"%x" >"%~dpx\min\%~nx-min.js"

ETA: Ok, I checked it, the dir method works in fact more reliable than using for /r. I apologize. From a batch file the following worked for me:

for /f "usebackq delims=" %%x in (`dir /s /b *.js`) do jsmin <"%%x" >"%%~dpx\min\%%~nx-min.js"

for /f always does tokenizing, usually only based on whitespace, though. But my profile folder already has whitespace in it, so better turn off tokenizing altogether.

Joey
That works if I type it directly into the command prompt, but when included in a batch file, it does not work. Recursion and the For loop are not the issue.Just running the following line from a batch file fails:jsmin <scripts\script.js >jsmin-stuff.js Why does it fail?How can it be changed so that it does not fail?
Freyday
By the way, I won't have any recursion problems using my original for loop since 'dir /b /s *.js' gets a list of files first... then creates new files.
Freyday