Comparing files in arrays, deleting lines from text file, functions, logging

后端 未结 2 355
[愿得一人]
[愿得一人] 2021-01-26 04:30

So I created these two arrays Approved shares and current shares.

\'Reads Approvedshare txt and makes the txt file into an array
public objFSO 
set objFSO = Cre         


        
相关标签:
2条回答
  • 2021-01-26 04:58

    You want to check if any of the current shares is in the list of approved shares, so you need to switch your inner and outer loop:

    For Each CurrentShareName In arrLines1
      found = False
      For Each ApprovedShareName In arrLines 
        If CurrentShareName = ApprovedShareName Then
          found = True
          Exit For
        End If
      Next
      If Not found Then
        'delete share
        'log share name
      End If 
    Next
    

    VBScript built-in arrays aren't objects, so you can't use something like

    If arrLines.Contains(CurrentShareName) then
      ...
    End If
    

    You could use an ArrayList object, though:

    Set objFSO = CreateObject("Scripting.FileSystemObject") 
    
    Set approvedShares = CreateObject("System.Collections.ArrayList")
    
    Set objTextFile = objFSO.OpenTextFile("C:\Users\...\approvedshares.txt")
    Do Until objTextFile.AtEndOfStream
      approvedShares.Add objTextFile.ReadLine
    Loop
    objTextFile.Close
    
    Set objTextFile = objFSO.OpenTextFile("C:\Users\...\currentshares.txt")
    Do Until objTextFile.AtEndOfStream
      share = objTextFile.ReadLine
      If Not approvedShares.Contains(share) Then
        'delete share
        'log share name
      End If
    Loop
    objTextFile.Close
    
    0 讨论(0)
  • 2021-01-26 04:59

    So your problem is to get the relative complement of the set 'approved' in the set 'current': the set of those elements that are in 'current', but not in 'approved'.

    The script:

    Option Explicit
    
    Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
    ExecuteGlobal goFS.OpenTextFile( ".\SetLib.vbs" ).ReadAll
    
    Function Fi2Ar(sFSpec)
      Dim aTmp : aTmp = Split(goFS.OpenTextFile(sFSpec).ReadAll(), vbCrLf)
      Dim nLst : nLst = -1
      Dim i
      For i = 0 To UBound(aTmp)
          If Trim(aTmp(i)) <> "" Then
             nLst = i
             aTmp(i) = Trim(aTmp(i))
             aTmp(i) = "'" & aTmp(i) & "'" ' just for display
          End If
      Next
      ReDim Preserve aTmp(nLst)
      Fi2Ar = aTmp
    End Function
    
    Dim aA   : aA   = Fi2Ar("..\data\s1.txt")
    Dim aB   : aB   = Fi2Ar("..\data\s2.txt")
    Dim aRes : aRes = diffArray( aA, aB )
    WScript.Echo "A  : [", Join( aA ), "]"
    WScript.Echo "B  : [", Join( aB ), "]"
    WScript.Echo "UNI: [", Join( aRes( 0 ) ), "]", "in A or B"
    WScript.Echo "INT: [", Join( aRes( 1 ) ), "]", "in A and B"
    WScript.Echo "A\B: [", Join( aRes( 2 ) ), "]", "in A but not in B"
    WScript.Echo "B\A: [", Join( aRes( 3 ) ), "]", "in B but not in A"
    goFS.CreateTextFile("..\data\s3.txt").WriteLine Join(aRes(3), vbCrLf)
    WScript.Echo "Bad Shares File:"
    WScript.Echo goFS.OpenTextFile("..\data\s3.txt").ReadAll()
    

    output:

    A  : [ 'Test' 'test123' 'test1234' 'flexare' 'this' 'is' 'a' 'example' ]
    B  : [ 'Test' 'test123' 'added 1' 'added2' 'test1234' 'flexare' 'added 3' 'this' 'is' 'a' 'example' 'added4' ]
    
    UNI: [ 'Test' 'test123' 'test1234' 'flexare' 'this' 'is' 'a' 'example' 'added 1' 'added2' 'added 3' 'added4' ]
     in A or B
    INT: [ 'Test' 'test123' 'test1234' 'flexare' 'this' 'is' 'a' 'example' ] in A and B
    A\B: [  ] in A but not in B
    B\A: [ 'added 1' 'added2' 'added 3' 'added4' ] in B but not in A
    Bad Shares File:
    'added 1'
    'added2'
    'added 3'
    'added4'
    

    reads your sample files, computes the relative complement B\A, and writes it to a files.

    It uses the diffArray() function from here; this function should go in the file SetLib.vbs in the same folder (the ExecuteGlobal-line 'imports' the function).

    The Fi2Ar() function reads the .txt files; I quoted the elements to improve the display, you should remove the statement after testing.

    0 讨论(0)
提交回复
热议问题