views:

731

answers:

4

Is there some way to specify a directory (let's say "C:\images") and move every .jpg file from there to another directory (say "D:\media") but preserve the directory structure (so if the file were "C:\images\paintball\july\07\headshot.jpg" after moving it would be "D:\media\paintball\july\07\headshot.jpg")?

I'm using cygwin (but would be happy to use DOS if that works too).

+2  A: 

Yup.

Do a tar archive of *.jpg files while preserving directory structure (there's a switch) then extract it to the target directory. Should be a one-liner.

Assaf Lavie
+2  A: 
( cd /cygdrive/c/images
tar --create --file - . ) | ( cd /cygdrive/d/media
tar --extract --file - )

There's also a --directory option in some versions of tar with which you can avoid the complexity of piping between subshells, but I never use it myself, so I may be missing something:

tar --create --file - -C /cygdrive/c/images . | tar --extract --file - -C /cygdrive/d/media

If you need more power/flexibility, take the time to investigate rsync.

Since you're on windows, you could also take a look at xxcopy. It's great for this kind of stuff and much else.

bendin
A: 

You can also use xcopy command, like in this example (old is a directory):

xcopy cvs_src\*.jpg old /e/i/h/y/d/exclude:files_to_exclude
dmityugov
A: 

Thanks for the XCOPY solution, it solved my similar problem, so I thought I'd share the details for anyone else needing it.

I wanted a list (not a copy) of all the files in a directory (and sub-directories) that were not of a particular type, such as *.jpg. But the DIR command doesn't have an exclude function. So I:

  1. Created a file named exclist.txt that contained a single line ".jpg"
  2. Ran the command "xcopy c:\files c:\test /exclude:exclist.txt /l /d /e /h /i /y > found.txt"
  3. Opened found.txt in Notepad to see the list of non-jpg files

Note the XCOPY /l parameter, which lists the files to be copied without copying them. Since XCOPY is executed in "list mode", the destination folder c:\test is not created and no files are copied. "> found.txt" saves the output from the XCOPY command to the file found.txt, rather than displaying the results on screen.