开发者

vbscript inserting a line alphabetically

I'm trying to insert a string that begins with the word title as in title: New string to be inserted into a file that has the following format. A bunch of text at the top, a block of lines each beginning with the word title:, and a bunch of text at the bottom.

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

The thing is I'd prefer to insert that new string title: New string to be inserted so that it's organized alphabetically in the block of titles to make it easier to maintain this file. How can I do this with vbscript. I discovered it a few hours ago and think it's a great alternative for what I'm trying to do, but not that great at it yet, so any help would be great

How would I loop through all lines of the file, copy each line to a new file, until I hit a title that is alphabetically after my title, add my title to the new file at that spot, then copy the rest of the file to the new file and close.

Because I'm poor in vbscript syntax, I can only think of a pseudo algorithm,

Loop to re开发者_运维百科ad 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


There's no easy way to sort in vbscript. You have to do your own sort subroutine. Here's a little cheating using the windows cmd sort.

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  

Output

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

If you want to do the sorting with vbscript, you can search stackoverflow for "vbscript sort arrays" and there are suggestions on how to do that.


EDIT: changed code to VB.

Very simple. You can achieve this by concatenating the string in VB.

Before adding the stringToBeInserted just do this before hand

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

Hope this helps. Let me know if it does.

PK

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜