Search code examples
batch-filexcopy

copy directories with matching string and all sub-directories and files


I've got a beginner question. I'm trying to use XCOPY to copy all directories beginning with the string "44*" and all files and sub-directories within, regardless of their names.

I've got this to work to grab the appropriate files, but the directory structure isn't coming along.

@echo off
for /f "delims=" %%a in ('dir /b/ad 
"C:\sourcepathgoeshere\44*" ') do xcopy /s /i /v 
"C:\sourcepathgoeshere\%%a\*" 
C:\destinationpath

I was able to copy the structure first with /t, but that just made a bunch of empty directories with all the files beneath.

Thanks for help putting these basic pieces together.


Solution

  • do xcopy /s /i /v "C:\sourcepathgoeshere\%%a*" "C:\destinationpath\%%a"

    You don't really specify what the problem you have is. The change I've made above works for me, generating

    c:\destinationpath\44-abc\(filetree from 44-abc)
    c:\destinationpath\44-xyz\(filetree from 44-xyz)
    

    IOW, a copy of only the directories from the source that start 44.

    It's also required that the xcopy commands all be on a single physical line.

    I'm not happy about the linebreak in the middle of the dir statement. It appears to work, but I'd break it between ' and ).