Substitutes every occurrence of a specified set of characters in a (text) file with another set of characters you specify, and writes the revised file. The Substitute function is case sensitive.
TrFile_Substitute ( switches ; sourceFile ; destinationFile ; search string ; replace string )
|switches||these determine how the function works. At the moment you can specify how the function handles case differences|
|sourceFile||the path to the file which is the source for the substitution|
|destinationFile||the path where the resulting file must be written to|
|search string||any text expression or field containing text|
|replace string||any text expression or field containing text|
switches can be one of the following:
|-CaseSensitive||substitute strings only if the case of the source string is the same|
|-IgnoreCase||substitute strings even if the case differs|
Switches can also be one of this:
This determines the character encoding of the text to be read.
Data type returned
The returned result is an error code. An error always starts with two dollars, followed by the error code. You should always check for errors. Returned error codes can be:
|0||no error||The destination file with the substitutions was created|
|$$-43||fnfErr||Source file not found, or destination directory does not exist|
|$$-48||dupFNErr||Destination file already exists|
|$$-1||genericErr||The substitution could not be made|
Other errors may be returned.
Troi File Plug-in 2.0
FileMaker Pro 16 to 19
If the destination parameter is empty the substitution result will be saved in the source file itself. This happens also when you specify the same source file and destination file (this possibility was added in version 2.5). Note that the plug-in uses a temporary file to implement this feature, which can require the same amount of disk space as the source file. The plug-in can fail if there is not enough space on the disk.
NOTE The length of the search and replace string can be up to 1 GB.
Up to v5.0 there was an error in the documentation: it was incorrectly stated that UTF-8 was the default encoding. The default is -Encoding=Native, which is the current encoding as set in the system preference, for example on western Windows systems it usually is ISO_8859_1 or ASCII_Windows.
Set Field [ result ; TrFile_Substitute ( "-IgnoreCase" ; "C:\Data\temp.tab" ; "C:\Main.tab" ; "country" ; "world" ) ]
This will create the file “C:\Main.TAB” where all occurences of “country” are replaced with the string “world”. The case is ignored, so that also a string “Country” will be substituted.
We assume that in your FileMaker file the following fields are defined:
gErrorCode Global, number gSourceFilePath Global, text gDestFilePath Global, text gSearchStr Global, text gReplaceStr Global, text
gSourceFilePath should contain the path to an existing file, for example “D:\TempLogs\Log01.TXT” (Windows) or “Mac HD:TempLogs:Log 1” (Mac).
gDestFilePath should contain the path to the destination and should not exist, for example “D:\Output\L2000_01.TXT” (Windows) or “Mac HD:Output:Log 2000_01” (Mac). In a script add the following scriptstep:
Set Field [ gErrorCode ; TrFile_Substitute ( "-CaseSensitive" ; gSourceFilePath ; gDestFilePath ; gSearchStr ; gReplaceStr ) ]
This will create a new file gDestFilePath where all occurences of gSearchStr are replaced with the contents of gReplaceStr.
Used in example file
Related script step
|Substitute In File|