Get string between parentheses in batch

I just decided to study a small batch.

To do something useful, I decided to create a script to organize the music. They are all in this format:

Group (album) - TrackNumber - TrackName

I want my script to create a folder named group and inside it have a folder called albuns. So I need to play with the filename, but I don't know how to use FINDSTR or any other method to get the string before the '(' and between the parent. How can I do this?

+3


source to share


1 answer


Here are some great resources for you:

Technet \ DosTips \ SS64 \ Rob van de Uude \ ComputerHope

Here is an example on how to parse the string you specified. Note that this example DOES NOT WORK if there are spaces within the group, album, or track.

for /f "delims=()- " %%A in ("Band (Album) - TrackNumber - TrackName") do (
    echo Band = "%%~A"
    echo Album = "%%~B"
    echo TrackNumber = "%%~C"
    echo TrackName = "%%~D"
)

      

Output:

Band = "Band"
Album = "Album"
TrackNumber = "TrackNumber"
TrackName = "TrackName"

      



However, this example will work with spaces without using them as a separator. Unfortunately, this means whitespace will not be truncated from the results.

for /f "delims=()-" %%A in ("Band Name (Album) - Track Number - Track Name") do (
    echo Band = "%%~A"
    echo Album = "%%~B"
    echo TrackNumber = "%%~C"
    echo TrackName = "%%~D"
)

      

Output:

Band = "Band Name "
Album = "Album"
TrackNumber = " Track Number "
TrackName = " Track Name"

      

Spaces can be trimmed from strings, but I'll let you learn this technique :) Hint, see links above.

+3


source







All Articles