我正在尝试插入一个以单词 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/
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
这里是Ruby新手。完成一些练习后碰壁了。练习:计算一系列成绩的字母等级创建一个方法get_grade来接受测试分数数组。数组中的每个分数应介于0和100之间,其中100是最大分数。计算平均分并将字母等级作为字符串返回,即“A”、“B”、“C”、“D”、“E”或“F”。我一直返回错误:avg.rb:1:syntaxerror,unexpectedtLBRACK,expecting')'defget_grade([100,90,80])^avg.rb:1:syntaxerror,unexpected')',expecting$end这是我目前所拥有的。我想坚持使用下面的方法或.join,
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
我有一个驼峰式字符串,例如:JustAString。我想按照以下规则形成长度为4的字符串:抓取所有大写字母;如果超过4个大写字母,只保留前4个;如果少于4个大写字母,则将最后大写字母后的字母大写并添加字母,直到长度变为4。以下是可能发生的3种情况:ThisIsMyString将产生TIMS(大写字母);ThisIsOneVeryLongString将产生TIOV(前4个大写字母);MyString将生成MSTR(大写字母+tr大写)。我设法用这个片段解决了前两种情况:str.scan(/[A-Z]/).first(4).join但是,我不太确定如何最好地修改上面的代码片段以处理最后一种
这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub
之前在培训新生的时候,windows环境下配置opencv环境一直教的都是网上主流的vsstudio配置属性表,但是这个似乎对新生来说难度略高(虽然个人觉得完全是他们自己的问题),加之暑假之后对cmake实在是爱不释手,且这样配置确实十分简单(其实都不需要配置),故斗胆妄言vscode下配置CV之法。其实极为简单,图比较多所以很长。如果你看此文还配不好,你应该思考一下是不是自己的问题。闲话少说,直接开始。0.CMkae简介有的人到大二了都不知道cmake是什么,我不说是谁。CMake是一个开源免费并且跨平台的构建工具,可以用简单的语句来描述所有平台的编译过程。它能够根据当前所在平台输出对应的m
深度学习部署:Windows安装pycocotools报错解决方法1.pycocotools库的简介2.pycocotools安装的坑3.解决办法更多Ai资讯:公主号AiCharm本系列是作者在跑一些深度学习实例时,遇到的各种各样的问题及解决办法,希望能够帮助到大家。ERROR:Commanderroredoutwithexitstatus1:'D:\Anaconda3\python.exe'-u-c'importsys,setuptools,tokenize;sys.argv[0]='"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-instal
@raw_array[i]=~/[\W]/非常简单的正则表达式。当我用一些非拉丁字母(具体来说是俄语)尝试时,条件是错误的。我能用它做什么? 最佳答案 @raw_array[i]=~/[\p{L}]/使用西里尔字符进行测试。引用:http://www.regular-expressions.info/unicode.html#prop 关于ruby-正则表达式将非英文字母匹配为非单词字符,我们在StackOverflow上找到一个类似的问题: https://
RSpec似乎按顺序匹配方法接收的消息。我不确定如何使以下代码工作:allow(a).toreceive(:f)expect(a).toreceive(:f).with(2)a.f(1)a.f(2)a.f(3)我问的原因是a.f的一些调用是由我的代码的上层控制的,所以我不能对这些方法调用添加期望。 最佳答案 RSpecspy是测试这种情况的一种方式。要监视一个方法,用allowstub,除了方法名称之外没有任何约束,调用该方法,然后expect确切的方法调用。例如:allow(a).toreceive(:f)a.f(2)a.f(1)
我正在尝试创建一个带有项目符号字符的Ruby1.9.3字符串。str="•"+"helloworld"但是,当我输入它时,我收到有关非ASCII字符的语法错误。我该怎么做? 最佳答案 你可以把Unicode字符放在那里。str="\u2022"+"helloworld" 关于ruby-如何在Ruby字符串中插入项目符号字符?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/1195