Copy and replace simple text in a txt file using vbs

Steve

When running the script below I see Error Line 2 Char 1 Invalid procedure call or argument please can someone advise:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt",ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Jim ", "James ")

Set objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt",ForWriting)
objFile.WriteLine strNewText
objFile.Close
Hackoo

Try like that :

Option Explicit
Const ForReading = 1
Const ForWriting = 2
Dim objFSO,objFile,strText,strNewText
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt",ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Jim", "James")

Set objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt",ForWriting)
objFile.WriteLine strNewText
objFile.Close
set objFSO = Nothing
set objFile = 

EDIT : In this case you should use VBScript regular expressions like this :

Option Explicit
Const ForReading = 1
Const ForWriting = 2
Dim objFSO,objFile,strText,strNewText,objRegEx
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt",ForReading)
strText = objFile.ReadAll
objFile.Close
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True   
objRegEx.IgnoreCase = True
objRegEx.Pattern = "jim"
strNewText = objRegEx.Replace(strText,"James")
Set objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt",ForWriting)
objFile.WriteLine strNewText
objFile.Close
set objFSO = Nothing
set objFile = Nothing

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replace text in text file using VBS

From Dev

copy "all contain of file.txt " and "replace word in other text file with it " in cmd using any script and any utility

From Dev

Copy and rename oldest file using vbs

From Dev

How to replace text in a txt file with python?

From Dev

How to replace particular text in the txt file?

From Dev

How to replace text and symbols in a txt file?

From Dev

delete a line and replace with new text in a txt file

From Dev

remove nul characters from text 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

'Send the first text line (row) C:\Alexander.txt to C:\Test.vbs to Line (row) n and replace

From Dev

Read line per line a txt file with VBS

From Dev

How to replace text of a specific line in a .txt file with Qt?

From Dev

Open txt file and replace text in the first two lines (C#)

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

copy certain files using batch script with whitelist txt file

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

Simple way to copy a file

Related Related

  1. 1

    Replace text in text file using VBS

  2. 2

    copy "all contain of file.txt " and "replace word in other text file with it " in cmd using any script and any utility

  3. 3

    Copy and rename oldest file using vbs

  4. 4

    How to replace text in a txt file with python?

  5. 5

    How to replace particular text in the txt file?

  6. 6

    How to replace text and symbols in a txt file?

  7. 7

    delete a line and replace with new text in a txt file

  8. 8

    remove nul characters from text file using vbs

  9. 9

    Using Grunt to Replace Text in a File

  10. 10

    Using Grunt to Replace Text in a File

  11. 11

    'Send the first text line (row) C:\Alexander.txt to C:\Test.vbs to Line (row) n and replace

  12. 12

    Read line per line a txt file with VBS

  13. 13

    How to replace text of a specific line in a .txt file with Qt?

  14. 14

    Open txt file and replace text in the first two lines (C#)

  15. 15

    Vbs Make a text file with text written in it

  16. 16

    Vbs Make a text file with text written in it

  17. 17

    copy certain files using batch script with whitelist txt file

  18. 18

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

  19. 19

    Unable to replace text in a file using perl

  20. 20

    Find and replace words in a text file using java

  21. 21

    Using sed in terminal to replace text in file

  22. 22

    Find and replace text within a file using commands

  23. 23

    PHP Replace string in text file using

  24. 24

    Replace text inside a PDF file using iText

  25. 25

    Find and replace text within a file using commands

  26. 26

    Using sed in terminal to replace text in file

  27. 27

    Replace text in file with variable using sed

  28. 28

    Select Text in a File and replace it using C#

  29. 29

    Simple way to copy a file

HotTag

Archive