Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows batch file - Concatenate all files in subdirectories

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!

like image 793
Chris Dutrow Avatar asked Jul 29 '12 17:07

Chris Dutrow


1 Answers

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")
like image 181
dbenham Avatar answered Oct 08 '22 07:10

dbenham