Computing.Net > Forums > Programming > Merging text files using batch file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Merging text files using batch file

Reply to Message Icon

Name: hawkinsa21
Date: August 29, 2009 at 12:22:49 Pacific
OS: Windows 7
CPU/Ram: Q6600 4Gb RAM
Product: Microsoft Microsoft windows 2000 scripting guide
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: hawkinsa21
Date: August 29, 2009 at 13:28:14 Pacific
Reply:

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


0

Response Number 2
Name: dtech10
Date: August 29, 2009 at 15:53:09 Pacific
Reply:

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
)


0

Response Number 3
Name: ricardo647
Date: August 29, 2009 at 17:03:39 Pacific
Reply:

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


0

Response Number 4
Name: hawkinsa21
Date: August 30, 2009 at 01:41:43 Pacific
Reply:

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


0

Response Number 5
Name: ricardo647
Date: August 30, 2009 at 10:30:51 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: hawkinsa21
Date: August 31, 2009 at 01:42:02 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Merging text files using batch file

Delete text range using batch file www.computing.net/answers/programming/delete-text-range-using-batch-file/17753.html

editing text file with batch file www.computing.net/answers/programming/editing-text-file-with-batch-file/15231.html

Find / Replace text in a batch file www.computing.net/answers/programming/find-replace-text-in-a-batch-file/15715.html