I’m trying to insert a string that begins with the word title as in title: New string to be inserted
into a file that has the following format. A bunch of text at the top, a block of lines each beginning with the word title:, and a bunch of text at the bottom.
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
The thing is I’d prefer to insert that new string title: New string to be inserted so that it’s organized alphabetically in the block of titles to make it easier to maintain this file. How can I do this with vbscript. I discovered it a few hours ago and think it’s a great alternative for what I’m trying to do, but not that great at it yet, so any help would be great
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.
Because I’m poor in vbscript syntax, I can only think of a pseudo algorithm,
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
There’s no easy way to sort in vbscript. You have to do your own sort subroutine. Here’s a little cheating using the windows cmd sort.
Output
If you want to do the sorting with vbscript, you can search stackoverflow for “vbscript sort arrays” and there are suggestions on how to do that.