VBS function to add content to the specified file


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
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
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.