Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Pick-File

Closed Thread
 
Thread Tools Display Modes
Old 05-25-2007   #1 (permalink)
hecks@hotmail.co.uk
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

Old 05-25-2007   #2 (permalink)
Kiron
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
Old 05-26-2007   #3 (permalink)
hecks@hotmail.co.uk
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

Old 05-26-2007   #4 (permalink)
hecks@hotmail.co.uk
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

Old 05-26-2007   #5 (permalink)
Kiron
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
Old 05-27-2007   #6 (permalink)
hecks@hotmail.co.uk
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

Closed Thread

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








Vistax64.com 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 2005-2008

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 47 48 49 50