Solved

Windows batch file - copy entire folder if contains specific file

Posted on 2013-02-04
12
493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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 55

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 55

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 70

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 55

Accepted Solution

by:
Bill Prew earned 500 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 70

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

734 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