' This script was developed by Rory Knowles of ASPBahamas.com  
' Note, save this script as a .vbs file.  
'----------------------------  
Public Function DoesFolderExist(byVal folderName) 
  dim fso 
  dim var 
  Set fso = CreateObject("Scripting.FileSystemObject") 
  If (fso.FolderExists(folderName)) Then 
     DoesFolderExist ="true"   
  end if 
  Set fso = nothing 
End function 
   
'----------------------------  
   
Sub badFolder(byVal folderName) 
  msgBox("Folder: " & folderName &", does not exist! Operation Cancelled") 
End Sub 
   
'----------------------------  
   
Sub operationCancelled() 
    msgBox("Replace operation cancelled!") 
End Sub 
   
'----------------------------  

'----------------------------  
   
Sub ParseFile(byVal objFile, byVal strOld, byVal strNew, byRef replaceCNT) 
 ' Developed by Ryan Trudelle-Schwarz for www.mamanze.com   
   const ForReading = 1 
   const ForWriting = 2 
   
   Dim objTextStream 
   Dim strInclude 
   
 ' Grab all the text out of the file.   
   Set objTextStream = objFile.OpenAsTextStream(ForReading) 
     strInclude = objTextStream.ReadAll 
   objTextStream.Close 
   
   Set objTextStream = Nothing 
   
 ' Check to see if the string we want to replace is even in the file, if   
 ' not then don't waste the time on replacing it.   
   If InStr(strInclude,strOld) > 0 Then 
      
 ' Do the Replace   
   strInclude = Replace(strInclude,strOld,strNew) 
   
 ' count how many files replaced  
   replaceCNT = replaceCNT + 1 
   
 ' Update the file   
   Set objTextStream = objFile.OpenAsTextStream(ForWriting) 
     objTextStream.Write strInclude 
   objTextSTream.Close 
   
   Set objTextStream = Nothing 
   End If 
End Sub 

'----------------------------  
   
Sub ReplaceFolder(byRef objFolder, byVal strOld, byVal strNew, byRef replaceCNT) 
  Dim objFile, objSubFolder 
   
 ' Loop through the files and parse each one.   
   For Each objFile in objFolder.Files
	If InStr(objfile,"eudora.ini") > 0 Then
		Call ParseFile(objFile, strOld, strNew, replaceCNT) 
	End If
   next'objFile   
   
 ' Loop through the sub folders and call self on each folder.   
   For Each objSubFolder in objFolder.SubFolders 
     Call ReplaceFolder(objSubFolder, strOld, strNew, replaceCNT) 
   Next'objSubFolder   
End Sub 
   
'----------------------------  
   
Sub ReplaceAll(byVal strFolder, byVal strOld, byVal strNew, byRef replaceCNT) 
   dim objFSO, objFolder 
   
 ' Setup the FSO Object   
   Set objFSO = CreateObject("Scripting.FileSystemObject") 
   
 ' Get the root folder   
   Set objFolder = objFSO.GetFolder(strFolder) 
 ' Do the Replace on the root folder.   
   Call ReplaceFolder(objFolder, strOld, strNew, replaceCNT) 
End Sub 


'-----------------------------  
   
Sub CheckstrNew(byVal strFolder, byVal strOld, iteration) 
    dim strNew 
    'strNew = InputBox("New Word:","New Word - Search and Replace") 
    strNew ="mail2.abac.edu"
    if strNew <>"" then 
       dim replaceCNT 
       replaceCNT = 0

       call ReplaceAll(strFolder, strOld, strNew, replaceCNT) 
       if replaceCNT <> 0 then 
          'msgBox(strFolder & " containing the old address:" & strOld &", were edited with the new address:" & strNew) 
          iteration = iteration + 1
       'else 
          'msgBox("No files in " & strFolder & " contained the old address:" & strOld &", nothing was replaced") 
       end if 
    else 
       call operationCancelled() 
    end if 
End Sub 
   
'-----------------------------  
   
Sub CheckstrOld(byVal strFolder, iteration) 
    dim strOld 
    'strOld = InputBox("Old Word:","Old Word - Search and Replace") 
    strOld="mail.abac.edu"
    if strOld <>"" then 
       call CheckstrNew(strFolder, strOld, iteration) 
    else 
       call operationCancelled() 
    end if 
End Sub 

'-----------------------------  
   
Sub CheckFolderEntry(strFolder, iteration) 
    'strFolder = InputBox("Enter Folder To Search (eg: c:\test)","Enter Folder - Search and Replace") 
    'strFolder="C:\Program Files\Qualcomm\Eudora"
    'Msgbox strfolder
    if strFolder <>"" then 
       if DoesFolderExist(strFolder) ="true" then 
          call CheckstrOld(strFolder, iteration) 
       'else 
          'call badFolder(strFolder) 
       end if 
    else 
       call operationCancelled() 
    end if 
End Sub 
   
'------------------------------  
   
Sub startRoutines() 
    iteration = 0
    Userid = InputBox("Enter the username for logging onto your PC. If you are unsure, contact Tech Support at 3228.","Enter the username for logging onto your PC.") 
    MsgBox("Looking in Eudora for mail.abac.edu  to replace with mail2.abac.edu. Be patient until you see message indicating that operation has completed.")
    Call CheckFolderEntry("C:\Program Files\Qualcomm\Eudora", iteration)
    Call CheckFolderEntry("C:\Documents and Settings\" & Userid & "\application data\Qualcomm", iteration)
    Call CheckFolderEntry("C:\Qualcomm\Eudora", iteration)
    Call CheckFolderEntry("C:\Eudora", iteration)    
    if iteration > 0 Then
       MsgBox("Completed: " & iteration & " occurrence(s) changed from mail.abac.edu to mail2.abac.edu. Thank you for waiting.")
    Else Msgbox("Completed: No changes occurred. Please complete the manual steps to change Eudora.")
    End If
End Sub 
   
'-------------------------------  
   
call startRoutines() 

