Appending a txt file from multiple CSVs in subdirectories

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.

13.10.2009 19:38:12
3 ОТВЕТА
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 /?

0
13.10.2009 20:24:09
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.

0
13.10.2009 20:43:19

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)
2
13.10.2009 21:14:14
Thank you very much! I am nesting this batch file in a VB script, so all of this is very useful. cheers
user189379 14.10.2009 11:50:21
I tried the command as suggested with %%a and got errors. Turns out that in Windows 7, you don't use variables as %%a, it needs to be prefixed with a single % (so %a).
TMC 1.11.2011 18:13:57
Actually the double percent sign is needed when the command is in a .bat file. If you're just running this on the command prompt then you only need a single percent sign.
Torre Lasley 29.03.2012 12:39:38