2013-05-25 20 views
5

Potrzebuję znaleźć ciąg "Test Case" & indeksu w pliku txt.Znajdź konkretny ciąg w pliku tekstowym ze skryptem VBS

dam wam przykład linii można znaleźć w tym pliku:

<tr><td><a href="../../Login/Log_ in_U1A1">Log_ in_U1A1</a></td></tr> 
<tr><td><a href="../Test case 5 DD/Form1">Form1</a></td></tr> 

Jak widać w drugim wierszu Mam wystąpienie napisu „Test case”.

Co chcę zrobić, to dodać kolejny konkretny ciąg w linii, która poprzedza ten, w którym pojawia się "Test Case 5". Na przykład:

<tr><td><a href="../../Login/Log_ in_U1A1">Log_ in_U1A1</a></td></tr> 
<tr><td><a href="../../Logs/DD/Beginning_of_DD_TC5.html">Beginning_of_DD_TC5</a></td></tr> 
<tr><td><a href="../Test case 5 DD/Form1">Form1</a></td></tr> 

Ważne jest również, że linia dodaję ma indeks i który zależy od liczby przypadków testowych, a muszę dodać go przed pierwszym wystąpieniem „test case” & ja, ja nie dbaj o następujące zdarzenia.

Testowałem jeśli funkcja InStr pracował na przykładzie:

Dim objFSO, filepath, objInputFile, tmpStr, substrToFind 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
filepath = "C:\VBS\filediprova.txt" 
substrToFind = "<tr><td><a href=" & chr(34) & "../Test case 5" 
Set objInputFile = objFSO.OpenTextFile(filepath) 
tmpStr = objInputFile.ReadLine 
If InStr(tmpStr, substrToFind) <= 0 Then 
    WScript.Echo "No matches" 
Else 
    WScript.Echo "Found match" 
End If 

I to działa, rozpoznaje mojego podciąg. W tej niewielkiej przykład plik txt contans tylko następujący wiersz:

<tr><td><a href="../Test case 5 DD/Form1">Form1</a></td></tr> 

Teraz, gdy próbuję pętli nad plikiem z dużo większej liczby linii mam jakiś problem, używam tej samej funkcji InStr. napisałem następującą pętlę:

Do until objInputFile.AtEndOfStream 
    strToAdd = "<tr><td><a href=" & chr(34) & "../../Logs/DD/Beginning_of_DD_TC" & CStr(index) & ".html" & chr(34) & ">Beginning_of_DD_TC" & CStr(index) & "</a></td></tr>" 
    substrToFind = "<tr><td><a href=" & chr(34) & "../Test case " & index 
    firstStr = "<?xml version" 'my file always starts like this 
    tmpStr = objInputFile.ReadLine 
    If InStr(tmpStr, substrToFind) <= 0 Then 
     If Instr(tmpStr, firstStr) > 0 Then 
      text = tmpStr 'to avoid the first empty line 
     Else 
      text = text & vbCrLf & tmpStr 
     End If 
    Else 
     text = text & vbCrLf & strToAdd & vbCrLf & tmpStr 
     index = index + 1 
    End If 
Loop 

Co się stało?

Odpowiedz

2

Polecam przy użyciu wyrażeń regularnych zamiast operacji strun do tego:

Set fso = CreateObject("Scripting.FileSystemObject") 

filename = "C:\VBS\filediprova.txt" 

newtext = vbLf & "<tr><td><a href=""..."">Beginning_of_DD_TC5</a></td></tr>" 

Set re = New RegExp 
re.Pattern = "(\n.*?Test Case \d)" 
re.Global = False 
re.IgnoreCase = True 

text = f.OpenTextFile(filename).ReadAll 
f.OpenTextFile(filename, 2).Write re.Replace(text, newText & "$1") 

Wyrażenie regularne będzie pasować do nowego wiersza (\n), a następnie przez linię zawierającą ciąg Test Case następnie przez szereg (\d), a zastąpienie będzie poprzedzać tekstem, który chcesz wstawić (zmienna newtext). Ustawienie re.Global = False powoduje zatrzymanie zamiennika po pierwszym dopasowaniu.

Jeśli podziały wiersza w pliku tekstowym są kodowane jako CR-LF (powrót karetki + line feed) musisz zmienić \n do \r\n i vbLf do vbCrLf.

Jeśli trzeba zmodyfikować kilka plików tekstowych, można to zrobić w pętli jak ta:

For Each f In fso.GetFolder("C:\VBS").Files 
    If LCase(fso.GetExtensionName(f.Name)) = "txt" Then 
    text = f.OpenAsTextStream.ReadAll 
    f.OpenAsTextStream(2).Write re.Replace(text, newText & "$1") 
    End If 
Next 
+0

Byłem też myśleć o użyciu wyrażenia reg, ale Nie wiedziałem, od czego zacząć, wydają mi się aramejskie! Chociaż znam java i visual basic, nigdy nie odważyłem się ich użyć. Zastanowię się nad tą możliwością, ale czy możesz dać mi dobry link do samouczka (dla manekinów) o regex? – Luceye85

+0

Wypróbuj [this] (http://www.codeproject.com/Articles/939/An-Introduction-to-Regular-Expressions) i oczywiście [dokumentacja] (http://msdn.microsoft.com/en -us/library/6wzad2b2). –

0

Spróbuj zmienić tak ..

firstStr = "<?xml version" 'my file always starts like this 

Do until objInputFile.AtEndOfStream 

    strToAdd = "<tr><td><a href=" & chr(34) & "../../Logs/DD/Beginning_of_DD_TC" & CStr(index) & ".html" & chr(34) & ">Beginning_of_DD_TC" & CStr(index) & "</a></td></tr>" 

    substrToFind = "<tr><td><a href=" & chr(34) & "../Test case " & trim(cstr((index))) 

    tmpStr = objInputFile.ReadLine 

    If InStr(tmpStr, substrToFind) <= 0 Then 
     If Instr(tmpStr, firstStr) > 0 Then 
      text = tmpStr 'to avoid the first empty line 
     Else 
      text = text & vbCrLf & tmpStr 
     End If 
    Else 
     text = text & vbCrLf & strToAdd & vbCrLf & tmpStr 

    End If 
    index = index + 1 
Loop 
+0

Witajcie w mezonie, dziękuję, że uświadomiłem sobie, jaki jestem głupi! Nie zdawałem sobie sprawy, że zapomniałem napisać CInt (indeks). Sprawdzę, czy skrypt będzie działał z tą modyfikacją i powiadomię cię. – Luceye85

+0

Prawdą jest, że poprawną zmianą był CStr (indeks), sprawdziłem, czy działa tak, ale tak nie jest. Może istnieć jakiś inny powód, dla którego ... – Luceye85

+0

Założono, że indeks jest liczbą całkowitą var .. – matzone

4

wow, po kilku próbach w końcu zorientowałem się, jak sobie poradzić z edycjami tekstowymi w vbs. Kod działa idealnie, daje mi oczekiwany rezultat. Może to nie jest najlepszy sposób, aby to zrobić, ale spełnia swoją rolę. Oto kod:

Option Explicit 

Dim StdIn: Set StdIn = WScript.StdIn 
Dim StdOut: Set StdOut = WScript 


Main() 

Sub Main() 

Dim objFSO, filepath, objInputFile, tmpStr, ForWriting, ForReading, count, text, objOutputFile, index, TSGlobalPath, foundFirstMatch 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
TSGlobalPath = "C:\VBS\TestSuiteGlobal\Test suite Dispatch Decimal - Global.txt" 
ForReading = 1 
ForWriting = 2 
Set objInputFile = objFSO.OpenTextFile(TSGlobalPath, ForReading, False) 
count = 7 
text="" 
foundFirstMatch = false 

Do until objInputFile.AtEndOfStream 
    tmpStr = objInputFile.ReadLine 
    If foundStrMatch(tmpStr)=true Then 
     If foundFirstMatch = false Then 
      index = getIndex(tmpStr) 
      foundFirstMatch = true 
      text = text & vbCrLf & textSubstitution(tmpStr,index,"true") 
     End If 
     If index = getIndex(tmpStr) Then 
      text = text & vbCrLf & textSubstitution(tmpStr,index,"false") 
     ElseIf index < getIndex(tmpStr) Then 
      index = getIndex(tmpStr) 
      text = text & vbCrLf & textSubstitution(tmpStr,index,"true") 
     End If 
    Else 
     text = text & vbCrLf & textSubstitution(tmpStr,index,"false") 
    End If 
Loop 
Set objOutputFile = objFSO.CreateTextFile("C:\VBS\NuovaProva.txt", ForWriting, true) 
objOutputFile.Write(text) 
End Sub 


Function textSubstitution(tmpStr,index,foundMatch) 
Dim strToAdd 
strToAdd = "<tr><td><a href=" & chr(34) & "../../Logs/CF5.0_Features/Beginning_of_CF5.0_Features_TC" & CStr(index) & ".html" & chr(34) & ">Beginning_of_CF5.0_Features_TC" & CStr(index) & "</a></td></tr>" 
If foundMatch = "false" Then 
    textSubstitution = tmpStr 
ElseIf foundMatch = "true" Then 
    textSubstitution = strToAdd & vbCrLf & tmpStr 
End If 
End Function 


Function getIndex(tmpStr) 
Dim substrToFind, charAtPos, char1, char2 
substrToFind = "<tr><td><a href=" & chr(34) & "../Test case " 
charAtPos = len(substrToFind) + 1 
char1 = Mid(tmpStr, charAtPos, 1) 
char2 = Mid(tmpStr, charAtPos+1, 1) 
If IsNumeric(char2) Then 
    getIndex = CInt(char1 & char2) 
Else 
    getIndex = CInt(char1) 
End If 
End Function 

Function foundStrMatch(tmpStr) 
Dim substrToFind 
substrToFind = "<tr><td><a href=" & chr(34) & "../Test case " 
If InStr(tmpStr, substrToFind) > 0 Then 
    foundStrMatch = true 
Else 
    foundStrMatch = false 
End If 
End Function 

Jest to oryginalny plik txt

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <meta content="text/html; charset=UTF-8" http-equiv="content-type" /> 
    <title>Test Suite</title> 
</head> 
<body> 
<table id="suiteTable" cellpadding="1" cellspacing="1" border="1" class="selenium"><tbody> 
<tr><td><b>Test Suite</b></td></tr> 
<tr><td><a href="../../Component/TC_Environment_setting">TC_Environment_setting</a></td></tr> 
<tr><td><a href="../../Component/TC_Set_variables">TC_Set_variables</a></td></tr> 
<tr><td><a href="../../Component/TC_Set_ID">TC_Set_ID</a></td></tr> 
<tr><td><a href="../../Login/Log_in_Admin">Log_in_Admin</a></td></tr> 
<tr><td><a href="../../Component/Set_Roles_Dispatch_Decimal">Set_Roles_Dispatch_Decimal</a></td></tr> 
<tr><td><a href="../../Login/Log_ in_U1A1">Log_ in_U1A1</a></td></tr> 
<tr><td><a href="../Test case 5 DD/Form1">Form1</a></td></tr> 
<tr><td><a href="../Test case 5 DD/contrD1">contrD1</a></td></tr> 
<tr><td><a href="../../Login/Logout">Logout</a></td></tr> 
<tr><td><a href="../../Login/Log_ in_U1B1">Log_ in_U1B1</a></td></tr> 
<tr><td><a href="../../Component/Search&OpenApp">Search&OpenApp</a></td></tr> 
<tr><td><a href="../Test case 5 DD/FormEND">FormEND</a></td></tr> 
<tr><td><a href="../../Component/Controllo END">Controllo END</a></td></tr> 
<tr><td><a href="../../Login/Logout">Logout</a></td></tr> 
<tr><td><a href="../../Login/Log_ in_U1A1">Log_ in_U1A1</a></td></tr> 
<tr><td><a href="../Test case 6 DD/Form1">Form1</a></td></tr> 
<tr><td><a href="../Test case 6 DD/contrD1">contrD1</a></td></tr> 
<tr><td><a href="../../Login/Logout">Logout</a></td></tr> 
<tr><td><a href="../../Login/Log_ in_U1B1">Log_ in_U1B1</a></td></tr> 
<tr><td><a href="../../Component/Search&OpenApp">Search&OpenApp</a></td></tr> 
<tr><td><a href="../Test case 5 DD/FormEND">FormEND</a></td></tr> 
<tr><td><a href="../../Component/Controllo END">Controllo END</a></td></tr> 
<tr><td><a href="../../Login/Logout">Logout</a></td></tr> 
<tr><td><a href="../../Login/Log_ in_U1A1">Log_ in_U1A1</a></td></tr> 
<tr><td><a href="../Test case 7 DD/Form1">Form1</a></td></tr> 
<tr><td><a href="../../Component/Controllo DeadLetter">Controllo DeadLetter</a></td></tr> 
<tr><td><a href="../../Login/Logout">Logout</a></td></tr> 
<tr><td><a href="../../Component/Set_Roles_Dispatch_Decimal">Set_Roles_Dispatch_Decimal</a></td></tr> 
<tr><td><a href="../../Login/Logout_BAC">Logout_BAC</a></td></tr> 
</tbody></table> 
</body> 
</html> 

I to jest wynik Czekam

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <meta content="text/html; charset=UTF-8" http-equiv="content-type" /> 
    <title>Test Suite</title> 
</head> 
<body> 
<table id="suiteTable" cellpadding="1" cellspacing="1" border="1" class="selenium"><tbody> 
<tr><td><b>Test Suite</b></td></tr> 
<tr><td><a href="../../Component/TC_Environment_setting">TC_Environment_setting</a></td></tr> 
<tr><td><a href="../../Component/TC_Set_variables">TC_Set_variables</a></td></tr> 
<tr><td><a href="../../Component/TC_Set_ID">TC_Set_ID</a></td></tr> 
<tr><td><a href="../../Login/Log_in_Admin">Log_in_Admin</a></td></tr> 
<tr><td><a href="../../Component/Set_Roles_Dispatch_Decimal">Set_Roles_Dispatch_Decimal</a></td></tr> 
<tr><td><a href="../../Login/Log_ in_U1A1">Log_ in_U1A1</a></td></tr> 
<tr><td><a href="../../Logs/CF5.0_Features/Beginning_of_CF5.0_Features_TC5.html">Beginning_of_CF5.0_Features_TC5</a></td></tr> 
<tr><td><a href="../Test case 5 DD/Form1">Form1</a></td></tr> 
<tr><td><a href="../Test case 5 DD/Form1">Form1</a></td></tr> 
<tr><td><a href="../Test case 5 DD/contrD1">contrD1</a></td></tr> 
<tr><td><a href="../../Login/Logout">Logout</a></td></tr> 
<tr><td><a href="../../Login/Log_ in_U1B1">Log_ in_U1B1</a></td></tr> 
<tr><td><a href="../../Component/Search&OpenApp">Search&OpenApp</a></td></tr> 
<tr><td><a href="../Test case 5 DD/FormEND">FormEND</a></td></tr> 
<tr><td><a href="../../Component/Controllo END">Controllo END</a></td></tr> 
<tr><td><a href="../../Login/Logout">Logout</a></td></tr> 
<tr><td><a href="../../Login/Log_ in_U1A1">Log_ in_U1A1</a></td></tr> 
<tr><td><a href="../../Logs/CF5.0_Features/Beginning_of_CF5.0_Features_TC6.html">Beginning_of_CF5.0_Features_TC6</a></td></tr> 
<tr><td><a href="../Test case 6 DD/Form1">Form1</a></td></tr> 
<tr><td><a href="../Test case 6 DD/contrD1">contrD1</a></td></tr> 
<tr><td><a href="../../Login/Logout">Logout</a></td></tr> 
<tr><td><a href="../../Login/Log_ in_U1B1">Log_ in_U1B1</a></td></tr> 
<tr><td><a href="../../Component/Search&OpenApp">Search&OpenApp</a></td></tr> 
<tr><td><a href="../../Component/Controllo END">Controllo END</a></td></tr> 
<tr><td><a href="../../Login/Logout">Logout</a></td></tr> 
<tr><td><a href="../../Login/Log_ in_U1A1">Log_ in_U1A1</a></td></tr> 
<tr><td><a href="../../Logs/CF5.0_Features/Beginning_of_CF5.0_Features_TC7.html">Beginning_of_CF5.0_Features_TC7</a></td></tr> 
<tr><td><a href="../Test case 7 DD/Form1">Form1</a></td></tr> 
<tr><td><a href="../../Component/Controllo DeadLetter">Controllo DeadLetter</a></td></tr> 
<tr><td><a href="../../Login/Logout">Logout</a></td></tr> 
<tr><td><a href="../../Component/Set_Roles_Dispatch_Decimal">Set_Roles_Dispatch_Decimal</a></td></tr> 
<tr><td><a href="../../Login/Logout_BAC">Logout_BAC</a></td></tr> 
</tbody></table> 
</body> 
</html> 
+0

To jest tekst oryginalny – Luceye85

Powiązane problemy