Solved

Replace a file in multiple locations

Posted on 2007-03-30
3
681 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 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