Readall split



Keywords: readall split
Description: Learn about Windows PowerShell

Hey, Scripting Guy! Right now I use a script to open a text file containing computer names. The script reads the first line of the file and connects to that computer, then reads the second line of the file and connects to that computer. Is there a way to store those names in a variable or something, so that I don’t have to keep reading the text file?

Hey, KS. We’re assuming that your script currently looks something like this one. In this sample script, we open a text file (servers.txt), read the first line (we’re also assuming that there is one computer name per line in the file), and then echo the name of the computer. (In your script, of course, you probably connect to that remote computer at this point.) We then read in the second line of the text file, echo the name of that computer, loop around, read in the third line of the text file, and continue in this vein until we’ve finished reading each line in the file:

Now, there’s nothing wrong with reading a text file this way; it works just fine. However, we can understand why you might want to read the text file all at once and then work with the list in memory. After all, suppose the text file is located on a remote computer. Not only will you have to continually travel across the network just to read a single line out of a text file, but if that remote computer goes down, then your script goes down as well.

So can you read the text file all at once, store that information in a variable, and then work with the list of computers from memory? Of course you can:

Here’s how the script works. Like your current script, we create a constant (ForReading) and assign it the value 1; this is required when using the FileSystemObject to read a text file. We then create an instance of the FileSystemObject, and use the OpenTextFile method to open the file C:\Scripts\Servers.txt. But you already knew that, didn’t you?

Here’s the part you’re really interested in. Instead of reading the file line-by-line, we use the ReadAll method to read the entire text file in one fell swoop, storing the contents of that file in the variable strText. The variable strText now becomes an exact replica of the text file. Suppose your text file looks like this:

Thus far we’ve succeeded in reading in the entire text file and storing it in a variable. However, that doesn’t really do us much good; after all, if we tried connecting to a computer using strText, we’d be connecting to the entire text file rather than a single computer, an operation doomed to fail. What we need to do now is somehow separate the individual computer names, so that we can then connect to the computers one at a time.

The Split command takes data (in this case, the variable strText) and creates an array out of it. How does it know where one computer name ends and the next begins? In our example, the “delimiter” that separates one computer from another is the carriage return linefeed; in our text file, we type one computer name, press ENTER, then type the next computer name. The VBScript constant vbCrLf used with the Split function represents the ENTER key. Suppose strText looked like this:






Photogallery Readall split:


EXTRA, EXTRA! Read all about it . . . Master RKC, Keira Newton, to ...


Using a text editor and regular expressions  Practical Computing ...


Congrats! Charlize Theron has secretly adopted another baby, a ...


THE ULTIMATE GUIDE TO A/B SPLIT TESTING | JOY GROUP OPEN SOURCE ...


SPLIT-PIVOT-3.png


Professor Green Read All About It Photo Shared By Shayne | Desktop ...


Split  in Croatia | The Traveled Mind


Holiday Villa in Kastel Sucurac, Nr. Split, Dalamatian Coast ...


Presentation "Python Code Examples. Word Spotting import sys ...


Chris Evans and Minka Kelly s Past Loves


Mobile spy reviews embrace home loans complaints rankin - Top 3 ...


Presentation "Python Code Examples. Word Spotting import sys ...


John's Naval, Marine and Military News on Blogger - Click here to ...


Knives knives knives! | Trapper Talk | Trapperman.com Forums


Board of State and Community Corrections - News


Psec prolific health options trading inc : Binary Deposit Bonus ...


CFB Film Preview Part 2: Oregon at Michigan State - Inside The Pylon


Samsung blackjack sms manage folders - Top Rated Online Casino ...


I only write songs about exes... I don't want to have to pen one ...


A book a day for Advent - You Baby Me Mummy


Script Read a Text File into an Array - smoltausubsu25's soup