3

I am trying to figure out how to use vbscript to:
1 - open a .csv file as a .txt file
2 - search for a certain string of text that is located randomly throughout the text 3 - replace that string with a different string.

I have found an article that helped me learn how to replace an entire line in a .txt document, but so far have had no luck finding anything about replacing just certain characters within the line.

Thanks!

Here is the code I am using currently:

Const ForReading = 1
Const ForWriting = 2

'Setting up our objects and focusing on the text file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\Documents\Script Practice\TextFiles-2-4-15-Folder\ReadandWrite\Textlook.txt", ForReading)


Do Until objFile.AtEndOfStream

    strLine = objFile.ReadLine


    If strLine = "Myer" Then
        strLine = "Mike"
    End If

    strContents = strContents & strLine & vbCrLf

Loop



objFile.Close


Set objFile = objFSO.OpenTextFile("C:\Users\Documents\Script Practice\TextFiles-2-4-15-Folder\ReadandWrite\Textlook.txt", ForWriting)


objFile.Write(strContents)
objFile.Close

The text file it references says:
Ken Myer
Fabrikam

Pilar Ackerman
Wingtip Toys

Jeff Hay
Fabrikam

Ellen Adams
Northwind Traders

Myer

(End of text file). So essentially, I have gotten the code to successfully change the "Myer" that is on its own line to "Mike". What I am having a hard time with is changing the "Myer" in the first line to "Mike". Hopefully this helps clarify things a bit...I'm extremely new at this so not sure of the language I should be using to describe the problem.

2
  • Please demonstrate what you have tried so far, and it's possible we might be able to help you figure out what you're doing wrong. Commented Feb 5, 2015 at 17:15
  • I added some edits to my question to hopefully help. Commented Feb 5, 2015 at 21:15

2 Answers 2

3

Use Replace on the file's content obtained by .ReadAll() and .Write the result back. In code:

Option Explicit

Dim goFS  : Set goFS  = Createobject("Scripting.FileSystemObject")
Dim goWAU : Set goWAU = WScript.Arguments.Unnamed

WScript.Quit main()

Function main()
  main = 1 ' assume error
  If 3 = goWAU.Count Then
     If goFS.FileExists(goWAU(0)) Then
        Dim s : s = goFS.OpenTextFile(goWAU(0)).ReadAll()
        If 0 < Instr(s, goWAU(1)) Then
           goFS.CreateTextFile(goWAU(0)).Write Replace(s, goWAU(1), goWAU(2))
           WScript.Echo "done"
           main = 0
        Else
           WScript.Echo goWAU(1), "not found"
        End If
     Else
        WScript.Echo goWAU(0), "does not exist"
     End If
  Else
     WScript.Echo "need 3 args: fspec, find, replacement"
  End If
End Function

output:

copy con 28350055.csv
1,2,3
4,5,6
^Z

cscript 28350055.vbs 28350055.csv 5 4711
done

type 28350055.csv
1,2,3
4,4711,6

cscript 28350055.vbs 28350055.csv 5 4711
5 not found

cscript 28350055.vbs 28350055.cs 5 4711
28350055.cs does not exist

Use that demo to determine what is needed to solve your real world problem.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm extremly new too, so i didnt get what the other answer do in the code, but i figured out in the last answer about "Replace" and use in your code for do what you need, and the result is this:

Const ForReading = 1
Const ForWriting = 2

'Setting up our objects and focusing on the text file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\Documents\Script Practice\TextFiles-2-4-15-Folder\ReadandWrite\Textlook.txt", ForReading)


Do Until objFile.AtEndOfStream

    strLine = objFile.ReadLine


    strLine = Replace(strLine,"Myer","Mike")

    ' If strLine = "Myer" Then
        ' strLine = "Mike"
    ' End If

    strContents = strContents & strLine & vbCrLf

Loop



objFile.Close


Set objFile = objFSO.OpenTextFile("F:\BIBLIOTECAS\Archivos\TEST.txt", ForWriting)


objFile.Write(strContents)
objFile.Close

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.