2013-05-25 3 views
5

txt 파일에서 "Test Case"문자열 &을 찾아야합니다.VBS 스크립트를 사용하여 텍스트 파일에서 특정 문자열 찾기

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

당신은 내가 문자열 "테스트 케이스"의 발생이 두 번째 줄에서 볼 수 있듯이 :

나는 당신에게 당신이이 파일에서 찾을 수있는 라인의 예를 제공합니다.

내가하고 싶은 일은 "Test Case 5"가 나타나는 행 앞에 다른 특정 문자열을 추가하는 것입니다. 예를 들어 :

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

내가 추가 라인은 테스트 케이스의 수에 따라 달라집니다 내가, 내가 & 내가, 내가 그나마 "테스트 케이스"의 첫 번째 발생 전에 추가해야 인덱스를 가지고도 중요 다음 사건에주의하십시오. InStr이 기능은 예를 들어 함께 일 경우

나는 시험 :

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 

을 그리고 그것은 내 문자열을 인식, 작동합니다. 내가 같은 InStr이 기능을 사용하여, 나는 몇 가지 문제가 훨씬 더 라인 파일을 통해 루프를 시도 할 때, 이제

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

:이 작은 예에서 txt 파일은 아래 행을 contans.

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 

문제점은 무엇입니까 : 나는 다음과 같은 루프를 썼다?

답변

2

나는이 대신 문자열 연산의 정규 표현식을 사용하는 것이 좋습니다 것 :

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") 

뒤에 숫자 문자열 Test Case을 포함하는 행 다음에 줄 바꿈 (\n)와 일치한다 정규 표현식 (\d), 바꾸기는 삽입 할 텍스트 앞에 변수를 추가합니다 (변수 newtext). re.Global = False을 설정하면 처음으로 일치 한 후에 교체가 중지됩니다. 텍스트 파일의 줄 바꿈이 CR-LF (캐리지 리턴 + 줄 바꿈)로 인코딩하는 경우

당신은 vbCrLf\r\nvbLf\n을 변경해야합니다.

여러 개의 텍스트 파일을 수정해야 할 경우이 같은 루프에서 그것을 할 수 :

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

을 기대하고있어 결과이지만, 나는 어디에서부터 시작해야할지 몰랐다. 그들은 나에게 아람어처럼 보인다! 비록 내가 자바와 시각적 인 기초를 알고 있지만 나는 그들을 사용하여 결코 감히하지 않았다. 나는이 가능성을 고려할 것이지만 정규 표현식에 대한 튜토리얼 (인형 용)에 대한 좋은 링크를 줄 수 있습니까? – Luceye85

+0

[this] (http://www.codeproject.com/Articles/939/An-Introduction-to- Regular-Expressions) 및 물론 [documentation] (http://msdn.microsoft.com/en/ -us/library/6wzad2b2). –

0

시도는 다음과 같이 변경합니다 ..

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

Hello matzone, 내가 얼마나 바보인지 깨닫게 해줘서 고마워 !! 나는 CInt (색인)를 쓰는 것을 잊었다. 스크립트가이 수정과 함께 작동하는지 확인하고 알려 드리겠습니다. – Luceye85

+0

사실 올바른 수정은 CStr (색인)이었습니다.이 방법이 작동하는지 확인했지만 그렇지 않습니다. 거기에 왜 다른 이유가있을 수 있습니다 ... – Luceye85

+0

그 인덱스는 정수 var입니다 가정 .. – matzone

4

와우를, 몇 가지 시도 후 I 마침내 VB에서 텍스트 편집을 처리하는 방법을 알아 냈습니다. 이 코드는 완벽하게 작동하며 기대했던 결과를 제공합니다. 어쩌면 이렇게하는 것이 가장 좋은 방법은 아니지만 그 일을하는 것입니다. 여기에 코드입니다 :

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 

이 원래 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> 

입니다 그리고 이것은 내가 또한 등록 표현식을 사용하는 방법에 대한 생각

<?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

와 문자열을 만드는 데 사용되는 이것은 원래 텍스트입니다 – Luceye85

관련 문제