merge files and write the original filename before the content (batch) [resolved]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
zimxavier
Posts: 53
Joined: 17 Jan 2016 10:09
Location: France

merge files and write the original filename before the content (batch) [resolved]

#1 Post by zimxavier » 11 Nov 2016 09:16

Hello,

I have two files :

File1.txt
content1 content1 content1 content1 content1 content1
content1 content1 content1 content1 content1 content1

File2.txt
content2 content2 content2 content2 content2 content2
content2 content2 content2 content2 content2 content2

I need to merge them and have the original filename before the content :

Output.txt
File1.txt
content1 content1 content1 content1 content1 content1
content1 content1 content1 content1 content1 content1
File2.txt
content2 content2 content2 content2 content2 content2
content2 content2 content2 content2 content2 content2

I think there is an easy way to do that in batch language, I don't remember how. Could you help me ?

Thank you !
Last edited by zimxavier on 11 Nov 2016 10:24, edited 3 times in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: merge files and write the original filename before the content (batch)

#2 Post by Squashman » 11 Nov 2016 09:18

You could do this multiple ways.

Code: Select all

@echo off

(FOR %%G IN ("file1.txt" "file2.txt") DO (
   echo %%~G
   type "%%~G")
)>output.txt

(echo file1.txt
type file1.txt
echo file2.txt
type file2.txt
)>output2.txt

echo file1.txt>output3.txt
type file1.txt>>output3.txt
echo file2.txt>>output3.txt
type file2.txt>>output3.txt

(FOR %%G IN (File*.txt) DO (
   echo %%G
   type "%%~G")
)>Output4.tmp
rename Output4.tmp Output4.txt

zimxavier
Posts: 53
Joined: 17 Jan 2016 10:09
Location: France

Re: merge files and write the original filename before the content (batch)

#3 Post by zimxavier » 11 Nov 2016 10:10

Thank you Squashman,

Your fourth example is interesting in my case, because of the token (I have many more files than two and I don't want write their names each time)
All is okay.

Batch used :

@echo off

(FOR %%G IN (text*.txt) DO (
echo %%G
type "%%~G")
)>Output.tmp
rename Output.tmp Output.txt

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: merge files and write the original filename before the content (batch)

#4 Post by Squashman » 11 Nov 2016 10:19

That is because the last line of file 1 does not have a CRLF.

zimxavier
Posts: 53
Joined: 17 Jan 2016 10:09
Location: France

Re: merge files and write the original filename before the content (batch)

#5 Post by zimxavier » 11 Nov 2016 10:20

Indeed. I edited my post.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: merge files and write the original filename before the content (batch)

#6 Post by Squashman » 11 Nov 2016 10:25

So if one of your files does not have a CRLF on the last line you can pipe the output to FINDSTR before the redirect.

Code: Select all

(FOR %%G IN (File*.txt) DO (
   echo %%G
   type "%%~G"|findstr "^")
)>Output4.tmp
move /Y Output4.tmp Output4.txt

Post Reply