Solved

Replace a file in multiple locations

Posted on 2007-03-30
3
682 Views
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.
0
Comment
Question by:mpopal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 30

Expert Comment

by:SteveGTR
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

setlocal

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,
Steve

0
 
LVL 3

Author Comment

by:mpopal
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!

0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
ID: 18825170
@echo off

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

setlocal

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 /?
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

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 …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

728 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