2013-10-18 1 views
0

그래서이 두 배열을 승인 된 공유 및 현재 공유로 만들었습니다.배열의 파일 비교, 텍스트 파일, 함수, 기록에서 줄 삭제

'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 

나는 다음 두 배열을 가지고 현재의 주에서 배열 항목을 가지고 그 배열을 삭제하고 다시 로그인 할 경우 승인 된 주에 있는지 확인해야합니다. 이것은 두 코드를 비교하기 위해 사용했던 코드이지만 작동하지 않습니다.

/c net.exe share %LINE% /DELETE 

이있는 파일

'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 

이 내가이 Wscript.shell에 무엇을 포함하고있다

'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 

txt 파일에서 줄을 삭제하기 위해 사용 된 기능입니다 승인 된 주식

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 
+0

내가 네브라스카 많은 txt 파일에 대해이 작업을 수행하기 위해이 작업을 수행해야합니다. 많은 txt 파일을 교환 할 수 있어야합니다. 원하는 결과는 –

답변

1

당신은 현재 주식의 승인 공유 목록에 있는지 확인하려면, 그래서 당신은 당신의 내부 및 외부 루프를 전환해야 그래서 당신은하지만, 당신은 ArrayList 개체를 사용할 수

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 
0

문제는 '현재'집합에있는 '승인'집합에서 '승인'집합의 상대 보완을 얻는 것이지만 '현재'에는 있지만 '승인'에는 없습니다.

스크립트 :

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() 함수를 사용합니다 (here). 이 함수는 동일한 폴더의 SetLib.vbs 파일에 있어야합니다 (ExecuteGlobal 행은 함수를 '가져옵니다').

Fi2Ar() 함수는 .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를 내장 배열은 객체되지 않습니다

+0

위에 게시됩니다. diffarray가 유형 불일치라는 것을 나타냅니다. 배열이 작동하지 않으며 objTextFile1이 정의되지 않았다 –

관련 문제