Using replace to update web related files
02/14/08
Using replace to update web related files
I was working on some web stuff for a friend and his development environment and my development environment are extremely different. After a couple of go rounds with global search and replace to fix the differences and always missing stuff (of course). I took one of my existing scripts and modified one of the sub routines to do this for me.
The updateFiles subroutine expects to be passed a filesystemobject folder object. It then enumerates all the files in the folder, opens each one and tries to make the updates that are specified by the replace statements.
'====================================================================
'== sub updateFiles - enumerate and update all the files located
'== in the target folder
'====================================================================
sub updateFiles( inFolder )
dim objFiles, file, file2Update, orgInfo, newInfo
set objFiles = inFolder.files
for each file In objFiles
if instr( 1, file.name, ".HTML" ) <> 0 or instr( 1, file.name, ".html" ) <> 0 _
or instr( 1, file.name, ".CSS" ) <> 0 or instr( 1, file.name, ".css" ) <> 0 then
wscript.echo "Updating file: " & inFolder & "\" & file.name
set file2Update = fso.opentextfile(inFolder & "\" & file.name, forreading)
orgInfo = file2Update.readall
file2Update.close
newInfo = Replace(orgInfo, "http://localhost/somepath/", "http://production_host/")
newInfo = Replace(newInfo, "http://localhost/", "/")
Set file2Update = fso.opentextfile(inFolder & "\" & file.name, forwriting)
file2Update.writeline newInfo
file2Update.close
wscript.echo "Completed updating file: " & inFolder & "\" & file.name
end if
next
end sub
There are some potential gotchas with this subroutine, the main one being that you have to be careful of the order in which you issue the replace statements. You must use the most specific first and then work through to the least specific. To ensure you don't replace something you plan to key on later.
I hope this post helped you out. If it didn't, I am always looking for new scripts to add so submit a request for your question or need and I will see if I can answer it.
Pingbacks:
No Pingbacks for this post yet...
This post has 6 feedbacks awaiting moderation...
Scripts
This is somewhere I can post interesting snippets as I come across them. Hopefully some folks out there will find this helpful.
Search
Follow Me:
Categories
- All
- Web Technologies (2)
- PHP (1)
- Windows Scripting (37)
- Batch Scripts (13)
- Wsh Scripts (23)
Archives
- December 2009 (2)
- March 2009 (2)
- February 2009 (3)
- January 2009 (2)
- December 2008 (4)
- November 2008 (1)
- October 2008 (1)
- February 2008 (1)
- December 2007 (3)
- July 2007 (1)
- April 2007 (1)
- February 2007 (2)
- More...
Misc
Who's Online?
- Guest Users: 4




