none
Removing Leading spaces from Folder Names RRS feed

  • Question

  • I have a bunch of folder many layers deep many of which have a leading space in the name - ask a mac user why they would do this cos I don't know.

    Anyway I need to get rid of the leading spaces so I tried this

    {
                If($folder.name.length -ne $folder.name.Trim().length) 
                {
            Rename-Item -Path $folder.name $folder.name.Trim()
    }

    But this doesn't work.

    It recognizes that the length of the folder names are different  so can distinguish between "Archive" and " Archive" but will not change and the error give n is that " Archive" does not exist.

    I'm new to Powershell so I think I'm in over my head.

    Please help   

    Wednesday, October 3, 2018 1:55 PM

Answers

  • Could you please format your code as code here in the forum?
    Get-ChildItem -Path 'G:\Mac Folders' -Directory -Recurse |
        ForEach-Object {
            If($_.Name.Length -gt $_.Name.Trim().Length) {
                Rename-Item -Path $_.FullName -NewName $_.Name.Trim()
            }
        }
    This code works in my environment.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, October 3, 2018 11:00 PM

All replies

  • If you carefully read the help for Rename-Item you will see that the cmdlet needs at least 2 parameters - the name and new name. You only provide one of them.  ;-)

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Wednesday, October 3, 2018 2:06 PM corrected typo
    Wednesday, October 3, 2018 2:05 PM
  • {
                If($folder.name.length -ne $folder.name.Trim().length) 
                {
            Rename-Item -Path $folder.name -NewName $folder.name.Trim()
    }

    Comes up with exactly the same error,  Cannot rename because item at " Archive" does not exist.

    It does not seem to recognize the folder with the leading space

    Wednesday, October 3, 2018 5:19 PM
  • Could you please format your code as code here in the forum? Thanks.

    I assume you do not change the current location to the location of the folder - then you should specify the complete path of the item you want to rename.

    If($folder.name.length -ne $folder.name.Trim().length) {
        Rename-Item -Path $folder.FullName -NewName $folder.Name.Trim()
    }



    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, October 3, 2018 6:25 PM
  • Tried that and it gives me exactly the same error except now it shows the full folder path when it says the item does not exist
    Wednesday, October 3, 2018 6:33 PM
  • Have you tried with -LiteralPath?
    Wednesday, October 3, 2018 6:42 PM
  • Try renaming the folder using the short 8.3 name. See https://code.adonline.id.au/short-paths-via-powershell/
    Wednesday, October 3, 2018 6:46 PM
  • Do you rename more than one folder? Did you check that the folder exists?

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, October 3, 2018 6:48 PM
  • I have hundreds - possibly thousands- of folders which require the name change which is why I'm trying to it it in powershell rather than just manually.

    So I have

    $folders = Get-ChildItem -Path “G:\Mac Folders” -Recurse -Directory
    foreach ($folder in $folders)

    {
               If($folder.name.length -ne $folder.name.Trim().length) {
        Rename-Item -Path $folder.FullName -NewName $folder.Name.Trim()

    }
    }

    The errors I am receiving are only on the folders with a leading space so the folders do exist 

       
    Wednesday, October 3, 2018 9:15 PM
  • Could you please format your code as code here in the forum?
    Get-ChildItem -Path 'G:\Mac Folders' -Directory -Recurse |
        ForEach-Object {
            If($_.Name.Length -gt $_.Name.Trim().Length) {
                Rename-Item -Path $_.FullName -NewName $_.Name.Trim()
            }
        }
    This code works in my environment.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, October 3, 2018 11:00 PM
  • Thank you - that seems to have worked beautifully!
    Thursday, October 4, 2018 7:56 AM
  • Leading spaces in names is a problem. Such objects can be difficult to find. I have found that PowerShell filters similar to below work:

    -Filter {Name -Like "\20*"}

    where "\20" is the ASCII hex representation of the space character. My findings in this Wiki article:

    https://social.technet.microsoft.com/wiki/contents/articles/29168.active-directory-leading-spaces-in-names.aspx


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Thursday, October 11, 2018 12:46 PM
    Moderator