Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

@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
share|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

FOR /R

FOR /R %%G IN (.) DO COPY %%G %NEWFOLDER%
share|improve this answer
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

 
discard

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.