CMD Command to create folder for each file and move file into folder

10

15

I need a command that can be run from the command line to create a folder for each file (based on the file-name) in a directory and then move the file into the newly created folders.

Example :

Starting Folder:

Dog.jpg
Cat.jpg

The following command works great at creating a folder for each filename in the current working directory.

for %i in (*) do md "%~ni"

Result Folder:

\Dog\
\Cat\
Dog.jpg
Cat.jpg

I need to take this one step further and move the file into the folder.

What I want to achieve is:

\Dog\Dog.jpg
\Cat\Cat.jpg

Can someone help me with one command to do all of this?

Tom

Posted 2014-05-31T19:35:27.650

Reputation: 101

Answers

10

The second command would be

for %i in (*) do move "%i" "%~ni"

EDIT: Added "" for the %i, based on and31415's comment. tnx.

LPChip

Posted 2014-05-31T19:35:27.650

Reputation: 42 190

4Following on from that i'd say A)could combine them- for %i in (*) do cmda && cmdb B)could echo first for %i in (*) do @ECHO cmda && cmdb C)could add I think it's a /R to go through subdirs. – barlop – 2014-05-31T21:14:05.693

2This command won't work when file names contain spaces. You should use something like this: for %i in (*) do move "%~i" "%~ni" Even that might not be good enough, as it can have side effects when the target folder don't exist (e.g files being "moved" over themselves, hence renamed). An easy solution would be to combine both commands, like this: for %i in (*) do md "%~ni" && move "%~i" "%~ni" The command assumes that the directories don't exist already. – and31415 – 2014-06-01T08:42:17.397

2Also, to expand barlop's comment, when you use cmda && cmdb then cmdb will be executed only if cmda was successful. If you were to use cmda & cmdb instead, then the second command would be executed regardless. In fact, a single ampersand (&) is just a command separator and the double ampersand (&&) is a conditional one. Commands which don't set an errorlevel value shouldn't be used with the latter. – and31415 – 2014-06-01T08:43:15.073

5

Just execute these commands in series:

For creating the folders for each file:

for %i in (*) do mkdir "%~ni"

For moving each file to its folder:

for %i in (*) do move "%i" "%~ni"

Yusuph wickama

Posted 2014-05-31T19:35:27.650

Reputation: 169

1Welcome to Super User! This duplicates another answer and adds no new content. Please don't post an answer unless you actually have something new to contribute. – DavidPostill – 2016-12-13T10:14:57.333

-1

This will do it if you have some folders like: example years\Filename.mp4

1901\Filename.mp4 
1902\Filename.mp4
1903\Filename.mp4

it will list all the folder 1st level files; lists all *.mp4 and *.mkv will create the 2 level folders with the filename and will move all the same name files in the 1st level folder to the 2nd level folder, run it at the years base folder.

for /d %D in (*) do for %i in (%~fD\*.mp4,%~fD\*.mkv) do mkdir "%~dpi%~ni" && move "%~dpi%~ni.*" "%~dpi%~ni\"

If you don't have a 1st level YEARS folder you can just bypass the first for and run the 2nd step, run it at the filename base folder.

for %i in (*.mp4,*.mkv) do mkdir "%~dpi%~ni" && move "%~dpi%~ni.*" "%~dpi%~ni\"

The && will ensure that the previous mkdir %ERRORLEVEL% is 0 to run the move of the files

To test, use this:

for /d %D in (*) do for %i in (%~fD\*.mp4,%~fD\*.mkv) do echo "%~dpi%~ni" && echo "%~dpi%~ni.*" "%~dpi%~ni\"

Olívio Moura

Posted 2014-05-31T19:35:27.650

Reputation: 1

Find left files: for /d %D in (*) do for %i in (%~fD\*) do echo "%~dpi%~fi" – Olívio Moura – 2019-07-01T15:25:59.257

for /d %D in (*) do for %i in (%~fD\*) do move "%~fi" "D:\TRASH" – Olívio Moura – 2019-07-01T15:34:42.563