Replace a file in multiple locations

Posted on 2007-03-30
Last Modified: 2012-08-13
Is there a way to search for a filename and replace it with another file. Basically, I have a Windows 2003 server that contains roaming user profiles. I want to replace a file called appsrv.ini in everyone's user profile with an updated file called appsrv.ini. If there was only one file to replace, I could of course use the copy command or replace it in windows with copy and paste. The problem is I have over 4,000 roaming user profiles on this server which means I have to replace 4,000 appsrve.ini files.

Anyone know how to script this? Thanks.
Question by:mpopal
  • 2
LVL 30

Expert Comment

ID: 18824205
Try something like this. You'll have to change the settings that specify where the master copy is and where the user profile root is:

@echo off


set fileName=appsrve.ini
set masterFile=\\server1\share\dir\%fileName%
set roamingRoot=\\server1\share\users

for /f "tokens=*" %%a in ('dir /s /b /a-d "%roamingRoot%\%fileName%" 2^>NUL') do copy "%masterFile%" "%%a"

Good Luck,


Author Comment

ID: 18824295
Steve, could you do me a favor and explain everything step by step? Kind of like:

(This sets an environment variable for the file appsrv.ini)
set fileName=appsrve.ini

(This sets masterfile environment variable blah blah blah)
set masterFile=\\server1\share\dir\%fileName%

an so on....

Thanks so  much!

LVL 30

Accepted Solution

SteveGTR earned 500 total points
ID: 18825170
@echo off

Disables the echoing of each line. The @ sign disables the command itself


Makes all environment variables local to this batch file

set fileName=appsrve.ini

Sets the name of the actual file we'll be replacing

set masterFile=\\server1\share\dir\%fileName%

Points to the place where the master file resides. This uses the fileName environment variable. You'll want to set the first part to the UNC of the master file.

set roamingRoot=\\server1\share\users

Points to the base directory where all profiles exist. You'll want to set this.

for /f "tokens=*" %%a in ('dir /s /b /a-d "%roamingRoot%\%fileName%" 2^>NUL') do copy "%masterFile%" "%%a"

Loops through all files off the roamingRoot directory. Uses the /b - bare option, /s recursive, and /a-d no directories options of the dir command. The 2^>NUL redirects all error to the NUL device. You must escape the > sign using the ^ in the dir statement. The tokens=* takes each line as one token represented in the case by %%a.

You can get help for any command by typing: command /?. For example:

if /?
setlocal /?
for /?

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In a recent question ( here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question