Truncating spaces in file names

Welcome Forums General PowerShell Q&A Truncating spaces in file names

This topic contains 4 replies, has 3 voices, and was last updated by

js
 
Participant
2 months, 1 week ago.

  • Author
    Posts
  • #170122

    Participant
    Topics: 3
    Replies: 3
    Points: 43
    Rank: Member

    I am trying to find the files with either leading or trailing spaces. After finding, I would truncate the speces.

    I am trying the following code, but apparently it is not working:

    Get-ChildItem -Recurse -File | where {($_.Name | out-string).Trim().Length -lt ($_.Name | out-string).Length}

    Here the command is running as if there is no where clause at all.

  • #170164

    Participant
    Topics: 1
    Replies: 1552
    Points: 2,698
    Helping Hand
    Rank: Community Hero

    I'd recommend to use a regular expression instead of your "circumstance" 😉

    Get-ChildItem -Recurse -File | 
        Where-Object { $_.BaseName -match '^\s+.*\s+$' }

    ... ooops ... forgot the "truncate part" ... I assume you want to get rid of the leading and trailing spaces at all ... add this:

        |
                ForEach-Object{
                    Rename-Item -Path $_ -NewName $(($_.BaseName -replace '^\s+' -replace '\s+$') + $_.Extension)
                }
  • #170173

    Participant
    Topics: 3
    Replies: 3
    Points: 43
    Rank: Member

    Thanks for the response. However, just for my learning, am I making any mistake in the code snippet, I provided?

    Get-ChildItem -Recurse -File | where {($_.Name | out-string).Trim().Length -lt ($_.Name | out-string).Length} |
    ForEach-Object {Rename-Item -Path $_ -NewName (($_.Name | out-string).Trim())}

     

    • #170365
      js

      Participant
      Topics: 25
      Replies: 692
      Points: 1,719
      Helping Hand
      Rank: Community Hero

      Out-string adds a newline to each name, so that where clause will be true for every name. Trimming it will remove the newline and will be at least one less in length. Out-string is unnecessary in this case. Name is already a string. Out-string is almost never useful.

      'hi there' | out-string | foreach { $_ -replace "`n",'\n' } 
      
      hi there\n
      
      get-childitem | foreach { $_.name.gettype() } 
      
      IsPublic IsSerial Name                                     BaseType
      -------- -------- ----                                     --------
      True     True     String                                   System.Object
      True     True     String                                   System.Object
      True     True     String                                   System.Object
      

      Here's another way:

      get-childitem | where { $_.name -ne $_.name.trim() } | 
        Rename-Item -NewName { $_.name.trim() } -whatif
      
  • #170176

    Participant
    Topics: 1
    Replies: 1552
    Points: 2,698
    Helping Hand
    Rank: Community Hero

    However, just for my learning, am I making any mistake in the code snippet, I provided?

    Did you get the results you expected? If your answer is "No" there seem to be a mistake in your expectations of the results you get from the code you used. To determine the exact output of the code you have to make it visible. Most of the time you can see additional charachters when you output a variable like this "'$($Variable)'" (pay attention to the double and single quotes) ... to compare your results you could use a custom object like this:

    $Result = Get-ChildItem -Recurse -File | 
        ForEach-Object {
            [PSCustomObject]@{
                Name               = $_.Name
                NameString         = "'$($_.Name | out-string)'"
                TrimmedNameString  = "'$(($_.Name | out-string).Trim())'"
                NameLength         = ($_.Name | out-string).Length
                TrimmedNameLength  = ($_.Name | out-string).Trim().Length
                NameLength2        = $_.Name.Length
                TrimmedNameLength2 = $_.Name.Trim().Length
            }
        }
    $Result | Format-Table -AutoSize

    ... as you can see – most of the objects or properties have a length property anyway ... you should be able to use that if you need. You should try to write your code as least complex as possible to provide the result you desire.

You must be logged in to reply to this topic.