Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming files to remove periods in Powershell

I have a list of files like this:
Test.(file1)[text].txt
Test.(file2)[text].txt
Test.(file3)[text].txt

I need to remove any "()","[]" and any ".", replace them spaces without changing the file extension. I've tried the following but it's not working:

dir C:\scratch\*.txt | % { Rename-Item $_ ($_.basename -replace "\(", " "`
            -replace "\)", " "`
            -replace "\[", " "`
            -replace "\]", " "`
            -replace ".", " ")}

Ideally the file name should end up in the format "Test file1 text.txt"

like image 451
fenster Avatar asked Dec 06 '22 04:12

fenster


2 Answers

IMO Rename-Item appears to be broken in terms of its handling of paths with [ ] chars in them. Fortunately, Move-Item works:

dir *.txt | 
    mi -dest {($_.Basename -replace '[()[\].]+',' ').Trim() + $_.Extension}

Where mi is the alias for Move-Item. Note that a number of cmdlets like Rename-Item and Move-Item can be piped to directly and then their NewName/Destination parameters can take scriptblocks in which you can use the pipeline object. Internally this works for pipeline bound parameters as PowerShell will execute the scriptblock and attempt to coerce the result to the type expected by the parameter.

like image 51
Keith Hill Avatar answered Dec 07 '22 18:12

Keith Hill


Rename-Item doesn't work with files that contain "[" or "]". This is a known issue.

It turns out that the problem is because Rename-Item is missing a -LiteralPath parameter. Move-Item has this parameter, so you can use it to do what you want:

ls C:\scratch\*.txt | 
    % { Move-Item -literalpath $_.fullname `
        ($_.name -replace "[()\[\]]|\.(?!\w{3}$)", " ") -whatif }

Remove the -WhatIf parameter when it looks like it is doing what you want it to do. My regular expression leaves files like "Test  file1  text.txt", which isn't quite what you wanted.

like image 31
Jeff Hillman Avatar answered Dec 07 '22 16:12

Jeff Hillman