Break Text File into Chunks with PowerShell

Doctor Scripto

Summary: Ed Wilson, Microsoft Scripting Guy, talks about breaking the contents of a text file into chunks with Windows PowerShell.

Hey, Scripting Guy! Question Hey, Scripting Guy! The other day, you tweeted Get First 140 Characters from String with PowerShell. That is fine, but what about the rest of the text? I mean, if I want to break a text file into lines of 140 characters, how do I go about it? Can you provide a concrete example please?

—BG

Hey, Scripting Guy! Answer Hello BG,

Microsoft Scripting Guy, Ed Wilson, is here. The markets are mixed, there is a chance of afternoon thunderstorms, and there is a lizard scampering about on the outside patio. In short, it is a normal morning around the Scripting household. I am sitting here with a glass of cold Green Tea that the Scripting Wife made last night, and I am checking the email sent to scripter@microsoft.com. Indeed, it is a normal day. It is a cool 78 degrees Fahrenheit (26 degrees Celsius), and I am thinking that I will soon begin drinking more hot tea in the mornings. In fact, I have a friend in New York who promised to send me some green tea…cool.

Anyway, BG, there are many ways of breaking a text file into pieces, but I will show you a really easy way to accomplish the task.

Read the contents of the file

The first thing I need to do is to read the contents of the text file. I will store the contents in a variable that I can use for later processing. The good thing is that Windows PowerShell makes it really easy to read the contents of a text file—I simply use the Get-Content cmdlet and specify the text file.

The following text file is basically one really long line. I did not add any carriage returns or line feeds in the file. Here is what it looks like in Notepad:

Image of text

I then store the returned contents in a variable:

$text = Get-Content -Path C:\fso\ATextFile.txt

The next thing I do is initialize my position holder variable. I need to know where I am at in the file contents, so I use the $i variable to do this. Because I want to start at the beginning, I use 0 for the initial value. Later, I will increment the value by 140 each time I loop through the contents of the text. Here is the initialization:

$i = 0

While means "as long as"

As long as the value of the $i variable is less than the length of the text file (minus 140), I want to keep reading from the text. To do this sort of "as long as" ("something is true" kind of thing), I use the While statement. The While statement consists of a condition that is evaluated and a script block that is executed.

I create the condition to evaluate the position in the text, and then I use the script block to retrieve a chunk of 140 characters from my current position in the text. Then I increment the value of the $i variable by 140:

While ($i -le ($text.length-140))

 {

  $text.Substring($i,140)

  $i += 140

 }

What about leftovers?

It should be pretty obvious that I would be extremely lucky if my text came out to be an exact multiple of 140 characters. (What would that be? One chance in a hundred and forty?) Anyway, what I need to do is take the position of $i and display the remaining text. If I do not specify the second number (that is the amount of text to retrieve), SubString will begin at the position and return everything. So in this way, I can get the remainder text. This is shown here:

$text.Substring($i)

Here is the complete script:

$text = Get-Content -Path C:\fso\ATextFile.txt

$i = 0

While ($i -le ($text.length-140))

 {

  $text.Substring($i,140)

  $i += 140

 }

$text.Substring($i)

The script and the output from the script are shown in the following image:

Image of command output

BG, that is all there is to using Windows PowerShell to break a text file into chunks. Join me tomorrow when I will talk about more way cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon