Batch to copy a file recursively to mult dirs

January 18, 2011 at 21:03:34
Specs: Windows 7
Short and sweet, I need a batch file to do something kind of weird. I need it to go through a folder (and its subdirectories), and for each file of extension ".foo" I need it to place a copy of a separate file in the folder with the same name as the first file, but with the extension ".fop". To put it into an example, say I have a folder with these files:

thing.foo
whatever.foo
yup.foo

I want the batch file to look at those files, then copy ANOTHER file into that folder and rename it for each file, so I wind up with:

thing.foo
thing.fop
whatever.foo
whatever.fop
yup.foo
yup.fop

All the ".fop" files being copies of one specific file.


See More: Batch to copy a file recursively to mult dirs

Report •


#1
January 19, 2011 at 08:29:44
I think...

@echo off
for /f "tokens=*" %%a in ('dir *.foo /b /s') do (
  copy "%%a\..\*.foo" "%%a\..\*.fop"
)

Will work. Note this code will be slow, because it will run the copy command for each instance of a foo file when it has already copied all foos to fops. This might be better...

@echo off
for /f "tokens=*" %%a in ('dir *.foo /b /s') do (
  if not exist "%%a\..\*.fop" copy "%%a\..\*.foo" "%%a\..\*.fop"
)

but with this it will only run once where with the first code it will run every time.


Report •

#2
January 19, 2011 at 18:52:49
That doesn't QUITE do what I needed to. I think the part I wasn't clear enough on is that all of those *.fop files ARE NOT copies of the *.foo files, they are copies of some OTHER file. In fact, ALL of them are copies of the SAME file held in some arbitrary directory, say "C:\Temp\File.fop".

Report •

#3
January 27, 2011 at 23:02:16
...anybody still checking this? I've tried poking around and I've got nothing.

Report •

Related Solutions

#4
January 28, 2011 at 07:34:06
You have to be more specific in how your write your question. I have no idea what you want. I gave you the tools all you have to do is modify them to do what you want.

If it is just that you simply want to copy a file to other sub directories then...

@echo off
for /f "tokens=*" %%a in ('dir *.foo /b /s') do (
copy "c:\temp\myfile.fop" "%%a\..\myfile.fop"
)


Report •

#5
February 5, 2011 at 11:31:05
great code. Thanks! Helped me develop a code for a different purpose.

Report •


Ask Question