Merging text files using batch file

Microsoft Microsoft windows 2000 scripti...
August 29, 2009 at 12:22:49
Specs: Windows 7, Q6600 4Gb RAM
Hi.

I have three folders, two with text files in.

Folder1

text1.txt
text2.txt
text3.txt

Folder2

text1.txt
text2.txt
text3.txt

Folder3
Empty (to copy merged files to)

Items in Folder2 contain the most recent data
Items in Folder1 contain the new data

I want to merge the files from Folder1 and
Folder2 together and output the same file
names of text1.txt text2.txt and text3.txt to
Folder3

So far I have this, but it doesn't work correctly
and I need a hand with the syntax / coding if
possible:

set MrgFiles=C:\test\Folder3
for %%F in ("C:\test\Folder1\*.txt") do copy
%%F +"C:\test\Folder2\*.txt" MrgFiles

Basically I've cut out all the @echo and pause commands, and some of the other set vars I did which didn't work. I could go through and setup each file hardcoded, but there are lots of them, so want to be able to say;

for every .txt file in <location>, merge it with this <location> with files of the same name, and output the result <here>

Does that make sense? I'm new to coding and I think I at least understand what I'm after, but cannot fathom the syntax for it.

Regards and many thanks

Alex


See More: Merging text files using batch file

Report •


#1
August 29, 2009 at 13:28:14
I think I want to do something similar to this (this is just an example I've stumbled across):


@echo off
setlocal
set RootFolder=C:\Documents and Settings\
set OutputFile=C:\Output.txt

for /f "delims=" %%a in ('dir "%RootFolder%*.txt" /b /s /a') do
type "%%a" >>%OutputFile%


But I want the OutputFile variable to effectively be able to hold
the values for each text file, not just a single text file... make
any sense?

Cheers


Report •

#2
August 29, 2009 at 15:53:09
Hi Hawkinsa21

Is this what you mean

@echo off
for /f "tokens=* delims=" %%a in ('dir /b Folder1\*.txt') do (
copy Folder1\%%a+Folder2\%%a% Folder3\ > nul
)


Report •

#3
August 29, 2009 at 17:03:39
I tested this code, it worked.
I inserted spaces in filenames and folder names to test eventual unexpected behaviours..

Any identical files in folder 1 and folder 2 will be merged.
You dont have to enumerate them (file1, file2 ...)

@echo off
setlocal enabledelayedexpansion
set fd1=c:\test\folder 1
set fd2=c:\test\folder 2
set MrgFiles=C:\test\folder 3
pushd "%fd1%"
for /f "tokens=1* delims=" %%a in ('dir /b /a-d "!fd1!"') do (
if exist "!fd2!\%%a" (
type "%%~fa">"!MrgFiles!\%%a"
echo.>>"!MrgFiles!\%%a"
type "!fd2!\%%a">>"!MrgFiles!\%%a"
)
)
popd


Report •

Related Solutions

#4
August 30, 2009 at 01:41:43
dtech10, thats great, exactly what I was looking for! I need to
learn more about batch file coding!

ricardo647, that script is fantastic, I really appreciate you helping out with that! I tweaked it to suit my needs, and you can see that it also does some copies and deletes as well...

setlocal enabledelayedexpansion
set fd1=c:\test\Folder1
set fd2=c:\test\Folder2
set MrgFiles=C:\test\Folder3
@echo Backing up Folder2 to temp folder
Pause
copy "c:\test\Folder2\*.txt" c:\test\temp
pushd "%fd1%"
for /f "tokens=1* delims=" %%a in ('dir /b /a-d "!fd1!"') do (
if exist "!fd2!\%%a" (
type "%%~fa">"!MrgFiles!\%%a"
echo.>>"!MrgFiles!\%%a"
type "!fd2!\%%a">>"!MrgFiles!\%%a"
)
)
popd
del "C:\test\Folder2\*.txt"
copy "C:\test\Folder3\*.txt" "c:\test\Folder2"
del "C:\test\Folder3\*.txt"
copy c:\test\temp\*.txt c:\test\temp\backups
del c:\test\temp\*.txt
@echo finished!
Pause


I have tested this though and it takes the files from Folder1 and Folder2 and indeed merges them into Folder3, but if the new data in Folder2 doesn't contain as many text files as Folder1, it overwrites the existing text files...

e.g.

Folder1 has text1 text2 and text3
Folder2 has text1 but with more up to date data
When running the batch file it merges the two text1 files together correctly, but the merged folder output (Folder3) only shows the text1 file... text2 and text3 have been deleted.

Can you advise if I can put a copy and replace switch in the batch code rather than a complete overwrite? That way I can keep the existing files even if there is no corresponding new files for files of the same name??

(Thats a mouth full! Sorry)

regards

Alex


Report •

#5
August 30, 2009 at 10:30:51
Hawkins, the bat as it is will merge only files that are present both in folder1 and 2. It was what I thought it should do.

But if you need to copy also files without correspondance in one or other folder, you may insert this lines after the first pause in the batch:

copy /y "%fd1%"\*.txt "%MrgFiles%"
copy /y "%fd2%"\*.txt "%MrgFiles%"

All files will be copied to folder3 at first, and in a second step files that are present in both folders will be overwritten with their merged version.

Try it and let us know if you succeed.


Report •

#6
August 31, 2009 at 01:42:02
You are a legend. It has worked a treat. I can't thank you enough
for your help on this Ricardo, its been a massive help to me.
Many many thanks!

Alex


Report •


Ask Question