Visual Basic 6 function "file_put_contents"

Go back

Below you'll find the source for the Visual Basic 6 function file_put_contents.

Attribute VB_Name = "modFilePutContents"
' This function is downloaded from:
' You may freely distribute this file but please leave all comments, including this one, in it.
' @Author Stefan Thoolen <>

Option Explicit

' Write a string to a file
' Almost the same syntax as the PHP function 'file_put_contents'
' See also:
' @param    String  filename        The file to write
' @param    String  data            The data to place in the file
' @param    String  boolean         When true, data will be appended
' @return   Boolean                 True on success, false on failure
' @author   Stefan Thoolen <>
Public Function file_put_contents(filename As String, data As String, Optional ByVal append As Boolean = False) As Boolean
    On Error GoTo file_put_contents_failure
    Dim ff As Integer: ff = FreeFile
    If append Then Open filename For Append As #ff
    If Not append Then Open filename For Output As #ff
        Print #ff, data;
    Close #ff
    file_put_contents = True
    Exit Function
    file_put_contents = False
End Function