Jump to content

CMD Rename file name after last dash.

sav99

Hello I am new here,

 

I have found how to rename a file in the command prompt :

 

rename  d:\folder\Artist - Song - Description.mp3 Artist - Song.mp3


I was also able to figure out how to make a for loop, in this example it just prints the name

for %I in (*.mp3) DO ECHO %I

 

I have files that are in this format :

Artist - Song - description.mp3

Artist Song - description.mp3

What I would like to have :

 

 

Artist - Song.mp3

Artist Song.mp3

 

So the logic for the code should be Remove/trim off the name after the last dash withoud the extension.
Could someone please pinpoint me in the right direction thanks! 

Link to comment
Share on other sites

Link to post
Share on other sites

Not sure on doing it through CMD/Bash but fairly simple (albeit messy) through a powershell script:

 

$path1 = "D:\test\temp"

$songs = Get-ChildItem $path1 *.mp3
Write-Host "before"
$songs

foreach ($song in $songs) {
    $name = $song.Name
    if ($name -match '-') {
        $name = $name.Substring(0, $name.lastIndexOf('-'))
        $name = $name.Trim()
        $name = $name + ".mp3"
        Rename-Item $song.FullName -NewName $name
    }
    else {}
}

Write-Host "after"
$songs = Get-ChildItem $path1 *.mp3

$songs

 

EDIT: Amended so it doesn't rename things that don't have a hyphen.

 

Example of it working:

Mode                 LastWriteTime         Length Name                                                                                                     
----                 -------------         ------ ----                                                                                                     
-a----        22/01/2021     02:34              0 Artist - song - desc.mp3                                                                               
-a----        22/01/2021     02:34              0 Artist Song 2 - descc.mp3                                                                             
-a----        22/01/2021     02:34              0 Artist Song 3.mp3                                                                                                                                                                                                                                                                                       
after
-a----        22/01/2021     02:34              0 Artist - song.mp3                                                                                     
-a----        22/01/2021     02:34              0 Artist Song 2.mp3                                                                                     
-a----        22/01/2021     02:34              0 Artist Song 3.mp3  

 

EDIT 2: Some formating

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×