6

I'm looking for a solution to use robocopy to copy several folders from a directory onto a distant network share. I want to choose several folders out of a directory that contains hundreds of folders I'm not interested in. I want to do something similar to scp in linux using regex, but this doesn't work in robocopy:

c:\robocopy "c:\results\1319_TC1.*" "\\datastore\somefolder\"

7 Answers 7

5

Try this one:

gci C:\results\1319_TC1.* | foreach-object { robocopy $_.fullname (".\datastore\somefolder\" + $_.name) }

gci C:\results\1319_TC1.* get's all matching files/directories first and puts them through the pipe where foreach-object takes care of all results from the first command. It'll robocopy the fullpath of each result (full path to your result-directories) and put them into .\datastore\somefolder\ with its original foldername e.g.:
C:\results\1319_TC1.123456 -> C:\results\datastore\somefolder\1319_TC1.123456

That thing in braces will put that target-directory-name and the original folder-name together.

Edit:
I just saw that your target-directory should be a UNC-path. Robocopy accepts UNC-paths (even with path-names longer than 256 characters). You just have to replace (".\datastore\somefolder\" with ("\\datastore\somefolder\" in my command. So the right command would be:

gci C:\results\1319_TC1.* | foreach-object { robocopy $_.fullname ("\\datastore\somefolder\" + $_.name) }

2

You would do it in a batch file. You will need on line per directory.

In the alternative, you could do this, which would copy everything and then delete the excess if that is easier: Robocopy z:\directory d:\directory /MIR /COPYALL (Caution: MIR is for mirror image and will overwrite anything in its way, so use only on a blank directory).

1

You will not be able to do this with Robocopy alone. If you have access to a linux machine, it would be trivially easy using find with the -exec option. Or you could use cygwin on windows (I would guess it has the find command), or you could use a scripting language like Ruby or Python on Windows.

3
  • Linux or cygwin aren't neccessary since he has access to powershell which is able to use regex. However, this would work...
    – wullxz
    May 23, 2011 at 21:42
  • Yeah, of course these are not the only options, but they would be my top picks.
    – James
    May 23, 2011 at 21:45
  • Yeah, each to his own liking :)
    – wullxz
    May 23, 2011 at 21:51
1

Go with the following:

for /d %d in (c:\results\1319_TC1.*) do robocopy "%d" "\\datastore\somefolder\%~nxd" /E /W:0 /R:0 /XO /XJ

Here we use Windows command for with a /d swith to loop over all directories of 1319_TC1.* and the for command would store each directory name, the full pathname c:\results\1319_TC1.* .. one by one in the variable %d.

With the help of %~nxd we separate the folder name and extension from the entire path and pass it to robocopy to ensure that the same folders are created at the destination of \\datastore\somefolder.

Choose your robocopy switches by your taste.

The for command allows you to enter and pass explicit names to another command, robocopy in this case. Here is an example to copy user profiles of john and paul to the datastore. I go to the source folder cd c:\users first to list the source folder names without the full path names:

cd c:\users
for /d %d in (john,paul) do  robocopy "%d" "\\datastore\somefolder\%d" /E /W:0 /R:0 /XO /XJ
1
  • This actually works for me in the Windows command prompt CMD by writing for /d %d in ("folder1" "folder2" ) do robocopy "sourcePath\%d" "destinationPath\%d" /MIR. /MIR mirrows the whole directory structure. Oct 1, 2022 at 23:21
1

Robocopy specifically does not support inclusion patterns, but it does support exclusion patterns. It also supports copying only files and folders which are already mirrored in the target directory, so you can combine both together to implement a flexible solution to include only folders with a specific pattern.

This answer is adapted from the beautiful answer by John at: https://community.spiceworks.com/topic/70361-robocopy-folder-wildcard

What you would like to have:

robocopy mySrc myDest /S /IncludeFolders 1319_TC1.*

What you can actually do:

  1. robocopy mySrc myDest /E /CREATE
  2. robocopy myDest DeleteMe /E /MOVE /XD 1319_TC1.*
  3. robocopy mySrc myDest /S /XL
  4. rd DeleteMe /s

Explanation

Step 1 creates a copy of the entire source, but all files are 0 Bytes (no actual movement of data)

Step 2 moves out all the folders that are not the target pattern into some temp folder (i.e all folders that do not start with 1319_TC1.)

Step 3 does the actual copy, copying from source into target only the files which are already in the destination

Step 4 deletes the unwanted 0 Byte files

This is such a great solution as it will only effectively copy the files you want but greatly increases the available expressivity that was intended in the original robocopy, genius!

0

A quick powershell like this:

$Dir = get-childitem "c:\results\"  -recurse
$List = $Dir | where {$_.FullName -match "1319_TC1."}
$List | split-path FullName -parent | get-unique

Will give you a list of all files that are in a folder that matches "1319_TC1." Then all you need to do robocopy each of those folders.

0

I know this is an answer to a 6 year old question, but all previous answers were kludges. Since this is the top result in google, I thought I'd chime in the correct solution to the specific question.

c:\robocopy "c:\results\1319_TC1.*" "\\datastore\somefolder\"

You can't select multiple source directories. However, directories are files.

If you have a structure of .\folder1\folder3\, .\folder1\folder4, and .\folder2

The correct syntax to copy folder3 and folder4 into folder2 with robocopy is:

robocopy folder1 folder2 folder* /e

where /e will move all directories.

This is where it gets interesting. To control which directories get copied you do not include a list or wildcard to choose the ones that you want to copy, you use /XD and use a list or wildcard to exclude the ones you do not want to copy.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .