Need help with a VBScript comparing files...

Author Message
madspire

  • Total Posts : 3
  • Scores: 0
  • Reward points : 0
  • Joined: 1/18/2012
  • Status: offline
Need help with a VBScript comparing files... Wednesday, January 18, 2012 9:23 AM (permalink)
0
What I’m trying to do is a write a script to compare sections from two text files, and from that comparison create a third text file listing the differences. The first file is a constant text file I keep with the script. I want the script to prompt for the second file – I’m  using  Set ObjFSO = CreateObject("UserAccounts.CommonDialog") to accomplish that.
 
The files each have multiple sections – sort of like this:
 
[Application]
Application1=ApplicationABC,Version1.234,Local Install,-,-,Y,-,Y,-,SDLIB,-,-,-,-
Application2=ApplicationXYZ,Version15,Local Install,-,-,Y,-,N,-,SDLIB,-,-,-,-
 
[InstalledApplication]
NextInstalledApplication=
 
[MachineType]
MachineType2=Laptop,-,-,-,Laptop
 
I only need to compare the [Application] sections of each file, and only care about the Application Name and Version (in bold above) – everything else is irrelevant.
 
The problem comes in two places
  1. The sections I want to compare are not necessarily in the same order within the files.
  2. The application lines themselves vary somewhat – For example, MS Office could be Application25 in one file, or Application30 in another. Some of the info following the name and version can also vary so I can’t just compare line by line.
 
This is what I have so far – very rough – after working out the details, I also want to look into picking multiple files, where the script compares, and creates a differences document for each. Also, getting the differences document to use the name of the original user-chose document would be nice.
 
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("C:\AmFam\Test\AOEPC.txt", ForReading)
Set Obj2FSO = CreateObject("UserAccounts.CommonDialog")
 Obj2FSO.Filter = "VBScripts|*.vbs|Text Files|*.txt|All Files|*.*"
 Obj2FSO.FilterIndex = 3
 Obj2FSO.InitialDir = "\\Sofenterpriseserver.amfam.com\Sofstage$\Prod\Generic\"

'Read first file
strApplications = objFile1.ReadAll
objFile1.Close

'Prompt for second file
InitFSO = Obj2FSO.ShowOpen
 If InitFSO = False Then
 Wscript.Echo "Script Error: Please select a file!"
 Wscript.Quit
 Else
 WScript.Echo "You selected the file: " & Obj2FSO.FileName
 End If
 
'Read second file
Set objFile2 = objFSO.OpenTextFile(Obj2FSO.FileName, ForReading)

'Compare files
Do Until objFile2.AtEndOfStream
 strCurrent = objFile2.ReadLine
 If InStr(strApplications, strCurrent) = 0 Then
 strDifference = strDifference & strCurrent & vbCrLf
 End If
Loop
objFile2.Close

'Display differences
Wscript.Echo "Differences: " & vbCrLf & strDifference
'Save differences to a text file
Set objFile3 = objFSO.CreateTextFile("C:\AmFam\Test\Differences.txt")
 objFile3.WriteLine strDifference
 objFile3.Close 

 
#1
    madspire

    • Total Posts : 3
    • Scores: 0
    • Reward points : 0
    • Joined: 1/18/2012
    • Status: offline
    Re:Need help with a VBScript comparing files... Wednesday, January 18, 2012 9:25 AM (permalink)
    0
    Sorry about all the green in the script text above! Any help with any part of the script will be much appreciated.
     
    #2
      ebgreen

      • Total Posts : 8227
      • Scores: 98
      • Reward points : 0
      • Joined: 7/12/2005
      • Status: offline
      Re:Need help with a VBScript comparing files... Thursday, January 19, 2012 3:00 AM (permalink)
      0
      so you just need to compare the application name and the version?
      "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
      Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
      http://www.visualbasicscript.com/m_47117/tm.htm
       
      #3
        madspire

        • Total Posts : 3
        • Scores: 0
        • Reward points : 0
        • Joined: 1/18/2012
        • Status: offline
        Re:Need help with a VBScript comparing files... Thursday, January 19, 2012 3:29 AM (permalink)
        0
        Yes - all the other information in the text files, even on the same line as the application info, is irrelevant.
         
         
        Basically - the one static file will contain a list of core applications and their versions. The script should compare the file chosen by the user to the core list, and write any differences to the output file.
        <message edited by madspire on Thursday, January 19, 2012 3:32 AM>
         
        #4
          ebgreen

          • Total Posts : 8227
          • Scores: 98
          • Reward points : 0
          • Joined: 7/12/2005
          • Status: offline
          Re:Need help with a VBScript comparing files... Thursday, January 19, 2012 3:40 AM (permalink)
          0
          So here is the basic logic of the way that I would do it personally.
           
          go through each line of file1 looking for [Application]
          process each line until I find a line that starts with [ (new section)
          for each of these lines Split on the = sign then split on ,
          Use the first and second parts of the array from the split to populate a dictionary (dic1) where the keys are application names in upper case and the values are the version number
           
          Do the same thing for file2 to generate dic2
          Go through each key in dic1 and see if that key exists in dic2. If it does, compare the keys. If it does not then you have found one that exists one place but not the other. Then check the keys in dic2 for keys that do not exist in dic1.
          "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
          Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
          http://www.visualbasicscript.com/m_47117/tm.htm
           
          #5
            59cobalt

            • Total Posts : 977
            • Scores: 91
            • Reward points : 0
            • Joined: 7/17/2011
            • Status: offline
            Re:Need help with a VBScript comparing files... Thursday, January 19, 2012 11:24 AM (permalink)
            0
            To simplify what ebgreen laid out you can use the INI parser I wrote some time ago.
             
            #6
              Hackoo

              • Total Posts : 105
              • Scores: 4
              • Reward points : 0
              • Joined: 6/25/2010
              • Status: offline
              Re:Need help with a VBScript comparing files... Friday, January 20, 2012 1:12 AM (permalink)
              0
              Hi Take a look at those codes :
              File2Compare.vbs and File2Compare.hta
               
              #7

                Online Bookmarks Sharing: Share/Bookmark

                Jump to:

                Current active users

                There are 0 members and 1 guests.

                Icon Legend and Permission

                • New Messages
                • No New Messages
                • Hot Topic w/ New Messages
                • Hot Topic w/o New Messages
                • Locked w/ New Messages
                • Locked w/o New Messages
                • Read Message
                • Post New Thread
                • Reply to message
                • Post New Poll
                • Submit Vote
                • Post reward post
                • Delete my own posts
                • Delete my own threads
                • Rate post

                2000-2012 ASPPlayground.NET Forum Version 3.9