vbscript按字母顺序插入一行

时间:2010-10-01 14:12:36

标签: windows vbscript batch-file

我正在尝试插入以title: New string to be inserted中的单词标题开头的字符串 到具有以下格式的文件中。顶部有一堆文字,一行以每个标题开头的一行,以及一堆底部的文字。

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

2 个答案:

答案 0 :(得分: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进行排序,可以在stackoverflow中搜索“vbscript sort arrays”,并提出如何做的建议。

答案 1 :(得分: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