Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Windows batch file - copy entire folder if contains specific file

Posted on 2013-02-04
12
Medium Priority
?
501 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 57

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 57

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 57

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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
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…
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…

670 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