Using replace to update web related files

02/14/08

Permalink 06:14:48 pm, by dave Email , 326 words, 346 views   English (US)
Categories: Windows Scripting, Wsh Scripts

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.

[More:]

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.



Did you like this post? If so, Share it!  del.icio.us digg reddit slashdot this article Facebook Twitter MySpace Email



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:

Misc

Who's Online?

  • Guest Users: 4

powered by b2evolution free blog software