0

I have been using the following script with the google closure compiler for years. I never had to look at it, it just worked - I don't even know where I originally had it from, and some parts of it I don't even understand.

However, now it doesn't correctly use the file paths from the DIR part. Instead, it takes a far away superdirectory. I mean, if the said "dev" directory is at c:\some\long\path\to\dev, the %%f used is suddenly c:\some\long instead of c:\some\long\path\to\dev\output\someScript.js.

Running the DIR part individually returns the correct list of complete, absolute paths all the way to the intended files. Running the batch script from cmd yields the same error as running it with a double-click (both ways worked before).

Between the last time the script ran and now, I have not changed anything that I would know of. This is all on a Windows 8 machine.

@echo off
echo compressing files: 
for /f %%f in ('DIR /b /s ..\..\dev\output\*.js') do (
  echo %%~nxf
  java -jar compiler.jar --charset UTF-8 --js "%%f" --js_output_file "..\..\files\%%~nxf"

)
set /p id="Done. Hit enter to close!"

What is wrong?

Swiss Mister
  • 3,260
  • 2
  • 20
  • 42
  • 2
    Add `"delims="` after `/f` –  Oct 08 '16 at 22:45
  • THANK YOU @Noodles. Works with `for /f "delims=" %%f in ('DIR /b /s ..\..\dev\output\*.js') do (`! – Swiss Mister Oct 08 '16 at 22:50
  • 2
    Best to provide an accurate report of the path. Your new path evidently contains a space and `for /f` "tokenises" by default using space as a delimiter and assigning only the first token (ie. up to the space) to the `metavariabl` (in this case, `%%f`.) The `delims=` turns the default delimiters off, so the entire line is delivered to `%%f` and you are picking just the `N`ame and e`X`tension from `%%f` using `%%~nxf` – Magoo Oct 09 '16 at 02:50

2 Answers2

0

You might find it easier to use for /r.

@echo off
echo compressing files: 
for /r "..\..\dev\output" %%f in (*.js) do (
   echo %%~nxf
   java -jar compiler.jar --charset UTF-8 --js "%%f" --js_output_file  "..\..\files\%%~nxf"
)
set /p id="Done. Hit enter to close!"

But you can indeed use for /f and remove the space as a delimiter like noodles suggested.

soja
  • 1,587
  • 8
  • 8
  • never change a winning team... ;-) - thanks for the recommendation! Since I don't know all the intricacies of those commands, I'll rather stick with @noodle's recommendation which works fine for my task. – Swiss Mister Oct 09 '16 at 16:09
0

This answer is precisely @Noodle's answer - I'd just like to put it into an answer format for posterity:

Answer: Add "delims=" (including the apostrophes!) after /f.

Result:

@echo off
echo compressing files: 
for /f "delims=" %%f in ('DIR /b /s ..\..\dev\output\*.js') do (
  echo %%~nxf
  java -jar compiler.jar --charset UTF-8 --js "%%f" --js_output_file "..\..\files\%%~nxf"

)
set /p id="Done. Hit enter to close!"

About delims: https://stackoverflow.com/a/6722085/1598477

Community
  • 1
  • 1
Swiss Mister
  • 3,260
  • 2
  • 20
  • 42