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 - path to item returned from get-childitem

Reply
 
Old 06-07-2007   #1 (permalink)
Mike Blake-Knox


 
 

path to item returned from get-childitem

I'm having difficulty extracting the path to a file from the output of
get-childitem. If I run "test.ps1" by entering .\test where test.ps1
contains:
foreach ($childx in $(get-childitem "test" -recurse -filter "*.txt"))
{
echo("childx=" + $childx)
echo ($childx)
$child =resolve-path $childx
echo ("child=" + $child)
}
with location set to a directory with a single .txt file, I get the
following output:

>> .\test

childx=first.txt

Directory:
Microsoft.PowerShell.Core\FileSystem::C:\prof\computing\experimenting\P
owerShell\test

Mode LastWriteTime Length Name

---- ------------- ------ ----

-a--- 6/6/2007 4:57 PM 6 first.txt

child=

80#

as $child has a null value.

How should I get the path?

Thanks

Mike



My System SpecsSystem Spec
Old 06-07-2007   #2 (permalink)
Keith Hill [MVP]


 
 

Re: path to item returned from get-childitem

"Mike Blake-Knox" <mikebk@nospam.nospam> wrote in message
news:VA.000000af.22c42243@nospam.nospam...
> I'm having difficulty extracting the path to a file from the output of
> get-childitem. If I run "test.ps1" by entering .\test where test.ps1
> contains:
> foreach ($childx in $(get-childitem "test" -recurse -filter "*.txt"))
> {
> echo("childx=" + $childx)
> echo ($childx)
> $child =resolve-path $childx
> echo ("child=" + $child)
> }


Will this work for you?

foreach ($childx in $(get-childitem "test" -recurse -filter "*.txt"))
{
echo("childx=" + $childx)
echo ($childx)
$child = $childx.fullname
echo ("child=" + $child)
}

Essentially you would tend to use Resolve-Path on a string like .\*.txt but
$childx is already a rich FileInfo *object* that contains lots of useful
information like the path via the Fullname property.

--
Keith

My System SpecsSystem Spec
Old 06-07-2007   #3 (permalink)
IJuan


 
 

Re: path to item returned from get-childitem

Or are you just looking for the directory that the file is in?

....
$child = $childx.directoryname
....

-= IJuan =-

"Keith Hill [MVP]" wrote:

> "Mike Blake-Knox" <mikebk@nospam.nospam> wrote in message
> news:VA.000000af.22c42243@nospam.nospam...
> > I'm having difficulty extracting the path to a file from the output of
> > get-childitem. If I run "test.ps1" by entering .\test where test.ps1
> > contains:
> > foreach ($childx in $(get-childitem "test" -recurse -filter "*.txt"))
> > {
> > echo("childx=" + $childx)
> > echo ($childx)
> > $child =resolve-path $childx
> > echo ("child=" + $child)
> > }

>
> Will this work for you?
>
> foreach ($childx in $(get-childitem "test" -recurse -filter "*.txt"))
> {
> echo("childx=" + $childx)
> echo ($childx)
> $child = $childx.fullname
> echo ("child=" + $child)
> }
>
> Essentially you would tend to use Resolve-Path on a string like .\*.txt but
> $childx is already a rich FileInfo *object* that contains lots of useful
> information like the path via the Fullname property.
>
> --
> Keith
>
>

My System SpecsSystem Spec
Old 06-08-2007   #4 (permalink)
Mike Blake-Knox


 
 

Re: path to item returned from get-childitem

In article <u90jsgRqHHA.4132@TK2MSFTNGP05.phx.gbl>, Keith Hill [MVP] wrote:
> Essentially you would tend to use Resolve-Path on a string like .\*.txt but*
> $childx is already a rich FileInfo *object* that contains lots of useful*
> information like the path via the Fullname property.


Thanks!

I'd actually tried get-member to find which member to use but kept having a
null return. I should have used the -inputObject parameter.

My System SpecsSystem Spec
Old 06-08-2007   #5 (permalink)
Josh


 
 

Re: path to item returned from get-childitem

On Jun 8, 7:15 am, Mike Blake-Knox <mik...@nospam.nospam> wrote:
> In article <u90jsgRqHHA.4...@TK2MSFTNGP05.phx.gbl>, Keith Hill [MVP] wrote:
> > Essentially you would tend to use Resolve-Path on a string like .\*.txt but
> > $childx is already a rich FileInfo *object* that contains lots of useful
> > information like the path via the Fullname property.

>
> Thanks!
>
> I'd actually tried get-member to find which member to use but kept having a
> null return. I should have used the -inputObject parameter.


You can also use:

split-path $x -parent

~Josh

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Get-ChildItem Path Limitation PowerShell
Get-ChildItem : Access to the path c:\asd is denied. PowerShell
Counting items returned from Get-ChildItem.... PowerShell
Get-ChildItem vs. Get-Item output PowerShell
Inconsistent types returned from get-childitem 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