Windows batch copy files from subfolders to one folder
Asked Answered
B

4

16

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!

Bartz answered 30/7, 2012 at 11:33 Comment(0)
P
40

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

Picker answered 30/7, 2012 at 11:54 Comment(3)
Separate files patterns with commas! Make a .bat and replace d:\downloads with %1 and d:\temp\%%~nxa with %2. Then you will have a reusable command, I put my Dropbox\bin into PATH so I can use it in all my computers. A great bat script, thanks!Exeunt
To automatically overwrite instead of asking for each one ad /y to the copy command.Woermer
Hehe, especially deal with cosmic events ;)Servitor
P
6

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.

Polymorphous answered 22/4, 2014 at 9:55 Comment(1)
This is actually a pretty easy way of doing it! :)Ghats
F
4
pushd D:\Source
   for /r %%a in (*.?*) do (
       MOVE "%%a" "D:\Destination folder\%%~nxa"
   )
popd
Felipa answered 27/12, 2013 at 18:18 Comment(1)
this will l take all files from subfolder and move them to destination folder you can also change the move to copyFelipa
A
1

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.

Assiduous answered 15/6, 2013 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.