Solved

Replace a file in multiple locations

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now