views:

76

answers:

3

Hi,

I am trying to write a batch file which will append all *.csv files in the immediate subdirectories to a single text file in the current directory.

From various sources I have managed to piece together this code which works fine for files in the current dir but not sub-dirs

for %%a in (*.csv) do (type %%a >> csvreport.txt)

If anybody could help me with this I would be extremely grateful as I have tried various approaches with wildcards but without success.

A: 
for /R .\ %%a in (*.csv) do (type %%a >> csvreport.txt)

The /R indicates recursive and the parameter afterward is the folder in which to start (.\ is the current directory).

You can find up more if you run for /?

Shay Erlichmen
A: 
dir /ad /b > dirs.txt
for /f "tokens=1*" %%i in (dirs.txt) do cd %%i & for %%b in (*.csv) do (type %%b >> c:\csvreport.txt) & cd ..

Using the /R flag will traverse all subdirectory trees. You can nest the 'for' statements to only work with the immediate subdirectories but not their subdirectories.

E_Cubed99
A: 

Yet another option...

for /f usebackq %%a in (`dir /s /b *.csv`) do (type %%a >> csvreport.txt)

EDIT: Reading your details a bit more ... you want just the immediate directories, you can do this:

for /f usebackq %%a in (`dir /b /ad`) do for %%b in ("%%a"\*.csv) do (type "%%b" >> csvreport.txt)
Chris J
Thank you very much! I am nesting this batch file in a VB script, so all of this is very useful.cheers
tom0112358