#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Reading from 1 Txt File, Searching another, and writing to a 3rd.


    I have the responsibility create a script to read a list file. At each line, of the 1st file, I need to search a 2nd file for a "no match" to each line in the first file (both files are list files). For each no match found, I need to write the line from the 1st file to a 3rd txt file. Below is what I have so far, and as you can see I am having some difficulty.

    I may even be on the right track, but I am fairly new to VBS. I have only had one class, and that was last year. Any help would be appreciated.



    Const FOR_READING = 1
    Const FOR_APPENDING = 8

    Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")

    Dim objNameList : Set objNameList = objFSO.OpenTextFile("C:\DATA\OLD-LIST.txt", FOR_READING, False)
    Dim objSearchFile : Set objSearchFile = objFSO.OpenTextFile("C:\DATA\NEW-LIST.txt", FOR_READING, False)
    Dim objResultsFile : Set objResultsFile = objFSO.OpenTextFile("C:\DATA\DELETE-LIST.txt", FOR_APPENDING, True)

    Dim objRegEx : Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.Global = True
    objRegEx.IgnoreCase = True


    Dim strSearchFile : strSearchFile = objSearchFile.ReadAll
    objSearchFile.Close
    Do Until objNameList.AtEndOfStream
    Dim strLine : strLine = objNameList.Readline
    objRegEx.Pattern = strLine
    Dim colMatches : Set colMatches = objRegEx.Execute(strSearchFile)
    objResultsFile.WriteLine strLine
    Loop
    objNameList.Close
    objResultsFile.Close
  2. #2
  3. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,449
    Rep Power
    4539
    What's your question? does your code give you errors?

    There are examples of reading text files with vbscript and the filesystem object in the fso documentation in the msdn library, I'd start with reviewing some samples. Also sites like www.w3schools.com have sample codes.
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0
    My problem is that I am getting results, but they are exactly the same as the 1st file (The list file). I need to know what in the list file does not match in the search file.

    The list file is like 55K, and the Search (Look up file) is like 500K. I need to know what in the list file is not in the search file, and have it write that line to the 3rd file (result).

    Not sure where I went wrong, but I think I am somewhat close, because I got a result. It just seems to write out the list file though. Can anyone see where I made my mistake, and even better, how do I fix it?
  6. #4
  7. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,449
    Rep Power
    4539
    One thing, don't Dim a variable inside a loop.
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0
    Doug - I moved the DIM Statements, and added a If-Then statement as well hoping for a better result:

    Const FOR_READING = 1
    Const FOR_APPENDING = 8

    Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")

    Dim objNameList : Set objNameList = objFSO.OpenTextFile("C:\DATA\ListFile.txt", FOR_READING, False)
    Dim objSearchFile : Set objSearchFile = objFSO.OpenTextFile("C:\DATA\SearchFile.txt", FOR_READING, False)
    Dim objResultsFile : Set objResultsFile = objFSO.OpenTextFile("C:\DATA\ResultList.txt", FOR_APPENDING, True)

    Dim objRegEx : Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.Global = True
    objRegEx.IgnoreCase = True

    Dim strLine
    Dim colMatches

    Dim strSearchFile : strSearchFile = objSearchFile.ReadAll
    objSearchFile.Close

    Do Until objNameList.AtEndOfStream
    strLine = objNameList.Readline
    objRegEx.Pattern = strLine
    Set colMatches = objRegEx.Execute(strSearchFile)
    If strLine <> strSearchFile then
    objResultsFile.WriteLine strLine
    Else
    End if
    Loop
    objNameList.Close
    objResultsFile.Close


    -----------------------------------------------------------

    the idea is to read the listfile which is a dump of a test domain user list (just account ID's), and compare it to a production domain (the search file) to build a list if accounts to delete from the test domain. The list file is 55K, and the Searchfile is about 20 times that size. So there should be a difference in size between the delete list, and the list file. But there isn't.

    Any ideas - I am fairly new to VB, but this is turning out to be a lot harder than I had thought.
  10. #6
  11. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,449
    Rep Power
    4539
    I don't do much with regex, maybe you have to reset a regex pointer for each iteration of the search? Just a wild guess, I'd probably be using a database rather than text files myself.

    You can use msgbox() as debug breakpoints to inspect variable contents. Just add a msgbox(myvariable) or whatever the syntax of msgbox is.
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0
    I did not read over your question/code in depth but if you need something to do multipal task as described in your title then you need to create seperate processes per "do" event. Their is a thread workhorse thingy in the VB toolbox. Sorry cant think of the technecail name brain fart.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0
    In your line: If strLine <> strSearchFile then
    Unless it matches the very first entry you will not match and it will be written out which is why you are getting the whole list.

    You want it to be: If strLine = strSearchFile then
    Also, if capitalization does not match perfectly it will be written out as well. I would upcase both before the equal test.
    If the search file is alphabetical then you can exit the search early with a second test of: If strLine > strSearchFile then (exit and write)

    When you come to the end of the search list (or exited early) then write out the strline to file #3.

    Code:
    Top:
    Do Until objNameList.AtEndOfStream
      strLine = objNameList.Readline
      objRegEx.Pattern = strLine
      Set colMatches = objRegEx.Execute(strSearchFile)
      If strLine = strSearchFile then goto Skip (or exit loop)
    rem if search list is alphabetical then use
      If strLine > strSearchFile then goto Skip (or exit loop)
    Loop 
    Skip:
    goto Top

IMN logo majestic logo threadwatch logo seochat tools logo