2

This is literally the first ever batch script I've ever written. I'm trying to iterate through the current folder, find all the .mp3 files and then create a folder for each mp3 file with the same title as the file but without the file extension (then move the file into its respective folder add called ffmpeg to split them into shorter chunks but I haven't gotten to that point yet).

I've found a method here to basically exactly what I need to do. I've also found questions here and here which kinda show how to do the assignment of the of the environment variable to the for loop variable, however its not working for me.

This is my code.

@ECHO OFF 
cd C:\test
for %%i in (*.mp3) do (  
  echo %%i
  set episodeName=%%i
  set episodeName=!episodeName:~0,-4!
  echo !episodeName!
  mkdir !episodeName!
)

This is my output.

C:\test>"file splitter.bat"
171 Election.mp3
!episodeName!
172 24 Hours at the Golden Apple.mp3
!episodeName!
A subdirectory or file !episodeName! already exists.

What stoopid obvious mistake am I making?

BoffinBrain
  • 6,337
  • 6
  • 33
  • 59
  • 5
    You've omitted the `setlocal enabledelayedexpansion` instruction line which should be placed directly after the `@echo off` line – Magoo Jan 03 '18 at 00:16
  • 4
    Oh, and try `echo`ing `%%~ni`. – Magoo Jan 03 '18 at 00:17
  • So tried echoing `%%~ni` and `%%i` and they both just echoed the same thing, what's the difference in the two commands? – generic purple turtle Jan 04 '18 at 21:49
  • 1
    Shouldn't have done. `%%i` should show the full name including the `.mp3`, whereas `%%~ni` should show the name part only, without the `.mp3`. The point being that using `%%~ni` should mean that you don't need `episodename` at all, you don't need to remove the last 4 characters, and if you decide to use the same mechanism to remove some other extension with a different length, there are no changes to make other than specifying the new extension. – Magoo Jan 04 '18 at 22:49

1 Answers1

1

As Mangoo said, there was the initial issue with enabledelayedexpansion being off, which meant your !variables! wouldn't be parsed as needed. Additionally, There was an issue with mkdir not putting the argument in quotes, which meant if you had an episode called 'Episode 1', then it would attempt to make the folders 'Episode' and '1' rather than a single folder. With these fixes in place, an additional move command added to the loop, and a pause at the end to check the output, you end up with the following....

@echo off
setlocal enabledelayedexpansion
cd C:\test
for %%i in (*.mp3) do (  
    echo %%i
    set episodeName=%%i
    set episodeName=!episodeName:~0,-4!
    echo !episodeName!
    mkdir "!episodeName!"
    move "%%i" "!episodeName!\%%i"
)
pause

If you're going to be doing this regularly, I may also suggest using Foobar2000 which, as well as being a robust and customizable audio player for Windows, also includes a swiss army knife of other tools for managing tags, converting audio, and of course, file operations! I use this for my own music library after purchasing and downloading new tracks. Since it can easily read ID3 tags, I can tell it to make folders by artist name and rename the files to reflect the track name. This gives you even more flexibility than just looking at a filename, if your tracks are tagged properly.

BoffinBrain
  • 6,337
  • 6
  • 33
  • 59
  • Brillaint thank you, I was wondering if you could also perhaps please tell me what I should google to read about and understand the !episodeName:~0,-4! syntax as I don't really know how that line works, I just copypastaed it. – generic purple turtle Jan 04 '18 at 21:43
  • Sure. If you just type `help set` into cmd, you'll get the full docs on it. Substring syntax starts a couple of pages down. – BoffinBrain Jan 05 '18 at 09:41