'====================================================================
'= Enumerate and Update File Contents VBScript sample - Copyright © 2008, Dave Moats
'=
'= This sample is provided 'as-is', without any express or implied warranty.
'= In no event will the authors be held liable for any damages arising from
'= the use of this sample code.
'=
'= Permission is granted to anyone to use this sample code for any purpose,
'= including commercial applications, subject to the following restrictions:
'=
'= The origin of this code must not be misrepresented;
'=       you must not claim that you wrote the original code.
'=       If you use this code, an acknowledgment in the
'=       documentation is requested - shown below:
'=
'= Portions Copyright © 2008, Dave Moats (http://www.davemoats.com/).
'=
'====================================================================
'====================================================================
'== enumUpdateFiles.vbs    -    script to enumerate all the folders and
'==                                     files in a specified folder and then update
'==                                     all the files based on specific criteria
'==                                     this is helpful for a folder structure that
'==                                     contains a web site that is configured for
'==                                     development and needs to be changed for
'==                                     the production environment
'====================================================================
option explicit

'====================================================================
'==    windows script host does not have an easy way to check for
'==    syntax errors, the only way to do this is to run the entire
'==    script, to work around this issue, uncomment the wscript.quit
'==    command below then run the script, this will get the wsh
'==    environment to check the script for syntax errors and the
'==    script will exit when it hits the wscript.quit command, so
'==    the entire will not be exceuted
'====================================================================
'wscript.quit

const forreading = 1
const forwriting = 2

dim startFolder, fso, objFolder

if wscript.arguments.count = 0 then
   wscript.echo ( "Usage: enumUpdateFiles.vbs <path to folder to process>")
   wscript.quit ( )
elseif wscript.arguments.item ( 0) = "/?" Or wscript.arguments.item ( 0) = "-?" then
   wscript.echo ( "Usage: enumUpdateFiles.vbs <path to folder to process>")
   wscript.quit ( )
else
   startFolder = wscript.arguments.item ( 0)
end if

set fso = createobject ( "Scripting.FileSystemObject")

set objFolder = fso.getfolder ( startFolder)

listFolders ( objFolder )

set objFolder = nothing
set fso = nothing

wscript.quit

'====================================================================
'== sub listFolders    -    enumerates all the folders under the target
'==                                  folder
'====================================================================
sub listFolders ( inFolder )

   dim subFolders, folder, objSubFolder

   '==================================================================
   '== ignore any folder does not exist errors
   '==================================================================
   on error resume next

   set subFolders = inFolder.subfolders

   for each folder in subFolders

      wscript.echo "ok - " & folder.path

      set objSubFolder = fso.getfolder ( folder.path )

      wscript.echo "    Folder: " & objSubFolder.path

      listFolders ( objSubFolder )

      updateFiles ( objSubFolder )

      set objSubFolder = nothing

   next

end sub

'====================================================================
'== 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


Copyright © 2005 - 2013 Dave Moats.  All rights reserved.  Links: Copyright © by their respective owners.
NO WARRANTIES EXTENDED.  Void where prohibited by law.  Please report any issues or broken links.
You may link to this site freely from your own site.  You may quote from this site, but please include a link to the original source on the originating site.