/build/static/layout/Breadcrumb_cap_w.png

file exists then replace if doesnt create

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   [ + ] Show comments

Answers (2)

Posted by: murali.bhat 13 years ago
Purple Belt
0
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>")
objFile.Close

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

Else

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

End If

Set objFSO = Nothing
Posted by: captain_planet 13 years ago
Black Belt
0
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
MyFile.Close
Set MyFile = Nothing
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ