21

Need to concatenate all of the javascript files in a directory and all of its sub-directories into one file.

Right now I have a very simple command in a batch file that concatenates all of the matching files in one directory into one file:

copy C:\javascripts\*.js concatenated.js

However, this only works for the one directory and not any of its sub-directories. How can I do this same thing and also include all of the matching files in the sub-directories?

Thanks so much!

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

2 Answers2

28

From the command line you can use

for /r "c:\javascripts" %F in (*.js) do @type "%F" >>concatenated.js

You might want want to first delete any existing concatenated.js before you run the above command.

From a batch file the percents need to be doubled

@echo off
del concatenated.js
for /r "c:\javascripts" %%F in (*.js) do type "%%F" >>concatenated.js

EDIT
It is a bit more efficient to put parentheses around the entire statement and use a single overwrite redirection instead of append redirection with each iteration. It also eliminates the need to delete the file in the beginning.

>concat.js (for /r "c:\javascripts" %F in (*.js) do @type "%F")

or from batch

@echo off
>concat.js (for /r "c:\javascripts" %%F in (*.js) do type "%%F")
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Your last example "or from batch" needs %F changed to %%F, or at least it does on win xp. – Mica Mar 20 '13 at 17:16
  • @Mica - Yep, all fixed. Thanks – dbenham Mar 20 '13 at 19:40
  • 1
    Is there any way to not have the file name & path echoed into the concatenated file? – Mica Mar 20 '13 at 21:39
  • Excellent, thanks! Noticed one small caveat: if the output file (e.g. concat.js) also has the extension of the to be concatenated files (e.g. .js), one extra cycle in the for is performed, **IF** the _output folder_ coincides with the _input folder_. A workaround would be to not output in the same folder or to output to a file with a different extension and renaming it afterwards. – Dr1Ku May 28 '13 at 08:28
  • how to add subfolder name two in output file? – user1586957 Sep 13 '17 at 11:15
  • @user1586957 - In the last code, insert `echo %%~dpF&` immediately before the `type "%%F"` – dbenham Sep 13 '17 at 11:25
  • it seems not working. output file is same as before. for instance `for /r "C:\Users\sri00571\Desktop\test_spats" %F in (*.csv) do echo %%~dpF& @type "%F" >>concatenated.csv` – user1586957 Sep 13 '17 at 11:35
  • 1
    I said to do that to the last code, not the one before it. The redirection needs to be applied to both the ECHO and the TYPE. – dbenham Sep 13 '17 at 11:53
1

I'm not aware of an approach to do that from a batch file, but you could use a tool like minify

http://code.google.com/p/minify/

to both combine JavaScript files and also minify them (e.g. by removing comments and unnecessary white space).

There are quite a few similar tools for a variety of programming environments.

Eric J.
  • 147,927
  • 63
  • 340
  • 553