1

I have multiple directories with names of artists and within them can be one and many directories with names of albums. Example

Michael Jackson\Invincible (Album)
Michael Jackson\Thriller (Album)
Luciano Pavarotti\'O sole mio (Single)
Queen\Bohemian Rhapsody (Single)

I would like to rename all the folders that have " (Album)" and simply leave them the name of the album.

Leaving the previous example as

Michael Jackson\Invincible
Michael Jackson\Thriller
Luciano Pavarotti\'O sole mio (Single)
Queen\Bohemian Rhapsody (Single)

I have this code but I don't know how to search for folders that contain " (Album)" and delete that.

@echo off
setlocal disableDelayedExpansion

for /d %%A in (*) do (
    set "folder=%%A"
    setlocal enableDelayedExpansion

    REM rename folder

    endlocal
)

The truth is that I've never done anything so complex. If anyone could help me, I'd really appreciate it.

  • 1
    Batch is not the best scripting language to use here. If you use PowerShell it will be a breeze to do. – LPChip Aug 29 at 21:17
  • 1
    I second that. If you're going to have to learn, learn a language that's going to do the job better. – I say Reinstate Monica Aug 29 at 21:44
  • 1
    It would probably be a "breeze" to write that in Perl. It would also be easy to write that in Python. But I'm not going to suggest either option because that is not what the OP is asking for. Zontier, start out with something like "Dir /ad > %temp%\dirlist.txt" this will give you a list of directory names in a temporary file called dirlist.txt. Then write a FOR loop that reads that file, one line at a time. As you read each line use the FINDSTR command to look for the word "album". When you get a hit, execute the RENAME command to rename that folder the same name without the word album in it. – Larryc Aug 29 at 21:50
  • A powershell one liner gci -dir -rec -Filter '*(Album)'| Ren -NewName {$_.Name -replace ' ?\(Album\)'} ccan also be wrapped in batch/cmdline powershell -Nop -c "gci -dir -rec -Filter '*(Album)'| Ren -NewName {$_.Name -replace ' ?\(Album\)'}" – LotPings Aug 29 at 23:49
  • I didn't know that with PowerShell you could also make this kind of scripts. I'll try to see how good it is. – Zontir Aug 30 at 11:10
0

A command to "search" for folders containing "(Album)" would be:

dir *(Album) /s /b /ad

See dir /? for more information. This could be the list to use in the for loop.

Just tried and this should work:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%f in ('dir /s /b /ad "*(Album)"') do (
    set "filename=%%~nf"
    ren "%%f" "!filename:~0,-7!"
)
  • This works, but has some errors, I had to run the script at least twice to get it to rename most of the folders however there are folders that do not rename them. For example when the folder name has apostrophes like There's No City (Like Miami) EP (Album) Any solution to this? I also couldn't figure out why I had to run the script twice so I could rename them all. – Zontir Aug 30 at 11:08
  • 1
    @Zontir I could not reproduce this issue. General idea to debug a batch script would be to echo the commands in the for loop so you can see what is actually executed. To clarify the script should be executed in the root of all albums. The only thing I could imagine would be that there is another folder named There's No City (Like Miami) EP so the ren command fails but in this case there should be an error message. – JMichael Aug 30 at 11:19
  • Everything works well but with the Albums that have apostrophes as the example I gave fails the renowned. It is surely because the apostrophe escapes from the file name – Zontir Aug 31 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.