![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||