can any one please help on this scenario

If file exists it should replace the text and if file doesnt exists it should create the file.
0 Comments   [ - ] Hide Comments


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.
Answer this question or Comment on this question for clarity


Dim objFSO, objFile, strNewText

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(<File Path>, 1)

If objFSO.FileExists (<Path of File>) then
strNewText = Replace(objFile.ReadAll,"<Text to Replace>")

Set objFile = objFSO.OpenTextFile(<File Path>, 2)
objFile.Write strNewText


objFSO.CreateTextFile("<Path to the File>")

End If

Set objFSO = Nothing
Answered 12/13/2010 by: murali.bhat
Purple Belt

Please log in to comment
errrm.....I'm not entirely sure you need to use the 'FileExists' method. OpenTextFile has a parameter which creates the file if it doesn't exist....[;)]

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Dim fso, MyFile, FileName

Set fso = CreateObject("Scripting.FileSystemObject")

' Open the file for output.
FileName = "c:\temp\testfile.txt"

Set MyFile = fso.OpenTextFile(FileName, ForWriting, True)

' Write to the file.
MyFile.WriteLine "This is a test"

'Close file
Set MyFile = Nothing
Answered 12/14/2010 by: captain_planet
Black Belt

Please log in to comment