I want to copy a directory and all the files within it that matches a particular name pattern i.e. starts with r1_t(followed by any number of alphanumeric characters) to the given location.


But apparently I can’t use wildcard to address a directory. So how can I achieve the above in a cmd batch file?

  • Find all directories matching the pattern and use a loop to run that command for each found instance. – Seth Mar 9 '17 at 9:07
  • You can use the /D option with the FOR command or the DIR command with the /AD switch to find the directories you want to copy then use the XCOPY or ROBOCOPY with those outputs. – Squashman Dec 17 '17 at 3:23

I want to copy a directory and all the files within it that matches a name pattern

copy is for copying files not directories.

To copy directories use xcopy or robocopy.


xcopy "C:\PROJECTd\r1_t*" "C:\PROJECT\BAK" /s /i


  • No trailing \ on the source directory name (otherwise you will get an Invalid path error).

  • /s - Copy folders and subfolders.

  • /i - If in doubt always assume the destination is a folder.

Further Reading

  • I tried C:\Users\maven>XCOPY "C:\Project\r1_t*" "C:\Project\BAK" /s /i but it gives my Cannot perform a cyclic copy even though the destination folder isnt a child of the parent. why is that? – Maven Mar 9 '17 at 9:54
  • @Maven BAK is a child of C:\Project. Note in your question it isn't (you have C:\PROJECTd\ as the source directory). – DavidPostill Mar 9 '17 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.