Search code examples
for-loopbatch-filewindowcmdfindstr

Find and copy files in batch with keeping the original structure


I need to find all *.PST file on my "C" drive, and copy all instance to a new folder on C:\

How to do this in dos-batch with keep the original structure? I don't want to override the second instance the first one..

  • I think this should work, but I need to keep the original subdirectory or take every hit to a unique subdirectory?
  • I have to exclude the c:\backuppst directory from search
 for /f "delims=" %%i in ('dir /s /b /a-d *.PST') do copy /b "%%~i" "C:\backuppst" 

Edit

As you suggested I use Xcopy:

Xcopy c:*.pst c:\backuppst /i/h/s/y /Exclude:my.txt

In my.txt I have: C:\backuppst

But I got "cannot preform cycling copy"


Solution

  • This should work for you if there are no PST files under c:\backuppst to begin with.

    @echo off
    for /f "delims=" %%i in ('dir /s /b /a-d \*.PST') do xcopy "%%~i" "c:\backuppst%%~pi"
    pause