Make directories from filename & move file

July 19, 2010 at 08:15:58
Specs: Windows 7, AMD Turion X2 64 / 4 GB DDR2

I have a directory set up that stores *.mp3 files.
I need to read the filename of the files, create a directory with only the artist's name and move the *.mp3 file into the new directory. I want to read in the filename from the beginning all the way up to the hyphen.


Filename: Black Sabbath - Crazy Train.mp3
New directory: Black Sabbath

Read filename up to hyphen "Black Sabbath " then Make directory "Black Sabbath"
Move "Black Sabbath - Crazy Train.mp3" to directory "Black Sabbath"

I sort of modified some script from a closely related question on here, but it doesn't exclude the hyphen and everything after it when naming the directory. In addition, even if it did exclude the hyphen and so on, I fear the program would not be successful in moving the file to the new directory since the names would not completely match. Anyone know how to do this? Thanks.

See More: Make directories from filename & move file

Report •

July 19, 2010 at 12:36:48
Exactly that format: <space><dash><space>?
Try the following:

@echo off

setlocal EnableDelayedExpansion

for /f "tokens=1* delims=-" %%a in ('dir /b *.mp3') do (
  if "%%b" neq "" (
    set NewDir=%%a
    set NewDir=!NewDir:~0,-1!
    md "!NewDir!" 2>nul
    move "%%a-%%b" "!NewDir!\"

Report •

July 19, 2010 at 20:33:37

You sir, are a life saver. Thank you so much! It works just about as good as it's going to get lol. It messed up the name of one directory. The song was "Jay-Z feat. Alicia Keys - Empire State Of Mind.mp3". It named the directory "Ja" and copied the file to that directory. Nothing major that I can't go in an rename. Thanks a ton!

Report •

February 10, 2011 at 04:16:10
1st thnx for that post u did onlyinajeep726

but i have a problem about applying this soultion and is that
i know nothing about where to write the commands that MarkLS wrote if u can plz jst point me where to write these.


Report •

Related Solutions

Ask Question