Question : File Find/Replace(or Amend) string/update file

I have to make some changes to a java properties file.  Below are my requirements, my problems and my current code that almost works.
**does not have to be vb**
Requirements:
1) Locate a file that is always in the same location for each user - subfolder in user's Documents and Settings
2) Change or amend a string within the file
Problems:
1) Not sure how to apply %Homepath% or at least %UserName% in the script
2) Every time the script runs any s already there stays - I end up with https -> httpss -> httpsss and so on
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
Imports System.IO
Imports System.Text.RegularExpressions
Module These

    Private Property oShell As Object

    Private Property user As Object

    Private Property comp As Object

    Private Property RegExpTest As Object

    Sub Main()
        Dim re As New System.Text.RegularExpressions.Regex("/http/")
        Dim reader As StreamReader
        Dim cleaner As StreamWriter
        Dim response As String
        Dim title As String
        Dim input As String
        oShell = CreateObject("WScript.Shell")
        user = oShell.ExpandEnvironmentStrings("%USERNAME%")
        comp = oShell.ExpandEnvironmentStrings("%ComputerName%")
        If System.IO.File.Exists("C:\Documents and Settings\samurijack\holdingpen\conf\these.properties") = True Then
            reader = New StreamReader(New FileStream("C:\Documents and Settings\samurijack\holdingpen\conf\these.properties", 

FileMode.Open))
            cleaner = New StreamWriter(New FileStream("c:\these.properties", FileMode.Create))
            input = reader.ReadLine()
            Do
                input = reader.ReadLine()
                cleaner.WriteLine(Regex.Replace(input, "http", "https"))
            Loop Until reader.EndOfStream()

            cleaner.Close()
            reader.Close()
        End If

        Dim filesys
        filesys = CreateObject("Scripting.FileSystemObject")
        If filesys.FileExists("c:\these.properties") Then
            filesys.CopyFile("c:\these.properties", "C:\Documents and Settings\samurijack\holdingpen\conf\")
        Else
            title = "File Not Found"
            response = MsgBox("Call Ghost Busters", MsgBoxStyle.OkOnly, title)
        End If
    End Sub

    Private Function WScript() As Object
        Throw New NotImplementedException
    End Function

    Private Function objRegExp() As Object
        Throw New NotImplementedException
    End Function

    Private Function strPattern() As Object
        Throw New NotImplementedException
    End Function

End Module

Answer : File Find/Replace(or Amend) string/update file

Follows a vb that replaces a specific string

Just need to instantiate these variables:

strTxtFile = "C:\Documents and Settings\" & strUser & "\Rest of the Path"
strMatch = "TEXT TO BE MODIFIED"
strNewText = "TEXT TO REPLACE ORIGINAL"
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
Dim strAllLines,strTxtFile,strMatch,objFSO,objFile,strNewText
Const ForReading = 1
Const ForWriting = 2

Set objNetwork = CreateObject("Wscript.Network")
strUser = objNetwork.UserName


strTxtFile = "C:\Documents and Settings\" & strUser & "\Rest of the Path"
strMatch = "TEXT TO BE MODIFIED"
strNewText = "TEXT TO REPLACE ORIGINAL"


'Reads the TXT file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strTxtFile, ForReading)
strAllLines = objFile.ReadAll
objFile.Close

strNewText = Replace(strAllLines,strMatch,strNewText)

Set objFile = objFSO.OpenTextFile(strTxtFile, ForWriting)
objFile.WriteLine(strNewText)
objFile.Close
Random Solutions  
 
programming4us programming4us