Windows – Using Robocopy to search and copy directories using pattern matching

backupdirectoryrobocopywindows

I have tried to find an answer to this but most questions related to robocopy and pattern matching are focused on excluding folders whereas my intention is to include folders.

Assuming i have the following folders.

c:\workspace\folder1\subfolder1\workInProgress monthly\*
c:\workspace\folder1\subfolder1\completed monthly\*
c:\workspace\folder1\subfolder1\rejected weekly\*

c:\workspace\folder2\subfolder2\workInProgress yearly\*
c:\workspace\folder2\workInProgress documents daily\*
c:\workspace\folder2\subfolder2\completed monthly\*
c:\workspace\folder2\subfolder21\rejected weekly\*

c:\workspace\folder3\subfolder3\workInProgress weekly\*
c:\workspace\folder3\subfolder3\completed daily\*
c:\workspace\folder3\subfolder3\rejected weekly\*

I have a backup script that executes the ROBOCOPY command for each directory i want to backup.

ROBOCOPY "c:\workspace\folder1\subfolder1\workInProgress monthly" D:\folder1\subfolder1\workInProgress documents /COPYALL /B /SEC /E /MIR /Z /XO /XF *.LCK /R:5 /W:5 /LOG+:dSyncFilesMinimal_%vl%_%tl%.txt /IPG:5
ROBOCOPY "c:\workspace\folder2\subfolder2\workInProgress yearly" D:\folder2\subfolder1\workInProgress documents /COPYALL /B /SEC /E /MIR /Z /XO /XF *.LCK /R:5 /W:5 /LOG+:dSyncFilesMinimal_%vl%_%tl%.txt /IPG:5
ROBOCOPY "c:\workspace\folder2\workInProgress daily" D:\folder2\subfolder1\workInProgress documents /COPYALL /B /SEC /E /MIR /Z /XO /XF *.LCK /R:5 /W:5 /LOG+:dSyncFilesMinimal_%vl%_%tl%.txt /IPG:5
ROBOCOPY "c:\workspace\folder3\subfolder3\workInProgress weekly" D:\folder3\subfolder1\workInProgress documents /COPYALL /B /SEC /E /MIR /Z /XO /XF *.LCK /R:5 /W:5 /LOG+:dSyncFilesMinimal_%vl%_%tl%.txt /IPG:5

Everytime a new folder is added I have to update the script to include the new folder.
How can i use ROBOCOPY to search the workspace folder to find any directory that contains the string workInProgress and copy it to a destination location.

Given that i know that the folder to be copied will have the string workInProgress in its name, is it possible to run a single ROBOCOPY command to recursively search the c:\workspace directory and copy across any directory that matches the patter *workInProgress*"

  • Only folder with patter workInProgress
  • The workInProgress folders can contain different strings after the string workInProgress e.g

    workInProgress daily
    workInprogress unknown

  • There is no known pattern on other folders that do not contain workInProgress in their name. Other folders can contain random names.

Best Answer

An example of a dir used in a FOR loop with how to change the drive letter when outputting it:

FOR /F "usebackq delims==" %i IN (`dir /s /a:d /b c:\workspace\workInProgress*`) DO @echo "c:%~pi" "d:%~pi"