tags:

views:

444

answers:

3

i have an edited version of a config file specific for my machine. i have the same config file in multiple different directories in my development folder. i want to, in a single bat file, replace all instances of this file with my edited one.

So in pusedo code: Take C:\edited.config and copy to C:\Projects\ /s wherever original.config is found

i want the final file to have the name of original.config, not edited.config

so i am guessing i need some combination of a FOR, a rename and copy or something like that

is this easier to do in Powershell?

can anybody help? Thanks

+3  A: 

I blogged about this a little bit ago at http://jamesewelch.wordpress.com/2008/05/01/how-to-write-a-dos-batch-file-to-loop-through-files/

I think your solution will look something similar to (below is untested but used to show general idea)

for /f %%a IN ('dir /b *.config') do copy c:\master.config %%a

There's probably a switch there on the copy to suppress file overwrite warnings, but I don't remember what the switch is. This will copy your master.config and overwrite your local file (variable of %%a).

Jim W
the /y option suppresses prompts
shufler
+1  A: 

I'm amazed what DOS batch file experts make work. Since I'm not one of them, I take an approach that's pragmatic for me. It might work for you as well.

  1. Get a list of destination folders

    C: Cd\ Dir original.config /s > original.bat

  2. Edit original.bat in your favorite text editor (I like Notepad++)

  3. Search for "original.config" and replace with "" (empty string)

  4. Insert the text "Xcopy C:\edited.config " at the front of each line

  5. Proof-read the result to be sure it's what you want. If you're not sure put an "Echo " in front of each line for a dry run.

  6. Run the batch file.

Eric J.
yeah some of the wizardry these guys come up with is amazing.
ryancrawcour
+1  A: 
@echo off
C:
cd \Projects
FOR /F "tokens=*" %%G IN ('DIR /B /S original.config') DO xcopy /y c:\edited.config %%G
DmitryK