Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to write a simple script for the windows command line that takes all files in the directory structure (files in the same folder as well as other folders in that directory), and concatenates them into a single file. In this case I only want to grab javascript (*.js) files.

This is what I have so far after viewing similar questions on this site.

@del pheonix.js
@FOR /R %%i in (*) do @type "%%i"\*.js >> pheonix.js

When I run it I get "The directory name is invalid." Any ideas on what's wrong?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Just use *.js in for loop and remove the \*.js from your type command.

Also I would recommend you to use different extension for the output file (at least temporarily) to avoid the for trying to append the file to itself.

@echo off
del pheonix.js
FOR /R %%i in (*.js) do type "%%i" >> pheonix.tmp
ren pheonix.tmp pheonix.js

Had you removed the @'s from your batch, you would easily see what's wrong yourself:

D:\temp>FOR /R %i in (*) do type "%i"\*.js  1>>pheonix.js

D:\temp>type "D:\temp\aaa.js"\*.js  1>>pheonix.js
The directory name is invalid.

D:\temp>type "D:\temp\bbbb.js"\*.js  1>>pheonix.js
The directory name is invalid.

D:\temp>type "D:\temp\subfolder\cccc.js"\*.js  1>>pheonix.js
The directory name is invalid.
share|improve this answer
2  
+1 for the 'had you removed' –  Jan Doggen Apr 30 '13 at 8:59
1  
If you want only *.js files, then use FOR /R %i in (*.js). Otherwise this command will take each and every file. –  Alexey Ivanov Apr 30 '13 at 15:39
    
@AlexeyIvanov Thanks. I haven't noticed. Updated the answer. –  Martin Prikryl Apr 30 '13 at 16:38
    
Brilliant. Thanks guys. –  John Apr 30 '13 at 23:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.