Wednesday, September 13, 2006

I have some large text files that I want to compress.  The key is that I want to keep them as separate files.  Let's use a little PowerShell scripting to keep this down to one line:

foreach ($file in ls *.txt ) {  & 'C:\Program Files\7-Zip\7z.exe' a ($file.name + '.zip') $file.name }

I'm using a simple foreach construct built into PowerShell.  I'm saying get all files in this directory that have a .txt extension and then iterate through those files executing whatever is between the curly brackets.  In this case it is the command to use 7Zip to compress the file into a new file named the same as the original file with the .zip extension tacked on the end.

 

Wednesday, September 13, 2006 12:08:23 PM (Central Standard Time, UTC-06:00)  #    Comments [0]
 Friday, June 30, 2006

I was in the process of downloading Cygwin to in order to get the wc command (among others) in order to do some file verification work.  I stopped for a second and thought why not use PowerShell?  I found a good source for this so here is the command:

PS C:\Scripts\AMA> gc test.134.* | Measure-Object

Count    : 395587
Average  :
Sum      :
Maximum  :
Minimum  :
Property :
Notice that I'm using gc as an alias for Get-Content.  gc, type and cat all work as aliases for Get-Content.  
test.134.* is just a typical DOS file mask to get all files that start with test.134.
Now let's get a word count:
PS C:\Scripts\AMA> gc test.134.23.00 |  Measure-Object -word
Lines                             Words Characters          Property
-----                             ----- ----------          --------
                                   8391
How about a character count:
PS C:\Scripts\AMA> gc test.134.23.00 |  Measure-Object -char
Lines               Words                        Characters Property
-----               -----                        ---------- --------
                                                    1670774
How about an explicit line count:
PS C:\Scripts\AMA> gc test.134.23.00 |  Measure-Object -line
              Lines Words               Characters          Property
              ----- -----               ----------          --------
               8391
Keep in mind that Measure-Object can also be used with objects but we'll save that for another post.  Also it might be nice to try to create a nice short alias like wc to save me some typing.
Friday, June 30, 2006 1:40:55 PM (Central Standard Time, UTC-06:00)  #    Comments [1]