Hi

I'm searching a vbs script which searches a string in a defined textfile.
If the string is not found it should be added in the file.
Can somebody help me?

Thank you.
Answer Summary:
Cancel
2 Comments   [ + ] Show Comments

Comments

  • How can I replace the string in the same line if is found in the text file.

    Please help me i wand to do this as soon as possible.

    Thanks in advance.
  • This content is currently hidden from public view.
    Reason: Repetitive
    For more information, visit our FAQ's.
  • This content is currently hidden from public view.
    Reason: Repetitive
    For more information, visit our FAQ's.
  • This content is currently hidden from public view.
    Reason: Repetitive
    For more information, visit our FAQ's.
  • what about if i need the script to read a file and search a text and change it with another text.
Please log in to comment

Community Chosen Answer

1
This should work:


Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set objShell = Wscript.CreateObject("Wscript.Shell")
Const FORREADING = 1
Const FORWRITING = 2
Const FORAPPENDING = 8
Dim sToSearch: sToSearch = "What you're Looking for"
Dim sFileName: sFileName = "C:\Temp.txt"
Dim sContent, Found
If Not objFSO.FileExists(sFileName) Then
MsgBox "File Not Found"
WScript.Quit 0
End If
Set TxtFile = objFSO.OpenTextFile(sFileName,FORREADING)
sContent = TxtFile.ReadAll
If InStr(sContent,sToSearch) Then Found = True End If
Set TxtFile = Nothing
If Not Found Then
Set TxtFile = objFSO.OpenTextFile(sFileName,FORAPPENDING)
TxtFile.WriteLine sToSearch
End If
Answered 07/28/2009 by: pjgeutjens
Red Belt

Please log in to comment
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.

Answers

0
Thank you.
That works just as I need it.
Answered 07/29/2009 by: AcIvI
Senior Yellow Belt

Please log in to comment
0
You don't actually need to define the Shell object even. Just a "bad" habit of mine when coding VBS. Also at the end you might want to add another

Set TxtFile = Nothing
Answered 07/29/2009 by: pjgeutjens
Red Belt

Please log in to comment
0
You might want to close the file after you have done reading/writing/appending to it and before setting it to nothing.
TxtFile.Close in this case.
Answered 02/06/2015 by: TimoP
Orange Belt

Please log in to comment
0
You might also want to make the code case-insensitive if there is ANY doubt that the case may vary in the string you are searching for..
Answered 02/06/2015 by: EdT
Red Belt

Please log in to comment
0
This question of me is about 5.5 years old.
Still thank you for your answers.
Answered 02/09/2015 by: AcIvI
Senior Yellow Belt

Please log in to comment
0
it's a very helpfull script
but what about if i need the script to read a file and search a text and change it with another text.
Answered 02/10/2015 by: Sanfoor
Senior Yellow Belt

Please log in to comment
Answer this question or Comment on this question for clarity