copy folders that contain a certain file type

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lennep
Posts: 3
Joined: 25 Jul 2013 06:43

copy folders that contain a certain file type

#1 Post by lennep » 01 Aug 2013 07:07

I am trying to create a batch file that will copy only folders containing a particular file type from a source directory to a destination directory. As the files names within these folders are often identical, I need to copy the folders, not just the files, to stop overwriting of copied files.

With my limited knowledge, I cobbled together the following batch file:

Code: Select all

@echo off
cd c:\X
for /d /r %%f in (*) do (
if exist %%f\*.jpg (
ROBOCOPY %%f C:\Y *.jpg *.tif /S
)
)


Whilst this successfully identifies folders containing .jpg files and copies both .jpg and .tif files to the destination C:\X, the /S option of robocopy doesn't work in this case and only the files are copied, not the containing folder. I am not sure what is causing this to happen. Any help is greatly appreciated.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: copy folders that contain a certain file type

#2 Post by foxidrive » 01 Aug 2013 09:32

The part you only mentioned to me in a PM is that you want this to be restartable - and only copy sections of the source tree at a time.

That changes the whole task.

You cannot use minage and maxage in robocopy using that method.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: copy folders that contain a certain file type

#3 Post by foxidrive » 01 Aug 2013 09:49

Run this and it will create c:\x\copyfiles.txt and it assumes you are looking for all three JPG/DCM/TIF files in every folder, and it will only include those folders which contain all three files.

Copy the first line from c:\x\copyfiles.txt and put it in a new batch file called, say docopy.bat, and see if it works - the path it creates needs to be tested but it should create paths like

c:\y\x\original\folder\names\

Essentially you can copy as many lines from the c:\x\copyfiles.txt file as you want in one go, put them in a bat file, and do it in stages.


Code: Select all

@echo off
cd /d "c:\X"
for /d /r %%f in (*) do (
if exist "%%f\*.jpg" if exist "%%f\*.dcm" if exist "%%f\*.tif" (
>>copyfiles.txt echo.@robocopy "%%f" *.jpg *.dcm *.tif "c:\y\%%~pnxf"
)
)

Post Reply