Batch file to merge 2 text files?

Soldato
Joined
15 Aug 2011
Posts
4,955
Hey guys


Quick and should be an easy question hopefully.

Two text files, both with the same text format in them!

If I do this,

Copy text1.txt+text2.txt text3.txt then that should merge them together..

Obviously have the paths for the text files in place.

Now what I would want to do is, then delete the original files and copy the newly made file back over to the original file paths.

Is it also possible to check for double entries against each file?

Basically it is for two ban files for two different servers on the same box.

Thanks

Will be using firedaemon to run the batch once or twice a a day.
 
You could try something like this....

Code:
@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION

SET FILE1="c:\batch\banfile1.txt"
SET FILE2="c:\batch\banfile2.txt"
SET SearchString=

FOR /F "usebackq delims=" %%A IN (%FILE1%) DO (
	SET SearchString=%%A
	SET SearchString=!SearchString:"=\"!
	FINDSTR /I /C:"!SearchString!" %FILE2%
	IF ERRORLEVEL 1 ECHO %%A>>%FILE2%
)

SET SearchString=

FOR /F "usebackq delims=" %%B IN (%FILE2%) DO (
	SET SearchString=%%B
	SET SearchString=!SearchString:"=\"!
	FINDSTR /I /C:"!SearchString!" %FILE1%
	IF ERRORLEVEL 1 ECHO %%B>>%FILE1%
)

ENDLOCAL

I've not fully tested it and it doesn't do any error checking or anything like that, so copy your ban files to a different location and test it offline first. But it's basically based around a simple batch file I did to parse a text file and pipe out non duplicated lines to a second file.

So all it is doing is...

Reads each line from %FILE1%
Tries to find it in %FILE2%
If not found in %FILE2%, pipe line to the end of %FILE2%

The bit where is sets the SearchString is so that we can escape any " that may be in the line we pull from the file. Depending on the contents of the files you are working with you may need to play about with escaping other characters as well.

And the the second FOR loop just does the same but with the files the other way around. Possibly not the most efficient way of doing it but should get pretty close to what you want.

In fact thinking about it again, you could copy FILE1 to FILE3 then look for lines in FILE2 to add to FILE3 and then copy FILE3 back over FILE1 and FILE2. Which may be quicker. And further to that powershell may be a better option.

Anyway, have a look/play/think about the above and let me know if you have any questions.

Two main issues I can see with this are that if the files are locked/in use at the time it may not be able to amend them and also the lines in each file won't be in the same order, so if the order the lines are in the file is relevant this may cause a problem.

(disclaimer: this has been done in the fug of a Sunday morning hang over)
 
A quick'n'dirty powershell example....

Code:
$banfile1 = Get-Content .\banfile1.txt
$banfile2 = Get-Content .\banfile2.txt

$banfile3 = $banfile1 + $banfile2

$banfile3 = $banfile3 | Sort-Object -Unique

$banfile3 | Out-File -FilePath '.\banfile1.txt' -Force
$banfile3 | Out-File -FilePath '.\banfile2.txt' -Force

Certainly looks to be a better way of doing it and should be a lot quicker.

As above though, please do your own testing before putting this live.
 
Hey mate

Thanks for replying, and sorry for taking so long to get back to you!

The second powershell example looks great, unfortunately I couldn't get it to work!

Left with a 1Kb txt file empty after using it!

Any ideas?

Thanks Again
 
Yup, it's just 2 commands, just dump it in a .bat and run it, obviously you'll have to pad it out with cd'ing to a place where the files are etc and then copying it to wherever, but those 2 lines will just type the contents of 1.txt and then write to a text file, then append to the text file with the second one.
 
Hey mate

Thanks for replying, and sorry for taking so long to get back to you!

The second powershell example looks great, unfortunately I couldn't get it to work!

Left with a 1Kb txt file empty after using it!

Any ideas?

Thanks Again

Odd, it seemed to work for me though I only tested it quickly with a couple of random files I had. Does it give any kind of error?

Can you upload the files somewhere or send me an example to test with? Or are these standard Punkbuster ban files, as I might be able to get one from somewhere.
 
type 1.txt > 3.txt
type 2.txt >> 3.txt
?

This isn't really going to cover what he is asking for though. It won't check for duplicates and after a while the files are going to get huge and the number of dupe entries is going to keep increasing as he then wants to replace 1.txt and 2.txt with 3.txt, so when the job runs again they'll just keep doubling up.


Just getting ready for work and can't access the forum whilst there, so will try and take a another look later.
 
Back
Top Bottom