29

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 it is keeping the sub-directories 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.

bobobobo
  • 5,460

4 Answers4

51

This can be done with good old for:

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

Nothing fancy.

Joey
  • 41,098
1

The batch file below is what I've ended up using for a bit more flexibility.
It builds on the accepted solution.

echo off

REM Copying all files with specified pattern recursively from source-directory and sub-folders into destination folder REM Variables REM SRCFOLDER: Specifiy source directory. Search is recursivly done within that location. REM SRCPATTERN: Search patter of one or multiple files, separated by space (Used in "for in ()" type loop REM DSTFOLDER: Specifiy destination directory. All files get copied into it. REM XCOPYOPTONS: Specify some copy options. /d only copies "newer" files, /y suppresses overwrite prompts setlocal set DSTFOLDER=C:\temp\found
set SRCFOLDER=C:\temp\test
set SRCPATTERN=.txt .asc set XCOPYOPTIONS=/d /y

for /R %SRCFOLDER% %%f in (%SRCPATTERN%) do xcopy "%%f" %DSTFOLDER% %XCOPYOPTIONS%

BmyGuest
  • 456
1

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...

0

Joey's above worked for me, but i did have to experiment with it a bit and-so this answer/reply, is just for clarification / explanation for those unfamiliar / intimidated by using commands, and to help avoid/explain a problem i had USING it so hopefully you won't :) ;

(in my case i was needing to merge files of an artist's music tracks, into a single directory since my players simple lists were all being screwed around with by windows media-files special-behaviours! removing artist / albumn when i didn't want it to! eyes roll )


Anyway, starting with his ;

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

the "C:\folder" bit,.. ... is where you specify which folders are to be seached-through for files to-then leave only-the-files, but-not-directories, FROM...

and

the "C:\png" bit,.. ... is where you want the files without directories,.. to-GO.


ex ;

...\KarlK\ (this is where i was in cmd - where the prompt was)

what worked for me, when i was wanting to add all the subdirectories' tracks into a single folder, in a directory before this one, called "allinONE" ... was ; ( by the way, .aiff files are music files just so you know what you're looking at )

FOR /r . %f IN (*.aiff) do @copy "%f" ..\allinONE

NOTE that because it cannot complete a "cyclical" search, you need to have the destination ELSEWHERE,.. than in subdirectories being-searched-THROUGH or to-be-searched-through you might say,.. before it will allow the search/copy - that's why i've got my destination in a ' ..\ ' directory, 'higher', or 'before' it - further 'BACK' towards the c: root.

(in other words, if you have it AFTER where it's to be searching through, it'd go loopy )

if you do that, AND have the cursor/cmd prompt ... in the directory where you want to search-FROM ... so say your music files are both loosely in the starting directory AND in subdirectories,.. then this is OK.

if you're OK with searching through a target-directory somewhere ELSE, other than the current directory, then you should have a complete(full length) location, and not "." as i did - again, if you're unfamiliar with tricks in DOS, then yes, that actually works, but if you'd prefer both full-length locations, then there's nothing wrong with that :)

if you ONLY wanted to use ones in subdir.s, but-NOT those in the current dir, this will NOT work - temporarily moving those in the starting-directory somewhere-else first should, but i'm sure someone aware of how to add some additional flag or operator or something, could improve on this answer :) If you're happy to type in both locations in a reliable FULL way, then ignore this last point.


in case you're wondering, do-not specify file types for the destination - just specify a directory(location to be coptied-TO ) - the filtering you might say ... happens at the point where i've got (*.aiff)

good luck!