Arrays 比较数组中的文件、从文本文件中删除行、函数、日志

Arrays 比较数组中的文件、从文本文件中删除行、函数、日志,arrays,vb.net,function,logging,vbscript,Arrays,Vb.net,Function,Logging,Vbscript,所以我创建了这两个数组:核准共享和当前共享 '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\Re

所以我创建了这两个数组:核准共享和当前共享

'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
然后,我需要获取两个数组,并从当前共享中获取一个数组项,查看它是否在已批准的共享中,如果不在,则删除该数组并记录它。这是我用来比较两者的代码,但它不起作用

'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
这是我用来从txt文件中删除该行的函数

'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
这是Wscript.shell中包含的内容

/c net.exe share %LINE% /DELETE
这是已批准共享中的文件

Test
  test123
test1234
flexare
   this
is
a
  example
Test
      test123
    test1234
    flexare
       this
    is
    a
      example
这是当前共享中的文件

Test
  test123
added 1
added2
test1234
flexare
added 3
   this
is
a
  example
added4
我希望当前股票与核准股票一样

Test
  test123
test1234
flexare
   this
is
a
  example
Test
      test123
    test1234
    flexare
       this
    is
    a
      example
移除下面的数组文件并放入另一个txt文件

added 1
added2
added 3
added4

因此,您的问题是在集合“current”中获取集合“approved”的相对补码:那些元素集合在“current”中,但不在“approved”中

剧本:

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()
读取示例文件,计算相对补码B\A,并将其写入A文件

它使用来自的diffArray()函数;此函数应放在同一文件夹的SetLib.vbs文件中(ExecuteGlobal行“导入”函数)


函数的作用是:读取.txt文件;我引用了元素以改善显示效果,您应该在测试后删除该语句。

您想检查当前的共享是否在已批准的共享列表中,因此您需要切换内部和外部循环:

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内置数组不是对象,因此不能使用

If arrLines.Contains(CurrentShareName) then
  ...
End If
不过,您可以使用对象:

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

我需要对许多txt文件执行此操作,因此我需要对许多txt文件进行互换。当我运行它时,所需的结果也在上面发布。它表明diffarray是一个类型不匹配,当我将option explicit放在顶部时,第二个数组不起作用,它表示未定义objTextFile1