Page 1 of 1

Make directory's in subdir tree and copy files (for command)

Posted: 01 May 2011 05:03
by xassnake
My goal is to make a directory in a sub directory and copy files in the map dir

example:
c:\dir\1
c:\dir\2
etc..............
what i want
c:\dir\1\map
c:\dir\2\map
c:\dir\3\map
etc.................

i know a few commands but that whont work
copy files works whit this command
FOR /R %%g IN (.) DO xcopy *.jpg "%%g\*"

i want the jpg files in a sub dir (map)
so i try this command
FOR /R %%g IN (.) DO md map "%%g\*"

it makes directory's in a loop c:\dir\1\map\map\map\map\...........
c:\dir\2\map\map\map\map\...........
c:\dir\3\map\map\map\map\...........

Who knows the command for creating sub directory's an copy *.jpg files to the created directory

I hope you'll understand i'm dutch sorry

another question..........

is it possible to move files with the same directory name to that directory
c:\map\photo1.jpg (file) (move this file to photo1 map)
c:\map\photo1 (map)

Re: Make directory's in subdir tree and copy files (for comm

Posted: 01 May 2011 05:14
by aGerman
One thing isn't clear to me. Do you try to copy only the .jpg files or the entire directory structure as well?

Regards
aGerman

Re: Make directory's in subdir tree and copy files (for comm

Posted: 01 May 2011 05:41
by xassnake
only *.jpg files
copy .jpg files to (created map directory)
c:\dir1\map
c:\dir2\map
c:\dir3\map
etc..............

Re: Make directory's in subdir tree and copy files (for comm

Posted: 01 May 2011 06:40
by aGerman
I see. You could try the following code:

Code: Select all

@echo off &setlocal

set "root=C:\Dir"

for /f "delims=" %%a in ('dir /ad /b "%root%" 2^>nul') do (
  md "%root%\%%~a\map" 2>nul
  for /f "delims=" %%b in ('dir /a-d /b /s "%root%\%%a\*.jpg"') do (
    copy "%%~b" "%root%\%%~a\map\"
  )
)

As you can see I prefer to process the DIR command, but it's up to you ...

Regards
aGerman

Re: Make directory's in subdir tree and copy files (for comm

Posted: 01 May 2011 09:04
by xassnake
thank you it works almost good

it makes the right directory and copied the jpg files in it
but my jpg files are in the root and not in sub dir
if i change the command
for /f "delims=" %%b in ('dir /a-d /b /s "%root%\%%a\*.jpg"')
in
for /f "delims=" %%b in ('dir /a-d /b /s "%root%\*.jpg"')

it copies the jpg file from the root to the map dir (c:\dir\dir1\map)
but if i look at the command shell it copies more than there are folders
see my image
Image

there are no copies all jpg files are in the right directory

Re: Make directory's in subdir tree and copy files (for comm

Posted: 01 May 2011 14:02
by aGerman
OK if all .jpg files are placed in the root you don't need DIR /S.
Try

Code: Select all

@echo off &setlocal

set "root=C:\Dir"

for /f "delims=" %%a in ('dir /ad /b "%root%" 2^>nul') do (
  md "%root%\%%~a\map" 2>nul
  copy "%root%\*.jpg" "%root%\%%~a\map\"
)

Regards
aGerman

Re: Make directory's in subdir tree and copy files (for comm

Posted: 03 May 2011 12:53
by xassnake
Thank you very much it works great :D