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?

4
  • 5
    You've omitted the setlocal enabledelayedexpansion instruction line which should be placed directly after the @echo off line Commented Jan 3, 2018 at 0:16
  • 4
    Oh, and try echoing %%~ni. Commented Jan 3, 2018 at 0:17
  • So tried echoing %%~ni and %%i and they both just echoed the same thing, what's the difference in the two commands? Commented Jan 4, 2018 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. Commented Jan 4, 2018 at 22:49

1 Answer 1

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.

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

2 Comments

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.
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.

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.