windows - vbscript 按字母顺序插入一行

标签 windows vbscript batch-file

我正在尝试插入一个以单词 title 开头的字符串,如 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 以便它在标题 block 中按字母顺序组织,以便更容易维护此文件。我怎样才能用 vbscript 做到这一点。我在几个小时前发现了它,并认为它是我正在尝试做的事情的一个很好的替代方案,但还不是很擅长,所以任何帮助都会很棒

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.

因为我的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

最佳答案

在 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”,并且有关于如何进行排序的建议。

关于windows - vbscript 按字母顺序插入一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3839955/

相关文章:

Windows批处理文件,等待命令完成?

com - 在 HTTPD 中调试 ASP 页面

string - 如何对字符串列表进行批量排序?

batch-file - for 循环中的管道中断双引号变量

windows - 使用临时端口的 Boost::asio UDP 广播

java - 更改 Java 安全级别

ruby-on-rails - Michael Hartl 的 Ruby on Rails 教程第 2 章练习(demo_app): app works on Windows 8 localhost, 但在 Heroku 上不起作用

windows - 用于检查系统中已安装程序及其版本的脚本

vbscript - "Identity cannot be determined for newly inserted rows"在 ADO RecordSet AddNew 和 Update 之后

excel - 从 Jenkins 运行 Excel 宏