none
Script to add two lines of text to a .js file if found on a computer's c drive

    Question

  • Hi,

    I'm looking for a script that will add two lines of text in between a specific set of text in a .js file if this file exists on the user's c: drive. Also if the two lines of text exist then replace with newer of the two lines.

    I found this example of finding and replacing a character in a file but I need to find a script that will add two new lines of text but also replace if this line exists. http://blogs.technet.com/heyscriptingguy/archive/2008/01/17/how-can-i-use-windows-powershell-to-replace-characters-in-a-text-file.aspx

    So here's another explanation of what I need:

    If the following files exists C:\Program Files (x86)\Mozilla Firefox\defaults\pref\firefox.js or  C:\Program Files\Mozilla Firefox\defaults\pref\firefox.js

    open the file and add the following two lines:

    New text
    New text

    in-between the existing lines of text. So the new text will be placed here:

    **************Existing text truncated************************************
    pref("network.protocol-handler.warn-external.nntp", false);

    New text
    New text

    // By default, all protocol handlers are exposed.  This means that
    **************Existing text truncated***********************************

    You may be saying why don't you create a script that will just replace the firefox.js file with a new one that has this information in it. This will not work for me because it will blow away previous individual settings that have been set. I just need to add two lines of text.

    Any help is greatly appreciated!


    Thanks,

    TKE402
    Monday, February 08, 2010 7:54 PM

Answers

  • Sorry about that... didn't have much time last week. 

    The checking functionality has been added so it will only write the update once - I only tested a couple times with a file full of junk so you may want to fully test it in your environment.

    These still apply:  (1) Make sure the TARGET_STRING exactly matches the complete line in firefox.js. (2) If you want to get rid of the debug messages, just prepend the Wscript.echo statements with either REM or '

    To answer your question from the 18th, the script checks two folder locations one at a time by default (the values assigned to constants SOURCE_FOLDER_ONE and SOURCE_FOLDER_TWO are the two folders).  The way it is written, it checks the ONE first and TWO second.  If a file named "firefox.js" exists in either location it will get rewritten.  If a file named "firefox.js" exists in both locations both files will get rewritten.

    Give it a shot and let me know how it goes...


    option explicit
    const TARGET_STRING = "We want to make sure mail URLs are handled externally..."
    const SOURCE_FOLDER_ONE = "C:\Program Files (x86)\Mozilla Firefox\defaults\pref\"
    const SOURCE_FOLDER_TWO = "C:\Program Files (x86)\Mozilla Firefox\defaults\pref\backup\"
    const FILE_NAME = "firefox.js"
    dim fso
    dim stringToAdd
    set fso = CreateObject("Scripting.FileSystemObject")
    stringToAdd = "" & vbCrLf & "// iPrism Auto-login Entries" & vbCrLf & "pref(""network.automatic-ntlm-auth.trusted-uris"", ""192.168.99.42"");"
    Wscript.echo "Searching for:" & vbCrLf & vbCrLf & SOURCE_FOLDER_ONE & FILE_NAME
    call ReWriteFile(SOURCE_FOLDER_ONE & FILE_NAME, SOURCE_FOLDER_ONE & "OLD_" & FILE_NAME)
    Wscript.echo "Searching for:" & vbCrLf & vbCrLf & SOURCE_FOLDER_TWO & FILE_NAME
    call ReWriteFile(SOURCE_FOLDER_TWO & FILE_NAME, SOURCE_FOLDER_TWO & "OLD_" & FILE_NAME)
    Wscript.echo "Done"
    Wscript.quit
    
    function ReWriteFile(byval originalFilePath, byval renamedFilePath)
        if fso.FileExists(originalFilePath) then
    		Wscript.echo "File found"
    		if IsFileAlreadyRewritten(originalFilePath) then
    			Wscript.echo "File already rewritten... skipping"
    		else
    			Wscript.echo "File requires rewrite"
    			dim sourceFile
    			dim newFile
    			set sourceFile = fso.GetFile(originalFilePath)
    			sourceFile.Move(renamedFilePath)
    			set sourceFile = nothing
    			set sourceFile = fso.OpenTextFile(renamedFilePath, 1)
    			set newFile = fso.CreateTextFile(originalFilePath, True)
    			do until sourceFile.AtEndOfStream
    				dim line
    				line = sourceFile.ReadLine
    				if line = TARGET_STRING then
    					Wscript.echo "Target string found... adding new content"
    					newFile.WriteLine(line)
    					newFile.WriteLine(stringToAdd)
    				else
    					newFile.WriteLine(line)
    				end if
    			loop
    			sourceFile.close
    			newFile.close
    			set sourceFile = nothing
    			set sourceFile = fso.GetFile(renamedFilePath)
    			sourceFile.Delete true
    			Wscript.echo "Edit complete"
    		end if
        end if
    end function
    
    function IsFileAlreadyRewritten(byval thisFilePath)
    	IsFileAlreadyRewritten = false
    	dim sourceFile
    	dim oneMatch
    	dim twoMatches
    	oneMatch = false
    	twoMatches = false
    	set sourceFile = fso.OpenTextFile(thisFilePath, 1)
    	do until sourceFile.AtEndOfStream
    		dim line
    		line = sourceFile.ReadLine
    		if len(line) > 20 then
    			if instr(stringToAdd, line) > 0 then
    				if oneMatch then
    					twoMatches = true
    				else
    					oneMatch = true
    				end if
    			end if
    		end if
    	loop
    	if twoMatches then
    		IsFileAlreadyRewritten = true
    	end if
    end function
    
    
    
    Monday, February 22, 2010 5:43 PM
  • Yes.  All you have to do is reverse these two lines in the script:


    newFile.WriteLine(line)
    newFile.WriteLine(stringToAdd)

    so that they are

    newFile.WriteLine(stringToAdd)
    newFile.WriteLine(line)


    They are in the ReWriteFile function inside of the Do Until loop.  This will add the config strings before the reference string.

    • Marked as answer by ChrisTKE402 Tuesday, February 23, 2010 7:33 PM
    Tuesday, February 23, 2010 2:49 PM

All replies

  • TKE402,

    Give this a try.  This renames the original file then creates a new file.  The new file is a line by line copy of the old one, but when the target line is found the two new lines are inserted.  All should have to do is edit the constants and the variable named stringToAdd to whatever you need them to be

    Let me know if it does the trick...


    option explicit
    const TARGET_STRING = "STRING I WANT TO FIND"
    const SOURCE_FOLDER_ONE = "c:\temp\"
    const SOURCE_FOLDER_TWO = "c:\temp2\"
    const FILE_NAME = "testFile.txt"
    dim fso
    dim stringToAdd
    set fso = CreateObject("Scripting.FileSystemObject")
    stringToAdd = "" & vbCrLf & "FIRST TEXT STRING TO ADD" & vbCrLf & "SECOND TEXT STRING TO ADD"
    call ReWriteFile(SOURCE_FOLDER_ONE & FILE_NAME, SOURCE_FOLDER_ONE & "OLD_" & FILE_NAME)
    call ReWriteFile(SOURCE_FOLDER_TWO & FILE_NAME, SOURCE_FOLDER_TWO & "OLD_" & FILE_NAME)
    Wscript.quit
    
    function ReWriteFile(byval originalFilePath, byval renamedFilePath)
    	if fso.FileExists(originalFilePath) then
    		dim sourceFile
    		dim newFile
    		set sourceFile = fso.GetFile(originalFilePath)
    		sourceFile.Move(renamedFilePath)
    		set sourceFile = nothing
    		set sourceFile = fso.OpenTextFile(renamedFilePath, 1)
    		set newFile = fso.CreateTextFile(originalFilePath, True)
    		do until sourceFile.AtEndOfStream
    			dim line
    			line = sourceFile.ReadLine
    			if line = TARGET_STRING then
    				newFile.WriteLine(line)
    				newFile.WriteLine(stringToAdd)
    			else
    				newFile.WriteLine(line)
    			end if
    		loop
    		sourceFile.close
    		newFile.close
    		set sourceFile = nothing
    		set sourceFile = fso.GetFile(renamedFilePath)
    		sourceFile.Delete true
    	end if
    end function
    
    Monday, February 08, 2010 10:54 PM
  • Thank you very much for the reply. I changed the script to as follows:

    *******************************************************************************
    option explicit
    const TARGET_STRING = "We want to make sure mail URLs are handled externally..."
    const SOURCE_FOLDER_ONE = "C:\Program Files (x86)\Mozilla Firefox\defaults\pref"
    const SOURCE_FOLDER_TWO = "C:\Program Files (x86)\Mozilla Firefox\defaults\pref\backup"
    const FILE_NAME = "firefox.js"
    dim fso
    dim stringToAdd
    set fso = CreateObject("Scripting.FileSystemObject")
    stringToAdd = "" & vbCrLf & "// iPrism Auto-login Entries" & vbCrLf & "pref("network.automatic-ntlm-auth.trusted-uris", "192.168.99.42");"
    call ReWriteFile(SOURCE_FOLDER_ONE & FILE_NAME, SOURCE_FOLDER_ONE & "OLD_" & FILE_NAME)
    call ReWriteFile(SOURCE_FOLDER_TWO & FILE_NAME, SOURCE_FOLDER_TWO & "OLD_" & FILE_NAME)
    Wscript.quit

    function ReWriteFile(byval originalFilePath, byval renamedFilePath)
        if fso.FileExists(originalFilePath) then
            dim sourceFile
            dim newFile
            set sourceFile = fso.GetFile(originalFilePath)
            sourceFile.Move(renamedFilePath)
            set sourceFile = nothing
            set sourceFile = fso.OpenTextFile(renamedFilePath, 1)
            set newFile = fso.CreateTextFile(originalFilePath, True)
            do until sourceFile.AtEndOfStream
                dim line
                line = sourceFile.ReadLine
                if line = TARGET_STRING then
                    newFile.WriteLine(line)
                    newFile.WriteLine(stringToAdd)
                else
                    newFile.WriteLine(line)
                end if
            loop
            sourceFile.close
            newFile.close
            set sourceFile = nothing
            set sourceFile = fso.GetFile(renamedFilePath)
            sourceFile.Delete true
        end if
    end function

    **********************************************************************
    But I receive the following Windows Script Host pop up:

    Line: 9
    Char: 78
    Error: Expected end of statement
    Code: 800A0401
    Source: Microsoft VBScript compilation error


    Perhaps this is because of the characters I have in the string? Here's the text that I need including the special characters:

    // iPrism Auto-login Entries
    pref("network.automatic-ntlm-auth.trusted-uris", "192.168.99.42");

    Thanks a bunch!!!

    TKE402

    Wednesday, February 10, 2010 4:06 PM
  • The quotes caused the compilation issue...  Give this a shot:

    option explicit
    const TARGET_STRING = "We want to make sure mail URLs are handled externally..."
    const SOURCE_FOLDER_ONE = "C:\Program Files (x86)\Mozilla Firefox\defaults\pref"
    const SOURCE_FOLDER_TWO = "C:\Program Files (x86)\Mozilla Firefox\defaults\pref\backup"
    const FILE_NAME = "firefox.js"
    dim fso
    dim stringToAdd
    set fso = CreateObject("Scripting.FileSystemObject")
    stringToAdd = "" & vbCrLf & "// iPrism Auto-login Entries" & vbCrLf & "pref(""network.automatic-ntlm-auth.trusted-uris"", ""192.168.99.42"");"
    call ReWriteFile(SOURCE_FOLDER_ONE & FILE_NAME, SOURCE_FOLDER_ONE & "OLD_" & FILE_NAME)
    call ReWriteFile(SOURCE_FOLDER_TWO & FILE_NAME, SOURCE_FOLDER_TWO & "OLD_" & FILE_NAME)
    Wscript.quit
    
    function ReWriteFile(byval originalFilePath, byval renamedFilePath)
        if fso.FileExists(originalFilePath) then
            dim sourceFile
            dim newFile
            set sourceFile = fso.GetFile(originalFilePath)
            sourceFile.Move(renamedFilePath)
            set sourceFile = nothing
            set sourceFile = fso.OpenTextFile(renamedFilePath, 1)
            set newFile = fso.CreateTextFile(originalFilePath, True)
            do until sourceFile.AtEndOfStream
                dim line
                line = sourceFile.ReadLine
                if line = TARGET_STRING then
                    newFile.WriteLine(line)
                    newFile.WriteLine(stringToAdd)
                else
                    newFile.WriteLine(line)
                end if
            loop
            sourceFile.close
            newFile.close
            set sourceFile = nothing
            set sourceFile = fso.GetFile(renamedFilePath)
            sourceFile.Delete true
        end if
    end function
    
    Wednesday, February 10, 2010 9:33 PM
  • Thanks again for the reply. Now it doesn't look like it does anything. I do not see the backup file being created and the entry isn't in the config file. Is it possible to put in stops at major points in the script to make sure it's major step works?

    Thanks,

    TKE402
    Thursday, February 11, 2010 6:57 PM
  • Found a bug... didn't have a "\" separating the file name from the rest of the path.  Also added some debug/status messages.

    Some things to note:  (1) Make sure the TARGET_STRING exactly matches the complete line in firefox.js. (2) Each time this script is run it will add the new lines to the file.  Checking funcitonality can be added. (3) If you want to get rid of the debug messages, just prepend the Wscript.echo statements with either REM or ' 

    Give it a shot.


    option explicit
    const TARGET_STRING = "We want to make sure mail URLs are handled externally..."
    const SOURCE_FOLDER_ONE = "C:\Program Files (x86)\Mozilla Firefox\defaults\pref\"
    const SOURCE_FOLDER_TWO = "C:\Program Files (x86)\Mozilla Firefox\defaults\pref\backup\"
    const FILE_NAME = "firefox.js"
    dim fso
    dim stringToAdd
    set fso = CreateObject("Scripting.FileSystemObject")
    stringToAdd = "" & vbCrLf & "// iPrism Auto-login Entries" & vbCrLf & "pref(""network.automatic-ntlm-auth.trusted-uris"", ""192.168.99.42"");"
    Wscript.echo "Searching for:" & vbCrLf & vbCrLf & SOURCE_FOLDER_ONE & FILE_NAME
    call ReWriteFile(SOURCE_FOLDER_ONE & FILE_NAME, SOURCE_FOLDER_ONE & "OLD_" & FILE_NAME)
    Wscript.echo "Searching for:" & vbCrLf & vbCrLf & SOURCE_FOLDER_TWO & FILE_NAME
    call ReWriteFile(SOURCE_FOLDER_TWO & FILE_NAME, SOURCE_FOLDER_TWO & "OLD_" & FILE_NAME)
    Wscript.echo "Done"
    Wscript.quit
    
    function ReWriteFile(byval originalFilePath, byval renamedFilePath)
        if fso.FileExists(originalFilePath) then
    		Wscript.echo "File found"
            dim sourceFile
            dim newFile
            set sourceFile = fso.GetFile(originalFilePath)
            sourceFile.Move(renamedFilePath)
            set sourceFile = nothing
            set sourceFile = fso.OpenTextFile(renamedFilePath, 1)
            set newFile = fso.CreateTextFile(originalFilePath, True)
            do until sourceFile.AtEndOfStream
                dim line
                line = sourceFile.ReadLine
                if line = TARGET_STRING then
    				Wscript.echo "Target string found... adding new content"
                    newFile.WriteLine(line)
                    newFile.WriteLine(stringToAdd)
                else
                    newFile.WriteLine(line)
                end if
            loop
            sourceFile.close
            newFile.close
            set sourceFile = nothing
            set sourceFile = fso.GetFile(renamedFilePath)
            sourceFile.Delete true
    		Wscript.echo "Edit complete"
        end if
    end function
    
    Friday, February 12, 2010 7:30 PM
  • U DA MAN!!!

    Works great. Yes could you add checking please so that it doesn't add the line every-time. Also if there are two locations the file can be in for example:
    C:\Program Files (x86)\Mozilla Firefox\defaults\pref\ and 
    C:\Program Files\Mozilla Firefox\defaults\pref\


    Should I just make two copies of the code one with the (x86) location and one without the (x86)? So essentially have two scripts running as one?

    Thanks,

    TKE402
    Thursday, February 18, 2010 3:02 PM
  • ShooterScott,

    Any chance of getting checking added to the script so that it doesn't add the same line over and over. I promise I won't bug you again!

    Thanks,

    TKE402
    Monday, February 22, 2010 2:14 PM
  • Sorry about that... didn't have much time last week. 

    The checking functionality has been added so it will only write the update once - I only tested a couple times with a file full of junk so you may want to fully test it in your environment.

    These still apply:  (1) Make sure the TARGET_STRING exactly matches the complete line in firefox.js. (2) If you want to get rid of the debug messages, just prepend the Wscript.echo statements with either REM or '

    To answer your question from the 18th, the script checks two folder locations one at a time by default (the values assigned to constants SOURCE_FOLDER_ONE and SOURCE_FOLDER_TWO are the two folders).  The way it is written, it checks the ONE first and TWO second.  If a file named "firefox.js" exists in either location it will get rewritten.  If a file named "firefox.js" exists in both locations both files will get rewritten.

    Give it a shot and let me know how it goes...


    option explicit
    const TARGET_STRING = "We want to make sure mail URLs are handled externally..."
    const SOURCE_FOLDER_ONE = "C:\Program Files (x86)\Mozilla Firefox\defaults\pref\"
    const SOURCE_FOLDER_TWO = "C:\Program Files (x86)\Mozilla Firefox\defaults\pref\backup\"
    const FILE_NAME = "firefox.js"
    dim fso
    dim stringToAdd
    set fso = CreateObject("Scripting.FileSystemObject")
    stringToAdd = "" & vbCrLf & "// iPrism Auto-login Entries" & vbCrLf & "pref(""network.automatic-ntlm-auth.trusted-uris"", ""192.168.99.42"");"
    Wscript.echo "Searching for:" & vbCrLf & vbCrLf & SOURCE_FOLDER_ONE & FILE_NAME
    call ReWriteFile(SOURCE_FOLDER_ONE & FILE_NAME, SOURCE_FOLDER_ONE & "OLD_" & FILE_NAME)
    Wscript.echo "Searching for:" & vbCrLf & vbCrLf & SOURCE_FOLDER_TWO & FILE_NAME
    call ReWriteFile(SOURCE_FOLDER_TWO & FILE_NAME, SOURCE_FOLDER_TWO & "OLD_" & FILE_NAME)
    Wscript.echo "Done"
    Wscript.quit
    
    function ReWriteFile(byval originalFilePath, byval renamedFilePath)
        if fso.FileExists(originalFilePath) then
    		Wscript.echo "File found"
    		if IsFileAlreadyRewritten(originalFilePath) then
    			Wscript.echo "File already rewritten... skipping"
    		else
    			Wscript.echo "File requires rewrite"
    			dim sourceFile
    			dim newFile
    			set sourceFile = fso.GetFile(originalFilePath)
    			sourceFile.Move(renamedFilePath)
    			set sourceFile = nothing
    			set sourceFile = fso.OpenTextFile(renamedFilePath, 1)
    			set newFile = fso.CreateTextFile(originalFilePath, True)
    			do until sourceFile.AtEndOfStream
    				dim line
    				line = sourceFile.ReadLine
    				if line = TARGET_STRING then
    					Wscript.echo "Target string found... adding new content"
    					newFile.WriteLine(line)
    					newFile.WriteLine(stringToAdd)
    				else
    					newFile.WriteLine(line)
    				end if
    			loop
    			sourceFile.close
    			newFile.close
    			set sourceFile = nothing
    			set sourceFile = fso.GetFile(renamedFilePath)
    			sourceFile.Delete true
    			Wscript.echo "Edit complete"
    		end if
        end if
    end function
    
    function IsFileAlreadyRewritten(byval thisFilePath)
    	IsFileAlreadyRewritten = false
    	dim sourceFile
    	dim oneMatch
    	dim twoMatches
    	oneMatch = false
    	twoMatches = false
    	set sourceFile = fso.OpenTextFile(thisFilePath, 1)
    	do until sourceFile.AtEndOfStream
    		dim line
    		line = sourceFile.ReadLine
    		if len(line) > 20 then
    			if instr(stringToAdd, line) > 0 then
    				if oneMatch then
    					twoMatches = true
    				else
    					oneMatch = true
    				end if
    			end if
    		end if
    	loop
    	if twoMatches then
    		IsFileAlreadyRewritten = true
    	end if
    end function
    
    
    
    Monday, February 22, 2010 5:43 PM
  • Awesome works like a charm! Now for the nit picky stuff. Is it possible that when the script finds the reference string
    "We want to make sure mail URLs are handled externally..."

    That it adds the config strings to the line before the reference string rather than after it? Unfortunately using another reference string may not work since I know the "We want to make sure mail URLs are handled externally..." is pretty much a static reference. I hope I'm making sense. If it's a big deal to do this then don't sweat it. You have helped me more than enough!


    Thanks,

    TKE402
    Tuesday, February 23, 2010 1:23 PM
  • Yes.  All you have to do is reverse these two lines in the script:


    newFile.WriteLine(line)
    newFile.WriteLine(stringToAdd)

    so that they are

    newFile.WriteLine(stringToAdd)
    newFile.WriteLine(line)


    They are in the ReWriteFile function inside of the Do Until loop.  This will add the config strings before the reference string.

    • Marked as answer by ChrisTKE402 Tuesday, February 23, 2010 7:33 PM
    Tuesday, February 23, 2010 2:49 PM
  • Thanks again ShooterScott. It works like a charm!

    TKE402
    Tuesday, February 23, 2010 7:34 PM