Copying folders with specific names (perhaps using ROBOCOPY)
I have a server with a fairly complex folder structure that needs
certain folders copied to another location.
Essentially, there are many folders and subfolders all over the D:
drive. In most of the subfolders there is a folder called "Archive".
Inside these Archive folders is the data that I need to move to
another server.
Does ROBOCOPY have a way of copying only folders with certain names? I
know how to copy files using wildcards to only copy certain filenames.
I am hoping that the same folder structure would be duplicated on the
destination server.
I have tried the following command, but of course it did not work (I
do not think that wildcards are allowed in folder names):
c:>robocopy \\source\d$\apps\arch*\ \\destination\d$\apps /s /e /z /
ipg:100 /r:1 /w:2
Any help would be greatly appreciated.
Thank you.
Re: Copying folders with specific names (perhaps using ROBOCOPY)
You could do it with the batch file below. Please note this:
- The batch file will fail if your folder names include "poison" characters
such as &, %, ^, !, (, ).
- To activate the batch file, you must remove the word "echo" in
the robocopy line.
@echo off
setlocal EnableDelayedExpansion
set Source=D:\Source Folder\
set Target=\\server\share\dest folder\
dir /s /ad /b "%Source%*.*" | find /i "\archive" > c:\dir.txt
for /F "tokens=*" %%* in (c:\dir.txt) do (
set T1=%%*
call set T2=!T1:%Source%=%Target%!
echo robocopy /s "%%a" "!T2!" *.*
)
Re: Copying folders with specific names (perhaps using ROBOCOPY)
Give yourself some eyes: Replace the line
robocopy /s "%%a" "!T2!" *.*
with
echo robocopy /s "%%a" "!T2!" *.*
and you will see immediately what's going on. Hint: Have a
closer look at this line:
for /F "tokens=*" %%* in (c:\dir.txt) do (
and ask yourself what name you assigned to your loop variable,
i.e. the one preceded with %%.
Re: Copying folders with specific names (perhaps using ROBOCOPY)
Yes, it is. Have a look at the robocopy help files to see how it's done. It
depends, of course, what you mean with "wanted files" . . .
Re: Copying folders with specific names (perhaps using ROBOCOPY)
Pegasus is not being very helpful in this post, and his original script was incorrect. Here is the corrected version:
@echo off
setlocal EnableDelayedExpansion
set Source=D:\Source Folder\
set Target=\\server\share\dest folder\
dir /s /ad /b "%Source%*.*" | find /i "\archive" > c:\dir.txt
for /F "tokens=*" %%a in (c:\dir.txt) do (
set T1=%%a
call set T2=!T1:%Source%=%Target%!
echo robocopy /s "%%a" "!T2!" *.*
)
He put an asterisk instead of an 'a' in two places, but used the 'a' correctly in the last line. This is what he meant by 'your loop variable'.
Re: Copying folders with specific names (perhaps using ROBOCOPY)
Is there a way to perform this copy using a wildcard in the Find command?
For example find all of the folders in the Source that have names beginning with "ABCD"?