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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
xassnake
Posts: 8
Joined: 01 May 2011 04:29

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

#1 Post by xassnake » 01 May 2011 05:03

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)
Last edited by xassnake on 01 May 2011 05:47, edited 1 time in total.

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

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

#2 Post by aGerman » 01 May 2011 05:14

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

xassnake
Posts: 8
Joined: 01 May 2011 04:29

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

#3 Post by xassnake » 01 May 2011 05:41

only *.jpg files
copy .jpg files to (created map directory)
c:\dir1\map
c:\dir2\map
c:\dir3\map
etc..............

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

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

#4 Post by aGerman » 01 May 2011 06:40

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

xassnake
Posts: 8
Joined: 01 May 2011 04:29

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

#5 Post by xassnake » 01 May 2011 09:04

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

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

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

#6 Post by aGerman » 01 May 2011 14:02

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

xassnake
Posts: 8
Joined: 01 May 2011 04:29

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

#7 Post by xassnake » 03 May 2011 12:53

Thank you very much it works great :D

Post Reply