Haris P
Haris P

Reputation: 47

copy specific folders and files using a batch file

I'm trying to create a batch file that will do the following:

I have multiple directories and in each one of those directories there is a folder called '06-2015'. I want to create a batch script that will go thru all of those directories and copy the folder '06-2015' and its files and nothing else.

Example:

C:\Files\Accounts\06-2015
C:\Files\Sales\06-2015
C:\Files\IT\06-2015

Is there a way I can create a script that will go something like:

xcopy C:\Files\*\06-2015 C:\Backup\*\06-2015 /s

Or is there a different/better way to do this?

Upvotes: 1

Views: 2698

Answers (2)

foxidrive
foxidrive

Reputation: 41234

Test this: remove the /s and /e if you don't want any folders inside 06-2015 copied.

@echo off
for /d /r "c:\files" %%a in (06-2015?) do (
  if /i "%%~nxa"=="06-2015" xcopy "%%a" "C:\Backup\%%~pnxa\" /s/h/e/k/f/c
)

Upvotes: 0

JosefZ
JosefZ

Reputation: 30113

A wildcard characters allowed only in the last path item.

@ECHO OFF
SET "target=06-2015"
IF NOT EXIST c:\backup\%target% MKDIR c:\backup\%target%
FOR /F "delims=" %%G IN ('DIR /B /AD "c:\files"') DO (
  if exist "c:\files\%%G\%target%\" (
    :: create backup directory if necessary
    MKDIR "c:\backup\%%G\%target%\" 2>NUL
    XCOPY /S /E /Y "c:\files\%%G\%target%\" "c:\backup\%%G\%target%\"
  )
)

Upvotes: 1

Related Questions