?
Solved

Replace a file in multiple locations

Posted on 2007-03-30
3
Medium Priority
?
685 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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