比较数组中的文件,删除文本文件中的行,函数,日志记录

时间:2013-10-18 15:53:24

标签: 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\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
added 1
added2
test1234
flexare
added 3
   this
is
a
  example
added4

我希望现有股票与批准的股票一样

Test
      test123
    test1234
    flexare
       this
    is
    a
      example

删除下面的数组文件并放入另一个txt文件

added 1
added2
added 3
added4

2 个答案:

答案 0 :(得分:1)

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

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

您可以使用ArrayList对象:

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

答案 1 :(得分:0)

所以你的问题是在集合'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()

输出:

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'

读取您的示例文件,计算相对补码B \ A,并将其写入文件。

它使用来自here的diffArray()函数;此函数应该放在同一文件夹中的文件SetLib.vbs中(ExecuteGlobal-line'导入'函数)。

Fi2Ar()函数读取.txt文件;我引用了元素来改进显示,你应该在测试后删除它。