tomasz86

How to merge two text files?

238 posts in this topic

@jaclaz

Also , i am not sure the "echo %%B >>lines.txt" line will work for every kind of character the line might contains: i am thinking about "%", ">" or "&" for example but perhaps the .inf files won't ever contains one of them.

Edited by allen2
0

Share this post


Link to post
Share on other sites

@allen2

I don't know either, I just provided what IMHO is the simplest solution that fulfills the OP requirements and works with the examples posted.

It is not meant in any way as a competition of the type "my batch is better than yours", it's simply a way to exchange ideas.

@tomasz86

Try with:

@ECHO OFF 
FOR /F %%A IN ('dir /b .\TEMP2\*.ver') DO (
FOR /F "skip=1 tokens=*" %%B IN (.\TEMP2\%%A) DO (
ECHO .\TEMP2\%%A -- %%B
ECHO %%B >>.\TEMP2\update.txt
)
)
ECHO [SourceFileInfo] >.\HFMER\UPDATE\update.ver
SORT .\TEMP2\update.txt >>.\HFMER\UPDATE\update.ver
DEL .\TEMP2\update.txt
ECHO.
MORE .\HFMER\UPDATE\update.ver

Or use %~dp0:

@ECHO OFF
FOR /F %%A IN ('dir /b %~dp0*.ver') DO (
FOR /F "skip=1 tokens=*" %%B IN (%~dp0%%A) DO (
ECHO .%~dp0%%A -- %%B
ECHO %%B >>%~dp0update.txt
)
)
ECHO [SourceFileInfo] >%~dp0HFMER\UPDATE\update.ver
SORT .\TEMP2\update.txt >>%~dp0HFMER\UPDATE\update.ver
DEL %~dp0TEMP2\update.txt
ECHO.
MORE %~dp0HFMER\UPDATE\update.ver

If I get it right what you need/want.

%0 is the actual command line invoked i.e. the name of the batch or parameter 0 (zero).

%~dp0 is the same variable expanded to only drive and path (with a trailing backslash)

jaclaz

0

Share this post


Link to post
Share on other sites

It works :) Thank you.

FOR /F %%A IN ('dir /b .\TEMP2\*.ver') DO ( 
FOR /F "skip=1 tokens=*" %%B IN (.\TEMP2\%%A) DO (
ECHO .\TEMP2\%%A -- %%B
ECHO %%B >>.\TEMP2\update.txt
)
)
ECHO [SourceFileInfo] >.\HFMER\UPDATE\update.ver
SORT .\TEMP2\update.txt >>.\HFMER\UPDATE\update.ver
RD /Q/S TEMP2
ECHO.
MORE .\HFMER\UPDATE\update.ver

Generally, (as allen2 already pointed out) what I want to do here is to make a script for merging hotfixes(updates) for Windows 2000/XP/2003. It's code-name is HFMER (Hotfix Merger).

I asked about copying files and checking their file versions in an another thread but it seems to be too complicated to do now. Until I learn how to write such a script I'll stick to xcopy as it's the same way as HFSLIP uses when slipstreaming hotfixes. It may not be ideal but still such cases when the older file has newer version are quite rare so it's not such a big problem.

Anyway, by using this script I'm able to almost do everything to prepare the folders, files and the update.ver file containing the information from all of the merged hotfixes. Of course there will be duplicates in it but not that many so I can just remove them manually. Update.ver is not used by HFSLIP anyway.

The main problem lies in combining update.inf files. What I'm thinking about now is this. It's just an example. Let's say I have two update.inf files. After doing

copy update1.inf+update2.inf update3.inf

I get

[Version]

Signature = "$Windows NT$"

[SourceDisksFiles]

ipsecmon.exe=1

[Version]

Signature = "$Windows NT$"

[SourceDisksFiles]

remotesp.tsp=1

It would really help me if I could get from it this:

[Version]

Signature = "$Windows NT$"
Signature = "$Windows NT$"

[SourceDisksFiles]

ipsecmon.exe=1
remotesp.tsp=1

I don't care for duplicates now but is there any program that would do such a sorting automatically?

Edited by tomasz86
0

Share this post


Link to post
Share on other sites

By the way, I checked the Beyond Compare program recommended by dencorso but I found something else which, while quite similar to the above, is simpler and I find it more suitable for the task of merging the update.inf files (unless there is a tool that can do the task I mentioned in the previous post...).

http://kdiff3.sourceforge.net/

kdiff3.jpg

It's GPL. The other tool I had already known and have been using is WinMerge but WinMerge can compare only 2 files at once while Kdiff and Beyond Compare can compare 3 files. What I like the most about KDiff is its interface which is sooooo simple that comparing and merging files is very easy and can be done quicker then Beyond Compare for example (and without the need to memorise tens of keyboard shortuts ;)).

Edited by tomasz86
0

Share this post


Link to post
Share on other sites

What you require can be done in pure batch, and obviously with the use of third party utilities.

What happens if you have multiple files in which the content potentially overwrites content of another, how do you decide which line takes precedence when your script orders it? Are you sure that alphabetical will not mean overwriting something which should take precedence. If the file for instance works in linear form meaning line 20 will always be processed after line 19, what happens if line 21 only happens if line 19 is set to 0, but line 20 alphabetically changed that to 1 because line 22 onwards only worked if if the data was set to 1. I know its hard to explain but that's why it is generally easier to allow each individual file to run in sequence.

I could of course just provide a routine for you, (but then I could simply post it as my own project and take all the credit for it myself). It is after all close to what HFSLIP should have been, had its not become bloated with pointless additions and spoiled by poor/unmanageable scripting.

0

Share this post


Link to post
Share on other sites

There is a pre-made batch, here:

http://www.robvanderwoude.com/files/readini_nt.txt

to parse .ini files (very similar if not identical to .inf/.sif files) that can possibly be adapted to your uses.

Read also these:

http://reboot.pro/3960/

Also, there is a tool, fedit.exe , here:

that has the "once" switch that may come useful.

The "prepare.cmd" here contains some examples of the use of fedit:

jaclaz

0

Share this post


Link to post
Share on other sites
[Version]

Signature = "$Windows NT$"
Signature = "$Windows NT$"

[SourceDisksFiles]

ipsecmon.exe=1
remotesp.tsp=1

I don't care for duplicates now but is there any program that would do such a sorting automatically?

Even if dupes (duplicate lines) aren't you concern right now, you'll have to eliminate them, so, just for the record, the yanklines script I put together only removes exact dupes, no matter how many and where they are on the text file. It's fast and can handle really big files, because it was intended to be used with merged history logs.

0

Share this post


Link to post
Share on other sites

The comments by Yzöwl, and your own tests re version numbers of files rather than just dates, probably deserve a little more consideration. After all, depending on one's settings, sometimes just downloading or copying a file can change the date of the file - and then what?

Cheers and Regards

0

Share this post


Link to post
Share on other sites

Thank you for all your input. I really appreciate it :thumbup

1. As for file version checking, I know it would be the ultimate way to be sure that only newest files are copied but even now with xcopy it's not a big problem. I did a test and compared Win2k UR2 beta1 which I created 100% manually with the same hotfixes merged by this script. The results were identical. Dates may change when copying files but here they are taken from inside the hotfixes.

I'll show you some screen shots:

uEGSQ.png

2. Removing duplicates automatically may be tough. For example, let's say in the merged update.ver we have:

agentdpv.dll=1673840A5FDD52DB9A60D8B61AAB0FCE,0002000000000D62,53008  
agentdpv.dll=3D8249606BED3456A36A1F3069BAE2A3,0002000000000D60,53008

As file size is the same, determining the correct file would require md5 checking... but as such instances are not many it's probably easier to just check it manually and remove the unnecessary lines ;)

As for the update.inf the problem with duplicates is that the same entry is used many times, ex:

[System32.Files]

ipsecmon.exe,,,8
oakley.dll,,,8
polagent.dll,,,8
polstore.dll,,,8
rasmans.dll,,,8

[Cache.Files]

ipsecmon.exe,,,8
oakley.dll,,,8
polagent.dll,,,8
polstore.dll,,,8
rasmans.dll,,,8

Removing duplicates would require checking only files under each heading ([...]) separately. What I was saying about not caring about such duplicates meant that even if the final update.inf looks like this:

[System32.Files]

ipsecmon.exe,,,8
ipsecmon.exe,,,8
ipsecmon.exe,,,8
polagent.dll,,,8
polagent.dll,,,8

[Cache.Files]

ipsecmon.exe,,,8
ipsecmon.exe,,,8
ipsecmon.exe,,,8
polagent.dll,,,8
polagent.dll,,,8

I could just use Notepad++ and sort each list manually which is quite easy to be done and would also remove the duplicates automatically.

Also, what about something like this?

sp3res.dll
sp3res.dll,,,8

3. Anyway, I'll have a look at all these scripts and try to use them. Thanks once again for all your help B)

Edited by tomasz86
0

Share this post


Link to post
Share on other sites

Dates can change easily. PE Timestamps not so. Since, in Win 2k, most executables are PE executables, PE Timestamps and version numbers should be enough to decide most ambiguities. I posted more about that in older posts, and below are poiters to them.

But, when push comes to shove, you can use the PE Timestamp (see this, this, this and this) to sort things out, in many occasions (provided the files in question *are* PE executables (EXE, SYS, OCX, DLL, TLB usually)... although, in some cases it just adds to the confusion. There's nothing equivalent for NE, LE and plain DOS executables, I'm sorry to say.

Also of interest may be this old post, by Petr, on versioning.

HTH

0

Share this post


Link to post
Share on other sites

I haven't had time yet to check all this information. For now I've just sorted and tidied the script up. I want to keep is as simple as possible.


@ECHO OFF
COLOR 1F
IF EXIST HF\*.EXE (
IF EXIST HFMER RD/Q/S HFMER
IF EXIST TEMP RD/Q/S TEMP
MD HFMER TEMP
DIR/B/A-D/OGN/ON HF\*.EXE>TEMP\HF.TXT
SET HF=
FOR /F %%I IN (TEMP\HF.TXT) DO (SET HF=%%I&IF DEFINED HF CALL :HFEXTRACT)
DEL/Q/S TEMP\HF.TXT
)
IF NOT EXIST HF\*.EXE (
ECHO HF folder is empty.
ECHO.
PAUSE
EXIT
)

:HFEXTRACT
ECHO Unpacking %HF%
MD TEMP\HF&START/WAIT HF\%HF% /Q /X:TEMP\HF
ECHO.
XCOPY/DEHRY TEMP\HF HFMER
IF EXIST TEMP\HF\UPDATE\update.inf MOVE TEMP\HF\UPDATE\update.inf TEMP\%HF%.inf
IF EXIST TEMP\HF\UPDATE\update_w2k.inf MOVE TEMP\HF\UPDATE\update_w2k.inf TEMP\%HF%.inf
MOVE TEMP\HF\UPDATE\update.ver TEMP\%HF%.ver
RD/Q/S TEMP\HF
ECHO.

IF NOT EXIST TEMP\HF.TXT (
FOR /F %%A IN ('dir /b TEMP\*.ver') DO (
FOR /F "skip=1 tokens=*" %%B IN (TEMP\%%A) DO (
ECHO Merging update.ver...
ECHO TEMP\%%A -- %%B
ECHO %%B >>TEMP\update.txt
)
)
ECHO [SourceFileInfo] >HFMER\UPDATE\update.ver
SORT TEMP\update.txt >>HFMER\UPDATE\update.ver
DEL/Q/S HFMER\UPDATE\update.inf HFMER\createcab.cmd
COPY TEMP\*.inf HFMER\UPDATE
RD/Q/S TEMP
)

Edited by tomasz86
0

Share this post


Link to post
Share on other sites

To get the version of a particular file within a batch file is actually quite easy using an embedded VB script like so:

...

IF NOT EXIST TMP\FILEVER.VBS (CALL :sub_FileVerScript)

FOR /F %%G IN ('CSCRIPT //NOLOGO TMP\FILEVER.VBS FileYouWantToKnowTheVersionOf') DO (

...

At this point "%%G" will be the version of the file in question so do whatever you need to do with it

...

)

...

:: This is the subroutine that creates the VB script to get the version info

:sub_FileVerScript

>>TMP\FILEVER.VBS (

ECHO Option Explicit

ECHO Dim FSystem

ECHO Set FSystem = CreateObject^("Scripting.FileSystemObject"^)

ECHO WScript.Echo FSystem.GetFileVersion^(WScript.Arguments^(0^)^)

ECHO Set FSystem = Nothing

)

EXIT /B

...

(Note: Escaping the left-hand parenthesis "^(" is not required, but I usually do it anyway to be symmetrical. :) )

Hope this is useful.

Cheers and Regards

0

Share this post


Link to post
Share on other sites

Well... The official MS way to get an MD-5 hash is FCIV and to get File Versions it's FILEVER, findable inside this package. The're both console command-line tools, useful also in batch files.

0

Share this post


Link to post
Share on other sites

To get the version of a particular file within a batch file is actually quite easy using an embedded VB script like so:

I get this when trying to run the script

qRxHW.png

My system is Windows 2000.

Edited by tomasz86
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.