Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
zimxavier
- Posts: 53
- Joined: 17 Jan 2016 10:09
- Location: France
#1
Post
by zimxavier » 11 Nov 2016 09:16
Hello,
I have two files :
File1.txtcontent1 content1 content1 content1 content1 content1
content1 content1 content1 content1 content1 content1
File2.txtcontent2 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.txtFile1.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
#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
#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
#4
Post
by Squashman » 11 Nov 2016 10:19
That is because the last line of file 1 does not have a CRLF.
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#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