1

Ok, I've been at this for days now. And I've tried it step by step but there's always something going wrong.

What I'd like to do is this: I have my entire music collection on an external HDD. I'd like to run a batch file that will:

  • go into each category folder (category is the first letter of the artist name)
  • create a markdown file for the category listing each artist in the category
  • go into each artist folder
  • create a markdown file for the artist listing each album
  • go into each album folder
  • create a markdown file for the album listing each track
  • create an mp3 folder and move all the mp3s for that album into it

This is what I've tried (and many variations of...)

Make a temp list of categories..

for /f %%I in (.) do break > %%~nxI
dir /b /o:n /a:d > temp_categories.txt

For each category, make a category markdown file

for /f "delims=" %%C in (TEMP_categories.txt) do (
echo The %%C category >> %%C.md

Then I basically do the same thing to go into each artist folder and each album. When I get in the album folder, I add...

mkdir mp3
move [/Y | /-Y] *.mp3 mp3

It works up until it gets into the first album of the first artist in the first category. It adds the mp3 folder but doesn't move the files and it never goes to the next item.

I realize this is probably not the best code. But I only need it to work once. Otherwise I'll need to do everything manually.

3
  • 1
    I'm not clear on what you are using "break" for. According to the documentation "This command is no longer in use": learn.microsoft.com/en-us/windows-server/administration/… Commented Mar 12, 2022 at 20:31
  • break > file.ext creates an empty file (0 bytes) Commented Mar 12, 2022 at 22:39
  • That was a snippet I found online. I removed those lines once I realized they didn't do anything. Commented Mar 13, 2022 at 6:04

1 Answer 1

1
@ECHO OFF
SETLOCAL
rem The following setting for the source directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"

rem C ategories
FOR /d %%C IN ( "%sourcedir%\*") DO (
 CALL :validatecategory "%%~nxC"
 IF NOT DEFINED invalidcat (
  rem aR tists
  FOR /d %%R IN ( "%sourcedir%\%%~nxC\*") DO (
   ECHO %%~nxR>>"%%C\%%~nxC artists.md"
   rem A lbum
   FOR /d %%A IN ( "%sourcedir%\%%~nxC\%%~nxR\*") DO (
    ECHO %%~nxA>>"%%R\%%~nxR albums.md"
    MD "%%A\mp3" 2>nul
    rem T rack
    FOR %%T IN ( "%sourcedir%\%%~nxC\%%~nxR\%%~nxA\*.mp3") DO (
     ECHO %%~nxT>>"%%A\%%~nxA tracks.md"
     MOVE "%%T" "%%A\mp3" >nul
    )
   )
  )
 )
)
GOTO :EOF

:: Categories must be one character

:validatecategory
SET "invalidcat=%~1"
SET "invalidcat=%invalidcat:~1%"
GOTO :eof

Note that throughout, %%Q will have the full file/pathname and %%~nxQ the name where Q is C:Category R:Rtist A:Album T:Track.

Obviously, test against a test subtree first.

Note that I've assumed that the .mp3s are in the Album directory. If you move the .mp3s to an MP3 directory, then further runs will ignore them, so the tracks.md files should not be initialised to empty but the other .md files should be deleted on re-run.

--- Response to comments

Add the obvious additional line

ECHO %%~nxR>>"%%C\%%~nxC artists.md"
CALL :makelinksfile "%%~nxR">>"%%C\%%~nxC artists_links.md"
rem A lbum

and add the new :makelinksfile routine below any GOTO :EOF line

GOTO :eof

:: Transform spaces in artists' names (%1) to "%%20"

:makelinksfile
SET "artist=%~1"
SETLOCAL enabledelayedexpansion
ECHO !artist: =%%%%20!
endlocal
GOTO :eof

What happens here is that artist is set to the parameter supplied (artist's name) and stripped of enclosing quotes.

Then, the setlocal enabledelayedexpansion invokes a parsing trick so that the space is replaced by %%20 when being echoed.

Sign up to request clarification or add additional context in comments.

5 Comments

That did the trick! I added a little bit to it from there to add some formatting to the .md files. I had been working on a much smaller scale "test" folder with just a few artists and all. I even had a "Restart" batch file to reset everything back after a mistake. In the morning I'll finally run it for real. Many thanks!
One last thing... When I write to the .md files, is there a way to take each artist name and replace the spaces with %20? For instance, in the category.md file, I'd like the artist names to be links: i.e. the [Artist with Spaces](./Artist with Spaces/Artist with Spaces.md) link would become [Artist with Spaces](./Artist%20with%20Spaces/Artist%20with%20Spaces.md). That would keep me from having to adjust each markdown file, even though I could to a search and replace in my text editor if I had to.
I've tried adding a basic search and replace just before the script writes the artist names, but that just showed up blank ([Artist with Spaces](.//.md).
Thanks Magoo for your reply. That CALL just adds a second line under the previous ECHO command so it shows as: - [Artist with Spaces](./Artist with Spaces/Artist with Spaces) Artist%%20with%%20Spaces I thought I would have to SET a second variable to keep "Artist with Spaces" separate from "Artist%20with%20Spaces"
Did you observe that the new command's output is redirected to ` artists_links.md, not ...artists.md` ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.