onlyf onlyf - 1 year ago 137 Question

VB - Find Replace on File

i have the following file :


I would like to do the "Sed" equivalent from VB, so i dont have to call a cygwin sed to do this job, the end result should be :


The variable will be passed as a parameter into the script. Really inexperienced with anything VB, so any help would be greatly appreciated. The only thing close i ve found is this :

Dim TestString As String = "Shopping List"
' Returns "Shipping List".
Dim aString As String = Replace(TestString, "o", "i")

but how do i iterate over each line to perform that? Isnt there a way to do this for every line of a file? Thanks in advance.

Answer Source

Try the below code. Here I have created a function which replaces "ddmmyy" with current date (in the same format)

Function FormatFileName(sFileName As String) As String
    Dim sReturnValue As String

    sReturnValue = Replace(sFileName, "ddmmyy", Format(Now(), "ddmmyy"), , ,   vbTextCompare)

    FormatFileName = sReturnValue
End Function

You may test the code by using below test procedure

Sub Process()
    Dim sOutputFileName As String

    sOutputFileName = FormatFileName("/home/InputDirectory/DHddmmyy.txt")
    Debug.Print sOutputFileName

    sOutputFileName = FormatFileName("/home/InputDirectory2/DHddmmyy.txt")
    Debug.Print sOutputFileName

    sOutputFileName = FormatFileName("/home/InputDirectory3/DHddmmyy.txt")
    Debug.Print sOutputFileName

End Sub

Hope it helps!!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download