0

所以我创建了这两个数组 Approved share 和 current share。

'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
4

2 回答 2

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
于 2013-10-21T08:43:45.937 回答
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,并将其写入文件。

它使用此处的 diffArray() 函数;此函数应位于同一文件夹中的文件 SetLib.vbs 中(ExecuteGlobal 行“导入”该函数)。

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

于 2013-10-18T16:41:30.543 回答