Batch file to make folders with part of file name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
doshisahil95
Posts: 1
Joined: 15 Nov 2015 01:06

Batch file to make folders with part of file name

#1 Post by doshisahil95 » 15 Nov 2015 01:16

I am a designer. I need to move files that are all present in one folder to folders that are of the file names. The files are of two formats, .psd and .jpg. I need a batch file that will read the file name, make a folder of part of that file name and move all files that start with that file name into that folder regardless the extension.

For example, if I have 12 files named as

20.psd
20.jpg
20_low.jpg
20_mob.jpg

220.psd
220.jpg
220_low.jpg
220_mob.jpg

120.psd
120.jpg
120_low.jpg
120_mob.jpg

I need a batch file that will make folders 20, 220 and 120 and move all the respective files into the respective folders. So, files 20.psd, 20.jpg, 20_low.jpg and 20_mob.jpg will be in folder named 20 and so on. I tried various links on stackoverflow.com but i couldnt find anything that works. Please help. This is a very monotonous and time consuming job and having a simple program to do my job is useful! :-p

For this i tried these programs with the basic language that i have about dos programming and using examples from stack overflow.

http://stackoverflow.com/questions/1144 ... move-files
http://stackoverflow.com/questions/1999 ... hat-folder

Now the problem i am facing with this code is that this isnt the kind of file name i am using. Also, there is no division as such in the file name. What can i do to make this work?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch file to make folders with part of file name

#2 Post by aGerman » 15 Nov 2015 07:53

That may work

Code: Select all

@echo off &setlocal
for /f "delims=" %%i in ('dir /a-d /b *.jpg *.psd') do (
  for /f "delims=._" %%j in ("%%i") do (
    2>nul md "%%j"
    move "%%i" "%%j\"
  )
)

Regards
aGerman

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to make folders with part of file name

#3 Post by foxidrive » 16 Nov 2015 17:21

doshisahil95 wrote:Now the problem i am facing with this code is that this isnt the kind of file name i am using.


Having accurate samples of the filenames will help.

Post Reply