Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Re: HOW TO: Modify the way a file list looks.

Reply
 
Old 12-09-2007   #1 (permalink)
Kiron


 
 

Re: HOW TO: Modify the way a file list looks.

Hi Extech, PowerShell is pretty versatile. I tweaked your function to demonstrate a few of PowerShell features: the -f operator, Get-ChildItem's -filter and <[IO.DirectoryInfo] & [IO.FileInfo]>'s PSIsContainer extended property.

The syntax of the -f operator is:
{index[,alignment][:formatString]} -f listOfValues

You can boost performance by having Get-ChildItem filter the name of the items before piping them. Also, filtering against PSIsContainer is faster than wildcard --or regEx-- comparison against Mode.

Try this:
(measure-command {ls | ? {!$_.psIsContainer}}).totalMilliseconds
(measure-command {ls | ? {$_.mode -notLike 'd*'}}).totalMilliseconds
(measure-command {ls -r| ? {!$_.psIsContainer}}).totalMilliseconds
(measure-command {ls -r| ? {$_.mode -notLike 'd*'}}).totalMilliseconds

function lsf_ {
# set a default value for a parameter
param([String]$first = '*')
$a=@{Name = 'Attrib'; Expression = {$_.Mode}}
# use the -f operator to format strings
$b=@{Name = "{0,-20}" -f 'LastWriteTime';
Expression = {"{0,20:MMM-dd-yyyy hh:mm tt}" -f $_.LastWriteTime}}
$c=@{Name = "{0,15}" -f 'FileSize';
Expression = {"{0,12} KB" -f [math]::round($_.Length/1KB,2)}}
# use Get-ChildItem's -filter to boost performance,
# items are filtered before they are sent to the pipe
ls . -filter $first |
# to list only files use PSIsContainer, it returms a
# boolean value and also boosts performance
? {!$_.PSIsContainer} |
select-object $a, $b, $c, Name | Format-Table -AutoSize
}

# compare performance
(measure-command {lsf *com*}).totalMilliseconds
(measure-command {lsf_ *com*}).totalMilliseconds

--
Kiron

My System SpecsSystem Spec
Old 12-09-2007   #2 (permalink)
eXtech


 
 

Re: HOW TO: Modify the way a file list looks.

Kiron,

Thanks for the assist. I knew someone would have a better way of doing
things. You do. I implemented your tweaks and discovered some items to read
up on. Of course, that will be a miracle in itself. I haven't been able to
find the ultimate resource.

My ideal goal would to be able to do everything ls does and only affect the
output without having to monkey around with XML and Update-FormatData
(probably the easiest way).

For example: Return a collection of files with a .LastWriteTime greater than
some date. Currently, my function is very limited.
My System SpecsSystem Spec
Old 12-09-2007   #3 (permalink)
Kiron


 
 

Re: HOW TO: Modify the way a file list looks.

You're welcome Extech. PowerShell has special Functions known as Filters. The main difference is that Filters process the current object in the pipe and Functions have to wait for the $input (an automatic variable which contains all the piped objects) to then begin to process each object. You can read about Functions and Filters by typing:
help about_function

# here are a few simple, but useful, filters:

filter dirs {
if ($_.psIsContainer) {$_}
}

filter files {
if (!$_.psIsContainer) {$_}
}

filter daysOld ([single]$days = 0.0) {
if ($_.lastWriteTime -gt (get-date).addDays(-$days)) {$_}
}

# this is your custom ls output format:

$lsFormat = @{Name = 'Attrib'; Expression = {$_.Mode}},
@{Name = "{0,-20}" -f 'LastWriteTime';
Expression = {"{0,20:MMM-dd-yyyy hh:mm tt}" -f $_.LastWriteTime}},
@{Name = "{0,15}" -f 'FileSize';
Expression = {"{0,12} KB" -f [math]::round($_.Length/1KB,2)}},
'Name'

# after adding these Filters and $lsFormat to your $Profile
# --or to the Global scope-- you could then use Get-ChildItem,
# the filters and your custom format like this
ls | daysOld 1.5 | files | select $lsFormat | ft -auto
ls | files | select $lsFormat | ft -auto
ls | dirs | select $lsFormat | ft -auto

If you're looking for books on PowerShell I would recommend Windows PowerShell In Action by Bruce Payette to learn the ins, outs and whys of PowerShell, then read Windows PowerShell Cookbook by Lee Holmes to learn cool scripting techniques from a plethora of real solutions; his book also has many handy reference tables.
Another good source of information on PowerShell are the many blogs. Brandon Shell has this PowerShell Blog Search that is pretty useful:
http://bsonposh.com/modules/wordpress/?page_id=13
Also, I find this NewsGroup very beneficial, lots of great minds willing to help and share.

--
Kiron
My System SpecsSystem Spec
Old 12-09-2007   #4 (permalink)
eXtech


 
 

Re: HOW TO: Modify the way a file list looks.

Awesome stuff, Kiron.

Thanks for the help. I guess I had to muddle through all of the other
material to get this far. Now, I realize I did way more than needed.
Properly using custom formats and custom filters will make things easier.

Thanks.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How can I see, and modify a list of blocked e-mail addresses, in Junk Mail Vista mail
Modify the hosts file Vista General
Modify a text file PowerShell
Modify XML file PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46