Solved

Windows batch file - copy entire folder if contains specific file

Posted on 2013-02-04
12
482 Views
Last Modified: 2013-02-19
Hello Techies,

i'm trying to write a batch file or find any utility to make this "simple" task:

I have a folder with 2000 subfolder
I would like to search for specific file inside these subfolder (exemple *.jpg)
I would like that if any jpg file found inside the subfolder, the entire directory is copied (not only jpg file)

Can't find any solution :(

Thanks for help
0
Comment
Question by:sibliege
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 15

Expert Comment

by:jerseysam
Comment Utility
I use Microsoft Robocopy GUI.

This allows you to create a batch easily and apply all the swtiches you require.

Available free:

http://technet.microsoft.com/en-us/magazine/2006.11.utilityspotlight.aspx

or if you want to do manually take a look at switches:

http://technet.microsoft.com/en-us/library/cc733145(WS.10).aspx
0
 

Author Comment

by:sibliege
Comment Utility
Hello,

robocopy does not allow what i'm searching for ;)
0
 
LVL 3

Expert Comment

by:MiamiCo
Comment Utility
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
Try the script below. It's currently in test mode and will only display the robocopy command(s) it would normally run. Remove the uppercase ECHO in front of robocopy in line 23 to run it for real.
@echo off
setlocal enabledelayedexpansion
set SearchFolder=C:\Temp
set SearchMask=*.jpg
REM *** The target folder for the copy folder; the folder name in which an item is found will be attached automatically.
set TargetFolder=D:\Target
set /a ProcessedCount = 0
for /f "delims=" %%a in ('dir /s /b "%SearchFolder%\%SearchMask%"') do (
	set Source=%%~dpa
	set Source=!Source:~0,-1!
	echo Found '%%~nxa' in folder '!Source!' ...
	set /a Skip = 0
	for /l %%i in (1, 1, !ProcessedCount!) do (
		if "!ProcessedFolders[%%i]!"=="!Source!" (set /a Skip = 1)
	)
	if "!Skip!"=="1" (
		echo ... skipped, folder already processed.
	) else (
		set /a ProcessedCount += 1
		set ProcessedFolders[!ProcessedCount!]=!Source!
		for %%s in ("!Source!") do (set Target=%TargetFolder%\%%~nxs)
		echo ... copying to '!Target!' ...
		ECHO robocopy.exe "!Source!" "!Target!" *.*
	)
)

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Here's how I would approach it.  This script will find all subdirectories with *.jpg files in them and copy to the destination folder.  Keep in mind that it will copy that folder into the dest folder at the top level, even if it was lower in a subfolder on the source folder.  You didn't really state what you wanted in that case so figured I'd start simple and we can adjust if needed.

@echo off

set BaseDir=C:\Temp
set DestDir=D:\Temp
set Search=*.jpg

for /R "%BaseDir%" %%A in (.) do (
  if exist "%%~dpnxA\%Search%" (
    xcopy /E /I "%%~dpnxA" "%DestDir%"
  )
)

Open in new window

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
If you do want to preserve the complete folder structure then this should handle that:

@echo off
setlocal EnableDelayedExpansion

set BaseDir=C:\Temp
set DestDir=D:\Temp
set Search=*.jpg

for /R "%BaseDir%" %%A in (.) do (
  if exist "%%~dpnxA\%Search%" (
    set Target=%%~dpnxA
    xcopy /E /I "%%~dpnxA" "!Target:%BaseDir%=%DestDir%!"
  )
)

Open in new window

~bp
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:sibliege
Comment Utility
Hello Bill,

actually found something in my side, your script seems cleaner then mine

Could it handle more search criteria like
set Search=*.jpg *.doc *.xls

????


Thanks
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Shown for the latter script, this will allow for multiple file masks:
@echo off
setlocal EnableDelayedExpansion

set BaseDir=C:\Temp
set DestDir=D:\Temp
set Search=*.jpg *.doc *.xls

for /R "%BaseDir%" %%A in (.) do (
   set found=
   for %%X in (%Search%) do (
     if not defined found if exist "%%~dpnxA\%%X" (
       set found=y
       set Target=%%~dpnxA
       xcopy /E /I "%%~dpnxA" "!Target:%BaseDir%=%DestDir%!"
     )
  )
)

Open in new window

0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
Sure, this will allow multiple patterns to be searched for:

@echo off
setlocal EnableDelayedExpansion

set BaseDir=C:\Temp
set DestDir=D:\Temp
set Search="*.jpg","*.doc","*.xls"

for /R "%BaseDir%" %%A in (.) do (
  set DoCopy=N
  for %%B in (%Search%) do if exist "%%~dpnxA\%%~B" set DoCopy=Y
  if "!DoCopy!" == "Y" (
    set Target=%%~dpnxA
    xcopy /E /I "%%~dpnxA" "!Target:%BaseDir%=%DestDir%!"
  )
)

Open in new window

~bp
0
 

Author Comment

by:sibliege
Comment Utility
Sorry for delay,

I finally use this one:

echo off
for /r %%x in (*.jpg* *.doc* *.xls* *.mov *.avi *.mp3 *.wmf *.ppt*) do call :extractpath "%%x"

:extractpath
set dir_source=%~dp1
set dir_name=%~p1
if not exist \\server\share\%dir_name% robocopy %dir_source% \\server\share\%dir_name% /E /COPY:DAT /PURGE /R:0 /w:0
0
 

Author Closing Comment

by:sibliege
Comment Utility
Not used this one but work also...
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Did you check my suggestion? It is almost the same as the accepted one, and would work too.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The way I use Experts Exchange to assist me in analyzing and diagnosing a problem is I first enter a Verbose Question at Experts Exchange like: Office 2007 will hang when opening and saving files I then launch WordPad (any text editor will do) an…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now