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:

y1.doc  
y2.doc  
y3.doc  
y4.doc  
y5.doc  
:  
:  
:   

and avoid replacement of the .doc files.

link|improve this question
feedback

2 Answers

FOR /R

FOR /R %%G IN (.) DO COPY %%G %NEWFOLDER%
link|improve this answer
feedback
@echo off & setlocal

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

:nextfile
    set /a num+=1
    set "target=D:\test\%~n1%num%%~x1"
    if exist "%target%" goto :nextfile
    copy "%~1" "%target%"
    goto :eof
link|improve this answer
2  
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
feedback

Your Answer

 
or
required, but never shown

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