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?
4Following on from that i'd say A)could combine them-
for %i in (*) do cmda && cmdb
B)could echo firstfor %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.6932This 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.3972Also, to expand barlop's comment, when you use
cmda && cmdb
thencmdb
will be executed only ifcmda
was successful. If you were to usecmda & 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 anerrorlevel
value shouldn't be used with the latter. – and31415 – 2014-06-01T08:43:15.073