【问题标题】:vbscript inserting a line alphabeticallyvbscript 按字母顺序插入一行
【发布时间】:2010-10-01 14:12:36
【问题描述】:

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

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

【问题讨论】:

  • 在你的循环中,前两行对我来说很有意义。但是,如果在循环中第三行的我的标题之前,您是什么意思?如果在我的标题之后,在循环中的第 4 行,然后你还说在那里复制我的标题,你是什么意思?我不明白。请详细说明。

标签: windows vbscript batch-file


【解决方案1】:

在 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 排序数组”,并提供有关如何执行此操作的建议。

【讨论】:

    【解决方案2】:

    编辑:将代码更改为 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

    【讨论】:

    • 我更新了答案,以便您理解。它现在在 VB 中
    猜你喜欢
    • 2019-01-25
    • 2012-10-21
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    • 2015-06-28
    相关资源
    最近更新 更多