Best way to replace characters in file names

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Olaf Soyk Olaf Soyk 2 weeks, 1 day ago.

  • Author
    Posts
  • #72335
    Profile photo of Kevin Greer
    Kevin Greer
    Participant

    I had to rename a bunch of files in a directory that included sub folders to remove any of these characters ',()&.' and replace any spaces with a '_'. The below ran fine but I'm still learning and wondering if there was a better way of doing this.

    $filelist = get-childitem -Path '\\Server\Share\Sub Folder' -Recurse -File
    $filelist | % {Rename-Item -Path $_.fullname -NewName ($_.BaseName.replace('(','').replace(')','').replace(',','').replace('','_').replace('&','').replace('.','') + $_.extension)} 
    

    The only issue with this is if a file name has more than one space together or already has a underscore and space next to it the name could contain multiple underscore characters in a row (File_ Name.file would rename to File__Name.file). I was hoping to figure out a regex solution to prevent multiple underscores together but just came up with this logic. Than you in advance for your thoughts!

    $strings = 'test 1', 'test  2', 'test   3', 'test_    4', 'test _ _ _ _ _ 5'
    $strings
    foreach($string in $strings){
      $string = $string.replace(' ','_')
      while ($string -like '*__*'){
        $string = $string.Replace('__','_')
      }
      $string
    }
    
  • #72343
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    If it does the job .... 😉
    But to make your code a little more sleak or sophisticated you could use another regex pattern like this:

    'A strange (file) name, like,.this & that,.' -replace '(\s|\(|\)|,|\.|&|_)+','_' 
  • #72344
    Profile photo of Kevin Greer
    Kevin Greer
    Participant

    Amazing. Thank you! I did not know how to format the regex so I just did a bunch of replace() methods. I modified your regex, and test string, to get the exact result I needed.

    '_  ____A strange (file) name, like,.this & that,._' -replace '(\(|\)|,|\.|&)+','' -replace '(_|\s)+','_'
    
    • #72347
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      If I got it right this could be even better ... 😉

      '_  ____A strange (file) name, like,.this & that,._' -replace '(\(|\)|,|\.|&)+','' -replace '(_|\s)+','_' -replace '^_|_$',''
  • #72349
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Here's another option.

    ('_  ____A strange (file) name, like,.this & that,._' -replace '[_(),.&]+','').trim() -replace '\s+','_'

You must be logged in to reply to this topic.