Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to use xcopy on a Windows machine to pull out all files with .doc extension into a single directory.

An example:

I need to copy the .doc file from the below source:

D:\new folder\new1\new1-1\new\y.doc  
D:\new folder\new2\new2-1\new\y.doc  
D:\new folder\new3\new3-1\new\y.doc  
D:\new folder\new4\new4-1\new\y.doc  
D:\new folder\new5\new5-1\new\y.doc  

And paste them in D:\test\ as below:


and avoid replacement of the .doc files.

share|improve this question


share|improve this answer
@echo off & setlocal

for /s "D:\new folder" %%f in (*.doc) do call :nextfile "%%~f"
goto :eof

    set /a num+=1
    set "target=D:\test\%~n1%num%%~x1"
    if exist "%target%" goto :nextfile
    copy "%~1" "%target%"
    goto :eof
share|improve this answer
Don't say "I'd like to use <some tool>" unless you have a specific reason to use that tool and you know the tool can do what you're asking for. In this case, you can replace copy with xcopy, but you still have to use the script, and if you have many files it may even be slower (xcopy is an external process, while copy is built-in). – grawity Jun 27 '11 at 17:17
for /L %a in (1,1,50) do XCOPY "D:\new folder\new%a\new%a-1\y.doc" D:\test\y%a.doc

replace "50" with whatever your ending folder number is.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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