VBScript Newbie - Final tweaks, help needed
I am new to the scripting world and could use a little assistance. I've played around with scripts here and there, but recently I've become more involved and have my first script that I've been working on that needs a little help. Please see the code below and advise on how I might tweak it a little if there is any redundancies that could be coded more cleanly, etc... I have tested it and it is currently working, but I'd also like to add one more report as well.
I have REMed out few lines that run a subroutine to log the results of the changes so you can see what I'm doing here.
I'd also like to log if the file \ folder does not exist - the script has run successfully, but no files were detected and no files have been changed. Exit SUCCESS
I do want to keep the variable types listed in the names as I've been told this makes it much easier to troubleshoot and good practive for other languages if I decide to move on further with coding.
I also like to keep the coding part very generic so all user data is defined in the Dim / Const area and can be easily recognized and changed for future use in similar projects.
Here is my script:
Option Explicit On Error GoTo 0 Const FORREADING = 1
Const FORWRITING = 2
Const CREATE = True Dim Counter : Counter = 1
Dim intNewTextCounter1 : intNewTextCounter1 = 0
Dim intNewTextCounter2 : intNewTextCounter2 = 0
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objMyFile
Dim objMyTemp
Dim strEditFile1 : strEditFile1 = strFilePath & "default.txt"
Dim strFilePath
Dim strMyLine
Dim strNewText1 : strNewText1 = "New text 1"
Dim strNewText2 : strNewText2 = "New text 2"
Dim strOriginalText1 : strOriginalText1 = "Text to be replaced 1"
Dim strOriginalText2 : strOriginalText2 = "Text to be replaced 2"
Dim strTempFile Dim arrFileFolders : arrFileFolders = Array("C:\Home\", _
"C:\Program Files\Home\")
For Each strFilePath In arrFileFolders
strEditFile1 = strFilePath & "default.txt"
strTempFile = strFilePath & "EditFile.tmp" 'Routine begins here If objFSO.FileExists(strEditFile1) Then
'LogProgress("The file [" & strEditFile1 & " has been found]") Set objMyFile = objFSO.OpenTextFile(strEditFile1, FORREADING)
Set objMyTemp = objFSO.OpenTextFile(strTempFile, FORWRITING, CREATE)
Do While Not objMyFile.AtEndofStream
strMyLine = objMyFile.ReadLine
If InStr(1, strMyLine, strOriginalText1, vbTextCompare) Then
strMyLine = Replace(strMyLine, strOriginalText1, strNewText1, 1, -1, vbtextcompare)
intNewTextCounter1 = intNewTextCounter1 + Counter
End If
If InStr(1, strMyLine, strOriginalText2, vbTextCompare) Then
strMyLine = Replace(strMyLine, strOriginalText2, strNewText2, 1, -1, vbtextcompare)
intNewTextCounter2 = intNewTextCounter2 + Counter End If objMyTemp.WriteLine strMyLine
Loop
objMyFile.Close
objMyTemp.Close
'LogProgress("The original text [" & strOriginalText1 & "] has been detected in " & strEditFile1 & " and changed to [" & strNewText1 & "] " & intNewTextCounter1 & " times") 'LogProgress("The original text [" & strOriginalText2 & "] has been detected in " & strEditFile1 & " and changed to [" & strNewText2 & "] " & intNewTextCounter2 & " times") intNewTextCounter1=0
intNewTextCounter2=0
objFSO.DeleteFile strEditFile1
objFSO.MoveFile strTempFile, strEditFile1
End If Next Set objFSO = Nothing ' I've been told that this prevents memory leakage
----------------------------------------------------------------------------------
Your thoughts?
Answers (0)
Be the first to answer this question