2010-10-01 95 views
1

我試圖插入一個以word標題開頭的字符串,如title: New string to be inserted 到具有以下格式的文件中。頂部有一堆文本,每行以title:開頭,底部有一堆文本。vbscript按字母順序插入一行

Some content here at the top 
And more content 
and the titles will begin next 

title: Basic String 
title: How does it evaluate strings 
title: Identify code links 
title: Translating vbscript to string 

some content at the bottom 
and more content at the bottom 

的事情是我寧願插入新的字符串title: New string to be inserted,使其在標題塊按字母順序排列,使其更易於維護這個文件。我怎樣才能用vbscript做到這一點。我幾個小時前發現它,並認爲它是我想要做的一個很好的替代方案,但還沒那麼棒,所以任何幫助都會很棒

我將如何循環遍歷所有行 文件,每一行復制到一個新 文件,直到我打了一個標題,我的冠軍頭銜後,按字母順序是 ,加我 標題到新的文件在該點, 然後將文件的其餘部分複製到 新文件並關閉。

因爲我在VBScript語法不好,我只能想到一個僞算法,

Loop to read through all lines 
    If the line does not start with the word title:, copy it as is into the new file 
    If the line starts with the word title, remove the `title:` sub-string, then check if it is alphabetically before or after my title 
     If before my title, copy it into the new file 
     If after my title, then copy my title there, and copy all rest of the file as is, and EXIT 
End loop 
+0

在你的循環中,前兩行對我有意義。但是如果在我的循環第三行的標題之前,你的意思是什麼? 和你是什麼意思,如果在我的標題後,在循環中的第4行,然後你也說我的標題在那裏複製?我不明白。請詳細說明。 – Pavan 2010-10-01 15:09:41

回答

3

有在VBScript進行排序沒有簡單的方法。你必須做你自己的排序子程序。這裏有一點使用Windows cmd排序的作弊。

strToInsert= WScript.Arguments(0) 
strFileName = WScript.Arguments(1) 
Set objFS = CreateObject("Scripting.FileSystemObject") 
If objFS.FileExists("temp") Then 
    objFS.DeleteFile("temp") 
End If 
Set objRE = New RegExp 
objRE.IgnoreCase = False 
objRE.Pattern = "^title.*" 
Set objFile = objFS.OpenTextFile(strFileName) 
Set objOutFile = objFS.OpenTextFile("temp",2 , True) 
Dim A() 
d=0 
Do Until objFile.AtEndOfStream  
    linenum=objFile.Line 
    strLine = objFile.ReadLine 
    Set Matches = objRE.Execute(strLine) 
    For Each Match in Matches ' Iterate Matches collection.    
     objOutFile.Write(Match.Value & vbCrLf) 
     ReDim Preserve A(d) 
     A(d)=linenum ' get position of title lines 
     d=d+1 
    Next  
Loop  
objOutFile.Write(strToInsert & vbCrLf) 
objFile.Close 
objOutFile.Close 

Set WshShell = CreateObject("WScript.Shell") 
Set objFile = objFS.OpenTextFile(strFileName) 
Do Until objFile.AtEndOfStream 
    linenum=objFile.Line 
    strLine = objFile.ReadLine 
    c=0 
    If linenum = A(0) Then 
     Set oExec = WshShell.Exec("sort temp ") 
     Do While Not oExec.StdOut.AtEndOfStream 
       sLine = oExec.StdOut.ReadLine 
       WScript.Echo sLine 
       c=c+1 
     Loop 
     oExec.Terminate 
    End If 
    If linenum <A(0) Or linenum > A(UBound(A)) Then 
     WScript.Echo strLine 
    End If 
Loop 

輸出

C:\test>cscript //nologo myscript.vbs "title: to insert new string" file 
Some content here at the top 
And more content 
and the titles will begin next 

title: Basic String 
title: How does it evaluate strings 
title: Identify code links 
title: to insert new string 
title: Translating vbscript to string 

some content at the bottom 
and more content at the bottom 

如果你想要做使用VBScript的排序,你可以搜索計算器的「VBSCRIPT排序陣列」中就有關於如何做到這一點的建議。

1

編輯:改變代碼VB。

很簡單。 您可以通過連接VB中的字符串來實現此目的。

添加stringToBeInserted之前只是這樣做的前手

Dim stringToBeInserted As String = "WHATEVER THE NAME OF THE TITLE" 
Dim appendTitle As String = "Title: " 
Dim newStringToBeInserted As String = appendTitle & stringToBeInserted 

希望這有助於。 讓我知道如果它。

PK

+0

我更新了答案,以便您能理解它。它現在在VB中 – Pavan 2010-10-01 14:23:35