Comparing files in arrays, deleting lines from tex

2019-03-04 14:48发布

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 = CreateObject("Scripting.FileSystemObject") 
Dim objTextFile 
Set objTextFile = objFSO.OpenTextFile ("C:\Users\a352592\Desktop\Abdullahi\Remove Shares Utility\approvedshares.txt") 
Public strTextFile, strData, arrLines, LineCount
CONST ForReading = 1
strTextFile = ("C:\Users\a352592\Desktop\Abdullahi\Remove Shares Utility\approvedshares.txt")
strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll
arrLines = Split(strData,vbCrLf)
LineCount = UBound(arrLines) + 1
wscript.echo "Approved share count : " &Linecount

'Reads current shares txt and also makes that txt into an array
Set objTextFile1 = objFSO.OpenTextFile ("C:\Users\a352592\Desktop\Abdullahi\Remove Shares Utility\currentshares.txt") 
Public strTextFile1, strData1, arrLines1, LineCount1

strTextFile1 = ("C:\Users\a352592\Desktop\Abdullahi\Remove Shares Utility\currentshares.txt")
strData1 = objFSO.OpenTextFile(strTextFile1,ForReading).ReadAll
arrLines1 = Split(strData1,vbCrLf)
LineCount1 = UBound(arrLines1) + 1
wscript.echo "current share count : " &Linecount1

I then need to take the two arrays and take an array item from the current shares and see if it's in the approved shares if not then delete that array and log it. This is the code I was using to compare the two but it doesn't work.

'Compare the two arrays and take out the one's that don't match
For Each ApprovedShareName In arrLines
  found = False
  For Each CurrentShareName In arrLines1 
    If ApprovedShareName = CurrentShareName Then
    found = True
    Exit For
    End If
    found = False
  Next
  If found = False Then
  'wscript.echo "This isn't on approve shares text : " &textstream2
  End If 
Next

If arrLines.Contains(CurrentShareName) then

Else

end If

This is the function I was using to delete the line from the txt file

'Set oShell = WScript.CreateObject("WSCript.shell")
'Function oShell (linedelete)
'oShell (linedelete) = oShell.run cmd cd /d C:dir_test\file_test & sanity_check_env.bat arg1
'end Function

This is what was contained in Wscript.shell

/c net.exe share %LINE% /DELETE

This is the files in the approved shares

Test
  test123
test1234
flexare
   this
is
a
  example

This is the files in the current shares

Test
  test123
added 1
added2
test1234
flexare
added 3
   this
is
a
  example
added4

I want the current shares to be just like the approved shares

Test
      test123
    test1234
    flexare
       this
    is
    a
      example

With the array files below removed and put in another txt file

added 1
added2
added 3
added4

2条回答
等我变得足够好
2楼-- · 2019-03-04 15:15

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
查看更多
Bombasti
3楼-- · 2019-03-04 15:29

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.

查看更多
登录 后发表回答