Search code examples
windowspowershellbatch-filerenamefilenames

Rename files in a folder using part of text of original name (batch file, powershell, other solution)


I have a folder with more files to rename.

I need to move a part of original name in another position.

al part in filename are separated from " - " (first part - thirth part - second part.gif)

All files use the same structure.

My original files:

111111111 - 333333333 - 2222222222.txt
AAAA AAA - CCC CCCC - BBB BBB.mp3
1home - 3home - 2home.jpg

Espected result:

111111111 - 2222222222 - 333333333.txt
AAAA AAA - BBB BBB - CCC CCCC.mp3
1home - 2home - 3home.jpg

All file name are composed 3 part i need to move second part with third part

Extension must not be changed keep original extension.

Any script, software to do this will be appreciated.

I tryed this Powershell code but seems not working

Set-Location C:\pathHere\

Get-ChildItem | ForEach-Object {
    $artist,$album,$song = $_.BaseName.Split(" - ")
    Rename-Item $_ -NewName ($artist + " - " + $song + " - "  + $album + $_.Extension)
}

Solution

  • Okay, I don't really like to answer questions where no batch file coding effort has been shown by the OP when using its tag, but here's a possible solution nonetheless:

    @Echo Off&SetLocal EnableExtensions DisableDelayedExpansion
    For %%G In ("* - * - *.*")Do (Set "BaseNameBefore=%%~nG"
        SetLocal EnableDelayedExpansion&For /F "Tokens=1-4 Delims=?" %%I In (
            "!BaseNameBefore: - =?!")Do EndLocal&If "%%L" == "" If Not %%K == "" (
            Ren "%%G" "%%I - %%K - %%J%%~xG"))
    

    As no coding effort was shown, I'll leave you to break down how the code works yourself, without my explanation.