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 .png extension into a single directory.

I tried

xcopy C:\folder\*.png /s C:\png\

But its keeping the subdirectories inside \folder, (for example in C:\png there is C:\png\a\b\c\img.png) which I don't want. I simply want all .png inside C:\png WITHOUT it retaining the directory structure that was in C:\folder.

share|improve this question

2 Answers 2

up vote 12 down vote accepted

This can be done with good old for:

for /r C:\Folder %f in (*.png) do @copy "%f" C:\png

Nothing fancy.

share|improve this answer
    
It works! Care to explain what the %f is for? –  bobobobo Nov 18 '10 at 19:13
    
It's the loop variable. –  Joey Nov 18 '10 at 21:53
    
This didn't work for me, I got something to the effect of f" was unexpected at this time. Changing %f to %%f worked. However, I was running this in a batch file: If you are using the FOR command at the command line rather than in a batch program, use just one percent sign: %G instead of %%G. ss64.com/nt/for.html –  ta.speot.is Dec 8 at 0:27
    
Uhm, yes. That's expected and exactly as you quoted. Where I'd your confusion coming from? –  Joey Dec 8 at 22:00

If you have cygwin installed, this would be a job for find:

cp `find /cygdrive/c/folder/* -name '*png'` /cygdrive/c/png/

(though that will have trouble if any of the filenames have spaces in them - you'll find some variant of a find command that will work in all circumstances though)

If you are running Vista, 2003 or 2008 then the less flexible but still useful "forfiles" is your friend. Something like:

FORFILES /P c:\folder\ /M *.png /S /C "cmd /c copy @file c:\png\"

Note: I've not tested either of the above commands, but in theory they should work...

share|improve this answer
    
+1 just verified that the FORFILES command works. I ran it twice and it copied the 2nd time w/out prompt, so I assume it just overwrites if it finds duplicates. –  hyperslug Aug 8 '09 at 18:39

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.