12

I need to search a specified folder and its subdirectories for file(s), then copy them to a specified folder somewhere else. Is it possible to use dir and copy with a pipe?

dir /s foo???.txt | copy g:\someplace

The above doesn't work. The left side of the pipe finds what I want it to, but I can't get it to copy the found files. I'm using Windows 7 command prompt.

2
  • What type of file do you need out of curiosity? Would a wildcard not suffice?
    – Dave
    Oct 3, 2013 at 15:02
  • I need to use the wildcards (???--"?" is a single digit wildcard ) in the command such that the above would capture foo_42, foo_01, etc and those would be in various subfolders. Oct 3, 2013 at 15:12

2 Answers 2

12

You could just use xcopy:

xcopy /s foo???.txt g:\someplace

This will hold the directory structure.

If you don't want the directory structure you can do the following:

for /r %i in (foo???.txt) do xcopy /Y "%i" g:\someplace

If you are using this in a batchfile you should double the % like this:

for /r %%i in (foo???.txt) do xcopy /Y "%%i" g:\someplace
5
  • Actually, I need to tweek it a bit. Can it be done without keeping the directory structure? Oct 3, 2013 at 21:50
  • Haha, that's what i was afraid of :) (that's why I specifically stated it). But what would you do with duplicate filenames? It can't be done with xcopy itself but you could do it with piping something to robocopy (or maybe just copy but in a batchfile). What version of Windows do you have?
    – Rik
    Oct 3, 2013 at 21:56
  • Windows 7. There will not be any duplicates for my application. Oct 3, 2013 at 22:05
  • Added the method for copying to one directory. Luckily no need for robocopy. Just use for /r.
    – Rik
    Oct 3, 2013 at 22:08
  • 1
    You rock. I wish I could upvote you again. Thanks! Oct 3, 2013 at 22:19
2

with Powershell u can run this:

cmd.exe /c dir *.txt /s /b | copy -Destination d:\md

part cmd.exe part Powershell bit more understandable but there overlapping

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .