Solved

Windows batch file - copy entire folder if contains specific file

Posted on 2013-02-04
12
490 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
Independent Software Vendors: 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!

 
LVL 84

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 54

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 54

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 54

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

Technology Partners: 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

INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
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…
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…
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…

732 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