Skip to main content

TrFile_CreateFileWithContents (function reference)

By June 1, 2024June 4th, 2024Online Help, Troi File Plug-in
Troi File Plug-in > Functions reference > TrFile_CreateFileWithContents
Troi File Plug-in for FileMaker Pro

TrFile_CreateFileWithContents

Creates a new (data) file and fills it with (text) contents in one step.

SyntaxFunction badge

TrFile_CreateFileWithContents ( switches ; filePath ; contents)

Parameters

switches modifies the action of the function
filePath the path to the file to create
contents the (text) contents

Switches

Switches can be empty or:

-CreateFolders also create new folders if they don’t exist in the specified path

Switches can also be one of this:

-Encoding=UTF8 (default)
-Encoding=Native
-Encoding=ASCII_DOS
-Encoding=ASCII_Windows
-Encoding=ASCII_Mac
-Encoding=ISO_8859_1 (Windows Latin-1)
-Encoding=BytesOnly only characters in the range 0-255 are written, others are skipped

This determines the character encoding of the text to be written.
You can also add this switch:

-CRtoCRLF use Windows line endings. Returns in the text parameter are substituted with CRLF (Carriage Return followed by a Line Feed) in the written file

Returned Result

Data type returned

Error code

Result

The returned result is an error code. An error always starts with 2 dollars, followed by the error code. You should always check for errors. Returned error codes can be:

0 no error The file was created
$$-48 dupFNErr Destination file already exists
$$-1 genericErr The file could not be created

Other errors may be returned.

Originated in

Troi File Plug-in 16.0

Compatibility

FileMaker Pro 17 to FileMaker Pro 21 (2024)

Considerations

This will create the file and directly set the contents of the file in one step.

You can use FileMaker styled paths, like “filewin:/C:/MyFiles/test.txt”.
You can also omit filemac: or filewin: too! Any path starting with a slash will be parsed as a FileMaker style path (of the platform FileMaker is running).

See also the function TrFile_SaveFileDialog to select a file path for the file.

Example

Set Field [ gErrorCode ; TrFile_CreateFileWithContents ( "-Unused" ; "C:\Testtext.txt"; "My contents!" ) ]

This will create the file “Testtext.txt” on the C disk, with as contents the text “The contents!”.

Set Variable [ $ErrorCode ; TrFile_CreateFileWithContents ( "-CreateFolders" ; 
                                             "/macHD/users/ad/nw/foldB/myfile.txt" ; "Hello FileMakers!") ]

This will create the file “myfile.txt”, with “Hello FileMakers!” as contents. This will also create new folders if they don’t exist in the specified path, for example the folders “nw” and “foldB” may be created.

Example 2

# First find the path to the desktop folder and put it in a variable:
Set Variable [ $DesktopPath ;  TrFile_FindFolder( "-Desktop" ) ]

# Now make a (semi) unique filepath:
Set Variable [ $TheTimeTxt ;  Hour(Get(CurrentTime)) & "-" & Minute(Get(CurrentTime)) & "-" 
                       &  (Seconds( Get(CurrentTime) ) ]
Set Variable [ $FilePath ;  $DesktopPath & "Demo_WithContents" & $TheTimeTxt & ".txt") ]
Set Variable [ $TheContents ; $TheTimeTxt & " Success: we created + filled the file with Troi File." ]

# Now create and fill the file in one go:
Set Variable [ $ErrorCode ; TrFile_CreateFileWithContents ( "-Unused" ; $FilePath ; $TheContents ) ]

This will create the filled file on the Desktop.

Used in example file

CreateFile.fmp12

Related functionsFunction badge

TrFile_AppendToFile
TrFile_CreateFile
TrFile_DeleteFile
TrFile_ExportToFile

Related script stepScript step badge

Create File with Contents

Related topics

Troi File Plug-in online help (overview)


Online Help Page for Troi File Plug-in for 17 to 2024 –> TrFile_CreateFileWithContents (filep4331) 2024-0604 19:25:51