Replace text in text file using VBS

Paul Haan

I have been using Notepad++ to make adjustments to a TXT file. I was wondering if its possible to automate this using VBScript?

  • Open the file.
  • Replace \r\n with " " (a blank space).
  • Replace I0 with \nI0.
  • Replace X0 with \nI0.
  • Save the file.
omegastripes

The below example is suitable for Unicode and ASCII text files:

sPath = "C:\Users\DELL\Desktop\tmp\test.txt"
sContent = ReadTextFile(sPath, 0) ' lFormat -2 - System default, -1 - Unicode, 0 - ASCII
sContent = Replace(sContent, vbCrLf, " ")
sContent = Replace(sContent, "I0", vbLf & "I0")
sContent = Replace(sContent, "X0", vbLf & "I0")
WriteTextFile sContent, sPath, 0

Function ReadTextFile(sPath, lFormat)
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath, 1, False, lFormat)
        ReadTextFile = ""
        If Not .AtEndOfStream Then ReadTextFile = .ReadAll
        .Close
    End With
End Function

Sub WriteTextFile(sContent, sPath, lFormat)
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath, 2, True, lFormat)
        .Write sContent
        .Close
    End With
End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy and replace simple text in a txt file using vbs

From Dev

Using Grunt to Replace Text in a File

From Dev

Using Grunt to Replace Text in a File

From Dev

remove nul characters from text file using vbs

From Dev

Vbs Make a text file with text written in it

From Dev

Vbs Make a text file with text written in it

From Dev

How to replace a column in a text file using a list?

From Dev

Unable to replace text in a file using perl

From Dev

Find and replace words in a text file using java

From Dev

Using sed in terminal to replace text in file

From Dev

Find and replace text within a file using commands

From Dev

PHP Replace string in text file using

From Dev

Replace text inside a PDF file using iText

From Dev

Find and replace text within a file using commands

From Dev

Using sed in terminal to replace text in file

From Dev

Replace text in file with variable using sed

From Dev

Select Text in a File and replace it using C#

From Dev

Replace all text in text file using regular expression

From Dev

Replace text between 2 particular lines in a text file using sed

From Dev

Vbs How to edit a text file through inputbox

From Dev

Read Content from Text File VBS

From Dev

Why does VBS not read this text file correctly?

From Dev

How to get VBS to read from text file?

From Dev

Vbs How to edit a text file through inputbox

From Dev

write a text file with the results of a query in vbs

From Dev

saving visible text on web page using VBS

From Dev

replace text in middle of the file

From Dev

Replace text in file with SED

From Dev

Replace parameters in a text file