Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Windows batch file - copy entire folder if contains specific file

Posted on 2013-02-04
12
Medium Priority
?
504 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
ID: 38851096
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
ID: 38851106
Hello,

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

Expert Comment

by:MiamiCo
ID: 38851146
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 85

Expert Comment

by:oBdA
ID: 38851187
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 59

Expert Comment

by:Bill Prew
ID: 38851436
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 59

Expert Comment

by:Bill Prew
ID: 38851492
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
 

Author Comment

by:sibliege
ID: 38851520
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 71

Expert Comment

by:Qlemo
ID: 38852044
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 59

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 38852209
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
ID: 38905059
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
ID: 38905064
Not used this one but work also...
0
 
LVL 71

Expert Comment

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

876 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