0

How can I copy a set of files by a specific pattern from a set of deeply structured folders recursively into another folder? Also I need to recreate the folder hierarchy from source folder in the target folder (only that folders, which contain copied files). I need to use standard Windows command-line tools.

This question looks like this one: How can I recursively copy files of a specific pattern into a single flat folder on Windows? ; but in my case I want to keep folder structure, so this script will not do this:

for /r %x in (*.dll, *pdb) do copy "%x" targetDir\
Community
  • 1
  • 1
Andremoniy
  • 34,031
  • 20
  • 135
  • 241

1 Answers1

0

The decision is:

FOR /r %x in (PATTERN) DO 
     (if not exist TARGET_DIR%~px mkdir TARGET_DIR%~px) & (copy %~x c:\\TARGET_DIR%~px)

So, the "secret" is in %~px command which gives relative path of copied file, so we should create this relative path in target dir.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241