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 - Converting Get-ChildItems to string for processing.

Reply
 
Old 02-01-2008   #1 (permalink)
rush


 
 

Converting Get-ChildItems to string for processing.

Why, when I use Get-ChildItem, can I not use the Array of files
returned as a text array.

I want to pass the filename to function to process the strings of the
filenames.!

Basically, in the sample script below how would I assign the $aFile to
a variable I could use as a string!?

Thanks in advance,
Russ
#############################################################################

$MonitorDir="C:\PowerShell\TEST"

#### FUNCTIONS ####
function ReadSubDirectories ([string]$InLine){
Get-ChildItem -Recurse -Path $InLine | Where {$_.psIsContainer -eq
$true}
}
function ReadDirContents ([string]$InLine){
Get-ChildItem -Path $InLine | format-list -property name
}

$SubDirs = ReadSubDirectories $MonitorDir
foreach ($Sub in $SubDirs ){
$FileList = ReadDirContents $Sub
foreach ($aFile in $FileList){
#$aFile
"File " + $aFile
}
}

########################################################
I get :
File Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
File Microsoft.PowerShell.Commands.Internal.Format.GroupStartData
File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
File Microsoft.PowerShell.Commands.Internal.Format.GroupEndData
File Microsoft.PowerShell.Commands.Internal.Format.FormatEndData

My System SpecsSystem Spec
Old 02-01-2008   #2 (permalink)
Shay Levi


 
 

Re: Converting Get-ChildItems to string for processing.

Hi

Remove the '... format-list -property name' in 'ReadDirContents' function

format-* cmdlets should be used as the last cmdlet in the pipeline, they are
inteneded to format objects to strings and disply them in the console.

BTW, '$_.psIsContainer' will always evaluate to $true, and so you can safely
write:

Where {$_.psIsContainer}

Instaed of:

Where {$_.psIsContainer -eq $true}




-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Why, when I use Get-ChildItem, can I not use the Array of files
> returned as a text array.
>
> I want to pass the filename to function to process the strings of the
> filenames.!
>
> Basically, in the sample script below how would I assign the $aFile to
> a variable I could use as a string!?
>
> Thanks in advance,
> Russ
> ######################################################################
> #######
> $MonitorDir="C:\PowerShell\TEST"
>
> #### FUNCTIONS ####
> function ReadSubDirectories ([string]$InLine){
> Get-ChildItem -Recurse -Path $InLine | Where {$_.psIsContainer -eq
> $true}
> }
> function ReadDirContents ([string]$InLine){
> Get-ChildItem -Path $InLine | format-list -property name
> }
> $SubDirs = ReadSubDirectories $MonitorDir
> foreach ($Sub in $SubDirs ){
> $FileList = ReadDirContents $Sub
> foreach ($aFile in $FileList){
> #$aFile
> "File " + $aFile
> }
> }
> ########################################################
> I get :
> File Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
> File Microsoft.PowerShell.Commands.Internal.Format.GroupStartData
> File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> File Microsoft.PowerShell.Commands.Internal.Format.GroupEndData
> File Microsoft.PowerShell.Commands.Internal.Format.FormatEndData

My System SpecsSystem Spec
Old 02-01-2008   #3 (permalink)
Shay Levi


 
 

Re: Converting Get-ChildItems to string for processing.



Russ,

If I get you, you want to get all files recursively from a starting root
directory. If so, you can replace
your code with a one-liner:

Get-ChildItem -Recurse -Path <path> | Where {!$_.psIsContainer} | foreach
{$_.fullname}

Notice that now 'Where {!$_.psIsContainer}' evaluates to $false since you
don't need to process directoryInfo objects.
Another notation for the above is 'Where {-not $_.psIsContainer}' which also
evaluates to $false.



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Why, when I use Get-ChildItem, can I not use the Array of files
> returned as a text array.
>
> I want to pass the filename to function to process the strings of the
> filenames.!
>
> Basically, in the sample script below how would I assign the $aFile to
> a variable I could use as a string!?
>
> Thanks in advance,
> Russ
> ######################################################################
> #######
> $MonitorDir="C:\PowerShell\TEST"
>
> #### FUNCTIONS ####
> function ReadSubDirectories ([string]$InLine){
> Get-ChildItem -Recurse -Path $InLine | Where {$_.psIsContainer -eq
> $true}
> }
> function ReadDirContents ([string]$InLine){
> Get-ChildItem -Path $InLine | format-list -property name
> }
> $SubDirs = ReadSubDirectories $MonitorDir
> foreach ($Sub in $SubDirs ){
> $FileList = ReadDirContents $Sub
> foreach ($aFile in $FileList){
> #$aFile
> "File " + $aFile
> }
> }
> ########################################################
> I get :
> File Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
> File Microsoft.PowerShell.Commands.Internal.Format.GroupStartData
> File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> File Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> File Microsoft.PowerShell.Commands.Internal.Format.GroupEndData
> File Microsoft.PowerShell.Commands.Internal.Format.FormatEndData

My System SpecsSystem Spec
Old 02-01-2008   #4 (permalink)
rush


 
 

Re: Converting Get-ChildItems to string for processing.

Thanks Shay,
That's got it.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Converting String Pairs to IDictionary? .NET General
Converting date time format to string format PowerShell
end of line while processing a string token issue PowerShell
are there tutorial on string processing with power-shell? PowerShell
Encountered end of line while processing a string token 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