gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 11:22:14 25 4
gpt4 key购买 nike

我正在尝试插入一个以单词 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com