![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | Re: Converting Get-ChildItems to string for processing. Thanks Shay, That's got it. |
My System Specs![]() |
![]() |
| 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 | |||