

(As an added bonus, that webcast will also show you how you can use a bubble sort to sort single-field data.)īefore we begin, we assume you have a text file that looks like this, with one computer name per line: red-ws-02 We’ll give you a quick overview of disconnected recordsets here if you want a more detailed explanation of how they work, you might want to check out the Scripting Week 2 webcast Things the Scripting Guys Never Told You. There are other ways we could do this (say, a bubble sort), but a disconnected recordset gives us more flexibility and is much easier to explain, especially for those of you who have some experience with database programming. To carry out this task we’ve chosen to use a disconnected recordset. A slightly roundabout way of doing things, but you’ll end up with a text file sorted alphabetically, which is all you really care about.

But this time we’re going to give you a workaround.Īlthough there’s no straightforward way to sort a text file, we can achieve the same net result by doing the following: 1) Use the FileSystemObject to read the file into memory 2) Sort the file alphabetically in memory 3) Replace the existing contents of the file with the sorted data we have in memory.

Oh, right: we were hoping everyone had forgotten about those times. But, hey, when have the Scripting Guys ever taken the easy way out? If we wanted to take the easy way out we’d just tell you, “Sorry, you can’t do that.” And we could get away with that because none of Microsoft’s scripting technologies feature a way to open up and then sort a text file. Hey, Scripting Guy! I have a text file that contains a list of computer names.
