VBS function to add content to the specified file

Time:2022-5-5

Copy codeThe code is as follows:


‘write a string to the specified file, and the third parameter specifies whether to delete the original content
Function Z_WriteFile(sFileName, sText, bAppend)
Dim fs, fso, iomode
if bAppend = True Then
iomode = 8 ‘ForAppending
else
iomode = 2 ‘ForWriting
end if
set fs = CreateObject(“Scripting.FileSystemObject”)
set fso = fs. Opentextfile (sfilename, iomode, true) ‘the third parameter indicates that the file does not exist, then a new file is created
fso.WriteLine sText
fso.Close
set fso = Nothing
set fs = Nothing
End Function

Dim path, sFileName, sText
path = “E:\Program\VBScript”
sFileName = path & “\1.txt”
sText = “what can I do for you”
Z_WriteFile sFileName, sText, True


Z_ The third parameter in WriteFile specifies whether to add content to the end of the file or clear the original content before inserting content.