Computing.Net > Forums > Programming > Compare and update 2 text files

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.

Compare and update 2 text files

Reply to Message Icon

Name: dougchiu
Date: December 19, 2007 at 19:23:01 Pacific
OS: W2K server
CPU/Ram: 4
Product: C
Comment:

I have 2 files
one is:
username date
A 2007-12-13
B 2007-12-18
C 2007-12-12

another is:
username
A
B
C
D
E
F

I want to compare these 2 files using ms-dos batch and give the result as below.

username date
A 2007-12-13
B 2007-12-18
C 2007-12-12
D
E
F

I got stuck to figure out the solution. Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: December 19, 2007 at 23:30:48 Pacific
Reply:

here's a vbscript
[code]
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set d = CreateObject("Scripting.Dictionary")
strFile1 = "c:\test\1.txt"
strFile2 = "c:\test\2.txt"
Set objFile1 = objFSO.OpenTextFile(strFile1,1)
Do Until objFile1.AtEndOfLine
strLine = objFile1.ReadLine
strArray1 = Split(strLine)
d.Add strArray1(0), strLine
Loop
Set objFile1 = Nothing
Set objFile2 = objFSO.OpenTextFile(strFile2,1)
Do Until objFile2.AtEndOfLine
strLine = objFile2.ReadLine
If d.Exists(strLine) Then
WScript.Echo d.Item(strLine)
Else
WScript.Echo strLine
End If
Loop
Set objFile2 = Nothing
Set objFSO = Nothing
[/code]


0

Response Number 2
Name: dougchiu
Date: December 20, 2007 at 01:51:11 Pacific
Reply:

Thanks...
but i want to output the result as textfile also.
the code above give me a popup window for each line.


0

Response Number 3
Name: klint
Date: December 20, 2007 at 02:32:40 Pacific
Reply:

There was a similar question answered here about a couple of weeks ago. I can't remember the subject title, but I'm sure you'll find it if you search.

By the way, if you try to use ghostdog's suggestion you should run it with Cscript, not Wscript. CScript outputs lines to the console, and can be redirected to a file. Wscript, which you were using, outputs lines in a pop-up message box.


0

Response Number 4
Name: dougchiu
Date: December 26, 2007 at 20:10:59 Pacific
Reply:

I got the output file, but I cannot get the 2nd column data in file 1. It only give me the same data as file 2.

Thank you~!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


VBS runs prog,display msg... Saving the image in MS-Ac...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Compare and update 2 text files

how to merge 2 txt files using batch script www.computing.net/answers/programming/how-to-merge-2-txt-files-using-batch-script-/19715.html

splitting a text file www.computing.net/answers/programming/splitting-a-text-file/14804.html

manipulating text files www.computing.net/answers/programming/manipulating-text-files/13115.html