Welcome › Forums › General PowerShell Q&A › Powershell String
- This topic has 3 replies, 3 voices, and was last updated 4 months, 2 weeks ago by
Participant.
-
AuthorPosts
-
-
September 2, 2020 at 2:32 pm #254195
I’m new to powershell, so bear with me. I’m trying to do something I thought would be easy and common: find file paths that are “too long” (greater than some value. i.e. 260 character). The below code runs fine, but while I can either output the file paths or and I can pipe to Select-Object and get the string length, I can’t figure out how to have them both. Get-Member shows $longpathfiles as a ‘System.String’ type, with the only property being ‘Length’. What I want to end up with is a 2 column table. ‘PathLength’, and ‘Path’ I can pipe to Out-Gridview or csv. So….what am I missing? Thanks in advance.
PowerShell1$longpathfiles = robocopy . NULL /L /s /njh /njs /fp /ns /nc /ndl | where length -gt 260 | foreach { $_.trim() } -
September 2, 2020 at 3:52 pm #254234
Almost there… You just need to create a PSCustomObject foreach paths with the path name and length
PowerShell12345678$longpathfiles = robocopy . NULL /L /s /njh /njs /fp /ns /nc /ndl | Where-Object length -gt 260 | ForEach-Object -Process {if ($Null -ne $_.trim()) {[PSCustomObject]@{Path = $_.trim()Length = $_.Trim().Length}}} -
September 2, 2020 at 6:18 pm #254285
Thanks for the quick reply. This works great.
But question though, why test against $NULL if I’m already filtering out anything with less than 260 character?
-
September 4, 2020 at 2:44 pm #254636
You could have lines with 261 white spaces. if ($_.Trim()) should suffice though.
-
-
AuthorPosts
- The topic ‘Powershell String’ is closed to new replies.