Search code examples
powershellrenamemovebulksuffix

Bulk Move Sequential Numbers (Suffix) - Images, JPEG, RAW etc


I need to move a sequential number or sometimes a random ID with letters.

As an example:

Australia_Brisbane_NP_©_Hello_World_1163  
Australia_Brisbane_NP_©_Hello_World_1164  
Australia_Brisbane_NP_©_Hello_World_1165  
Australia_Brisbane_NP_©_Hello_World_AHDFGF
Australia_Brisbane_NP_©_Hello_World_GHRADQ
Australia_Brisbane_NP_©_Hello_World_QGASFS

What I need to do is have ©_Hello_World at the end and move the ID behind the ©, Example below:

Australia_Brisbane_NP_1165_©_Hello_World  
Australia_Brisbane_NP_AHDFGF_©_Hello_World

The ideal script would allow me to specify between 1-15 characters at the end of the word without effecting the extension and move the 1-15 characters behind .

I have tried searching for a lot of different scripts however either they do not work or they are too complicated for me to adapt them to what is required.

I am unable to use any external software and as such I have to stick to PowerShell.


Solution

  • The basic "change filename" script is:

    • Get filenames Get-ChildItem -LiteralPath 'C:\Path\To\Files'
    • Pipe the results into Rename-Item
    • Use the property -NewName with a scriptblock {}
    • In the scriptblock, code to calculate the new name from the old name
    • Extract the filename out from the path and the extension
    • Change it
    • Put the path and the extension back on

    I have tried searching for a lot of different scripts however either they do not work or they are too complicated for me to adapt them to what is required.

    Text processing is all about details, details make code more complicated, and small details can invalidate whole approaches.

    It's not at all clear to my why you say:

    The ideal script would allow me to specify between 1-15 characters at the end of the word without effecting the extension and move the 1-15 characters behind _©.

    Why would you benefit from specifying the character count, instead of having the ideal script move "all of them"?

    This script should do it:

    $count = Read-Host -Prompt 'How many characters to move?'
    
    
    Get-ChildItem -LiteralPath 'C:\Path\To\Files' | 
        Rename-Item -NewName { 
    
            $newName = $_.BaseName -replace "(©_Hello_World)_(.{$($count)})", '$2_$1'
    
            "$($_.DirectoryName)/$($newName + $_.Extension)"
    
    }
    

    Nb. it will move the count you asked for, even if there are more characters there.

    If you don't need to specify the count, and just take all to the extension, then replace (.{$($count)}) with (.*), and remove the Read-Host line.