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:
' http://www.stefanthoolen.nl/archive/vb6-functions/
' 
' You may freely distribute this file but please leave all comments, including this one, in it.
' 
' @Author Stefan Thoolen <mail@stefanthoolen.nl>

Option Explicit

''
' Write a string to a file
' Almost the same syntax as the PHP function 'file_put_contents'
' See also: http://www.php.net/manual/en/function.file-put-contents.php
' @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 <mail@stefanthoolen.nl>
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_failure:
    file_put_contents = False
End Function