Append string into a text file variation

November 18, 2011 at 20:14:44
Specs: Windows 7
I need to create one text file with contents of multiple text files, but first insert the name of the file before the content of each file, then skip a line.
example:
a.txt has content .. abcd
b.txt has content .. 1234
c.txt has content .. bird
d.txt has content .. driving fast

new.txt should have:
a
abcd

b
1234

c
bird

d
driving fast

Looking for a batch file that would achieve this for an entire directory.

I can successfully create one file with the content of all files with a simple copy command, but do not know how to insert the file name before the content of each file.



See More: Append string into a text file variation

Report •


#1
November 18, 2011 at 20:49:56
Maybe type can help:

type *.txt > "newfile.txt" 2>&1

But it will add the file extension.


Report •

#2
November 18, 2011 at 21:12:08
Sorry if I come off as a complete newbie, but I just discovered what 'command prompts' and working on batch files were just today..

I put in this following code in a batch file that I created. A 'newfile' was created but the file was empty.

echo.>>*.txt > "newfile.txt" 2>&1

I found this code somewhere on this site, but it slightly different, in that it doesn't copy over the files' name or skip a line but instead puts a dollar sign.

@echo off > output.new
pushd %*
for %%j in (*.txt) do (
type "%%j"
echo.$
) >> output.new
ren output.new output.txt
popd


Report •

#3
November 18, 2011 at 21:50:55
echo.>> huh?

What I posted was just a single line that can be used as is.

Sure you could use the other approach:

for %%j in (*.txt) do (
type "%%j"
echo %%~nj
echo.
) >> output.new


Report •

Related Solutions

#4
November 18, 2011 at 22:01:41
Thank you for your response. The new code works great, except one little thing.

What it should have been:
a
abcd

b
1234

c
bird

d
driving fast

Result of the new code:
aabcd

b1234

cbird

ddriving fast

aabcd

b1234

cbird

ddriving fast

output


Thank you again for helping me out!


Report •

#5
November 18, 2011 at 22:31:34
That means the files don't end in line breaks, instead we can just add them in(the type version can't be fixed):

for %%j in (*.txt) do (
type "%%j"
echo.
echo %%~nj
echo.
) >> output.new


Report •

#6
November 19, 2011 at 08:36:58
Thank you, Judago!

It works perfectly, except for one minor thing. For some reason, the content of the text files are showing up first, then the file name appears beneath it as show below.


abcd
a

1234
b

bird
c

driving fast
d


Report •

#7
November 19, 2011 at 17:10:00
I can't believe that I missed that!

If you look at the script the statements are out of order ;).

for %%j in (*.txt) do (
echo %%~nj
echo.
type "%%j"
echo.
echo.
) >> output.new


Report •

#8
November 19, 2011 at 20:30:11
Thanks, Judago! You're the best!

Report •

#9
November 24, 2011 at 03:38:55
could you, pls, post the complete code? I have similar problem.
Thanks.

Report •


Ask Question