Tuesday, December 31, 2019

Find and rename a folder Windows Batch


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.


Answer



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!"
)

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...