Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows batch copy files from subfolders to one folder

I had tried to make batch script that copies all *.tif files located in D:\images(random named subfolders here) to d:\all.

xcopy D:\Downloads\*.TIF D:\temp\ /s

works, but it copies with all folder tree. I tried to use other keys, but its dont works. Thanks for help!

like image 405
Sergii Rechmp Avatar asked Jul 30 '12 11:07

Sergii Rechmp


4 Answers

FOR is your friend. Read HELP FOR on the /R option and the %~nx variable substitution; and then try this very simple code.

   pushd d:\downloads
   for /r %%a in (*.tif) do (
     echo COPY "%%a" "d:\temp\%%~nxa"
   )
   popd

watch carefully the results and then remove the ECHO command.

You will have to refine the code to cope with errors, duplicate names, edge cases, names with reserved characters, race conditions, cosmic events...

like image 172
PA. Avatar answered Sep 22 '22 19:09

PA.


Searched files using windows file explorer for e.g. *.gif , I got files in search window, used Edit=>Select All , copy and then pasted to desired folder. This copied all the gif files in all sub directories to single folder. For large number of files, it sometimes hangs/not responding, but otherwise works ok.

like image 24
Sham Yemul Avatar answered Sep 26 '22 19:09

Sham Yemul


pushd D:\Source
   for /r %%a in (*.?*) do (
       MOVE "%%a" "D:\Destination folder\%%~nxa"
   )
popd
like image 36
Stur Avatar answered Sep 26 '22 19:09

Stur


You can also use the XXCOPY freeware. Works like XCOPY, but when you use a /SG parameter, it flattens the sub-directories. See how to use it here.

like image 24
Ev0oD Avatar answered Sep 25 '22 19:09

Ev0oD