![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Pick-File I often find that I need a quick way of browsing directory contents and picking a single file from the list, but without all the typing. This function works somewhat, but I was wondering if I was missing a better way of doing it: function Pick-File ($file, $index) { $file = get-childitem $file if ($index -ne $null) {return $file[$index]} else {$file | %{$i=0}{write $_; $i++} | format-table @{label="Index";expression={$i}}, Name, @{label="Path";expression={$_.directoryname}} -auto} } All this does is return the directory contents against a list of index numbers; these can then be added as a second parameter to get the file object, e.g. 'pick-file c:\*' to view, then 'pick-file c:\* 12' to select file number 12 in the list. -Hecks |
| | #2 (permalink) |
| Guest | RE: Pick-File I tweaked your function a little. This way it gets only files, including hidden ones. And when you pass an index it lists all the file's properties. Since the function targets one directory, the Path is shown as a header. function Pick-File ($file, $index) { $file = get-childitem $file -force | ? {!$_.PSIsContainer} if ($index -ne $null) {return $file[$index] | fl *} else {$file[0].directoryName; $file | %{$i=0}{write $_; $i++} | ft @{label="Index";expression={$i}}, Name -auto | more} } -- Kiron |
| | #3 (permalink) |
| Guest | Re: Pick-File On May 26, 3:15 am, Kiron <K...@discussions.microsoft.com> wrote: > I tweaked your function a little. This way it gets only files, including > hidden ones. And when you pass an index it lists all the file's properties. > Since the function targets one directory, the Path is shown as a header. > > function Pick-File ($file, $index) { > $file = get-childitem $file -force | ? {!$_.PSIsContainer} > if ($index -ne $null) {return $file[$index] | fl *} > else {$file[0].directoryName; $file | %{$i=0}{write $_; $i++} | ft > @{label="Index";expression={$i}}, Name -auto | more}} > > -- > Kiron Thanks, that's much better. Only the last part doesn't work for me, as I just need the file object to be returned rather than a formatted list. I've just noticed that it also accepts multiple indices as args, e.g. $f = pick-file C:\* 10,11,12. Also, if directories aren't excluded, the function can be nested to browse and select from folders: pick-file (pick-file c:\* 1) 15. But maybe that's getting too fiddly, and your solution is a lot neater. -Hecks |
| | #4 (permalink) |
| Guest | Re: Pick-File On May 26, 3:15 am, Kiron <K...@discussions.microsoft.com> wrote: > I tweaked your function a little. This way it gets only files, including > hidden ones. And when you pass an index it lists all the file's properties. > Since the function targets one directory, the Path is shown as a header. > > function Pick-File ($file, $index) { > $file = get-childitem $file -force | ? {!$_.PSIsContainer} > if ($index -ne $null) {return $file[$index] | fl *} > else {$file[0].directoryName; $file | %{$i=0}{write $_; $i++} | ft > @{label="Index";expression={$i}}, Name -auto | more}} > > -- > Kiron And here's another variant that has some interesting results: 'pick- file c:\' enables folder selection, but 'pick-file c:\*' lists subfolders and subfolder contents in an alphabetized list - any of these can be selected, too, with an optional -force switch for including hidden items. function Pick-File ($file, $index,[switch]$force) { if ($force) {$file = (get-childitem ("" + $file + "\*") -force | sort-object @{expression={$_.PSIsContainer}; Descending=$true}, @{expression={$_.Name}; Descending=$false})} else {$file = (get-childitem ("" + $file + "\*") | sort-object @{expression={$_.PSIsContainer}; Descending=$true}, @{expression={$_.Name}; Descending=$false})} if ($index -ne $null) {return $file[$index]} else {$file | %{$i=0}{write $_; $i++} | ft @{label="Index";expression={$i}},Name, #@{label="File Path";expression={$_.directoryname}}, @{label="Path";expression={if (!($_.directoryname)) {"<Dir> " + $_.fullname} else {$_.directoryname}}} -auto | more} } -Hecks |
| | #5 (permalink) |
| Guest | Re: Pick-File Yes, it is quite efficient. Thanks for the tip on the '\*', but when no arguments are past, Pick-File will always list the contents of the root directory of the current drive, ignoring the present working directory. To avoid this, pass a '.' at least. Also, notice that you can use the range operator, but it has to be inside parenthesis, like so: pick-file . -fo (736..723) -- Kiron |
| | #6 (permalink) |
| Guest | Re: Pick-File On May 27, 5:34 am, Kiron <K...@discussions.microsoft.com> wrote: > Yes, it is quite efficient. Thanks for the tip on the '\*', but when no > arguments are past, Pick-File will always list the contents of the root > directory of the current drive, ignoring the present working directory. To > avoid this, pass a '.' at least. Also, notice that you can use the range > operator, but it has to be inside parenthesis, like so: > > pick-file . -fo (736..723) > -- > Kiron Nice tips! Thanks. Here's a final cleaned-up version: function Pick-File ($file, $index, [switch]$force) { if ($force) {$switch = "-force"} $file = (invoke-expression "get-childitem '$file\*' $switch" | sort-object @{expression={$_.PSIsContainer}; Descending=$true}, @{expression={$_.Name}; Ascending=$true}) if ($index -ne $null) {return $file[$index]} else {$file | %{$i=0} {$_; $i++} | format-table @{label="Index"; expression={$i}}, Name, @{label="Path"; expression={ if ($_.PSISContainer) {"<Dir> $_"} else {$_.directoryname}}} -auto | more} } -Hecks |
| |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| can't pick up e-mail | AlexLiz | Vista mail | 1 | 01-11-2008 06:39 PM |
| pick your unbuntu poison... | Frank | Vista General | 32 | 06-30-2007 12:51 AM |
| How To Pick Winning Stocks | info@stockinvesting.com | Avalon | 0 | 06-15-2006 03:32 AM |