Copy File name to a folder name and insert file inside

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rodman
Posts: 6
Joined: 23 Mar 2012 04:37
Location: Paris

Copy File name to a folder name and insert file inside

#1 Post by Rodman » 01 Sep 2012 11:21

Hello,

I have a new problem.
I will use an example to understand.
I have a list of different files named "a.jpg" "b.txt" "c.mov" inside a folder named ROOT :

ROOT
(
a.jpg
b.txt
c.mov
)

I would like to put each file inside a folder with the name of the file :

ROOT
(
folder a
(
a.jpg
)
folder b
(
b.txt
)
folder c
(
c.mov
)
)

Thanks in advance for your help ! :D

Rodman
Posts: 6
Joined: 23 Mar 2012 04:37
Location: Paris

Re: Copy File name to a folder name and insert file inside

#2 Post by Rodman » 01 Sep 2012 14:43

I need to do this with any files, and for n files in the ROOT folder.
Last edited by Rodman on 01 Sep 2012 14:51, edited 1 time in total.

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

Re: Copy File name to a folder name and insert file inside

#3 Post by aGerman » 01 Sep 2012 14:44

Put this batch code in your root folder:

Code: Select all

@echo off
for /f "delims=" %%i in ('dir /a-d /b^|findstr /vxc:"%~nx0"') do (
  md "%%~ni" 2>nul
  copy "%%i" "%%~ni\"
)

The findstr /v excludes the own batch file from processing.

Regards
aGerman

Rodman
Posts: 6
Joined: 23 Mar 2012 04:37
Location: Paris

Re: Copy File name to a folder name and insert file inside

#4 Post by Rodman » 01 Sep 2012 14:54

Are you the God of the batch ! :D

Thank you for the time you passed on it ! I hope I can be as strong as you one day. ;)

Post Reply