I have a root folder: C:\Users\username\Desktop\New folder which contains .mp3 files that are named like this:



I tried creating a batch that would sort them by the first 5-digits of the 4th series of numbers ( the 97270 or 97620 in this case) create folders named like that and then move all the files into their specific folder.

Here is what I've come up with:



set "sourcedir=C:\Users\username\Desktop\New folder"

for /f "tokens=1,2,3,4 delims=_" %%a in ('dir /b /a-d *_*_*_*.mp3') do (

set x= %%d

set y= !x:~1,5!

mkdir "!y!" 2>nul

move "%%a_%%b_%%c_%%d" "!y!"


It works! but, I would like to know how to add text to the folder names, like have the created folder named:

97270 - Bla Bla Bla or

Bla Bla Bla - 97270

The 'Bla Bla" should not depend on anything taken from the file name, rather have it set by the user from withing the batch.

I have not managed to find the proper syntax for it.

  • 1
    -1 You haven't troubleshooted and broken the problem down. Why don't you write echo before move, and then see what commands are being run. and then forget user input for a moment, but try adjusting the mkdir line and see how it affects the echo move line. And also figure out the right syntax that move requires for just a single file. And then once you have that figured out, why don't you write a new program to figure out how to get input from a user. And then, why not use that. And if you get stuck on any of those little tasks you can ask – barlop Jun 25 '16 at 10:01
  • Okay so keeping the initial part the same I added a second one that should run one the sorting is complete, but, it tells me the syntax in incorrect, I've tried multiple times with slight changes but no use for /f %%a in ('dir /b') do ( IF %%a = 97270 (rename "%%a" "%%a name1") Else ( IF %%a = 97730 (rename "%%a" "%%a name2") Else ( IF %%a = 97720 (rename "%%a" "%%a name3") Else ( IF %%a = 97620 (rename "%%a" "%%a name4") ) ) ) ) endlocal' This second part is supposed to simply rename the folders created by the first part. – Alex.T Jun 25 '16 at 12:04
  • You're still not troubleshooting. What do you mean "no use" re what you tried.. You should investigate what it's doing. Why don't you echo the command and see what happens, like I wrote in my comment. You could then add your investigations into your question. This site isn't for people to write programs for them. Figure out what commands it is running vs what commands you want it to run. Then you might ask why it's doing what it's doing. You're just not troubleshooting. – barlop Jun 25 '16 at 15:28
  • Well, turns out that I didn't need the else command just the IF's Also my folders had a space in front of their name and this is why it didn't work the first time, I was getting an error telling me that the file was not found. I tried running the echo command but as soon as the batch reached the part where it would stop working the CMD window would close and I could not get any feedback, or I was entering the command wrong idk – Alex.T Jun 25 '16 at 21:39
  • sometimes it can help when troubleshooting also, to turn echo on. And you can add echos like "got this far 1" Also if you find the window closes suddenly, then you could try running the bat file from a cmd prompt window.. it shouldn't close the parent window then. – barlop Jun 26 '16 at 1:52

After checking some other topics on SuperUser, SS64, and other random sites found via Google Search I decided that I would use the 'rename' command in combinations with 'IF' and 'Else'.

Nesting the 'IF' and 'Else' was not that easy to do, so I dropped the 'Else' and only used 'IF' - it worked!

'for /f %%a in ('dir /b /d *') do (

IF %%a EQU 97270 (rename "%%a" "%%a - Pens&Payroll")

IF %%a EQU 97730 (rename "%%a" "%%a - Informare ERB")

IF %%a EQU 97720 (rename "%%a" "%%a - Lost")

IF %%a EQU 97620 (rename "%%a" "%%a - Credit Cards")


| improve this answer | |
  • -1 If %a equals 97270 somewhere in the string other than the desired location, the rename wouldn't work correctly. – Sun Jun 27 '16 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.