Substitute In File |
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.
Syntax
Substitute In File [ Select ; Result (error); Source filePath ;
Destination filePath ; Search string ; Replace string ;
Ignore case ; Encoding ]
Options
Select entire contents | replaces the entire contents of a field with the result. If not checked the result replaces the currently selected portion of the field |
Result (error) | an error code |
Source filePath | the path to the file which is the source for the substitution |
Destination filePath | (optional) 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 |
Ignore case | substitute even if the case differs |
Encoding | the desired character encoding like UTF-8 |
Returned Result
Data type returned
Error code
Result
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.
Originated in
Troi File Plug-in 9.0
Compatibility
FileMaker Pro 17 to FileMaker Pro 21 (2024)
Considerations
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. 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.
Example
Substitute In File [ Select ; $ErrorCode ; "C:\Data\temp.tab" ; "C:\Main.tab" ;
"country" ; "world" ; Ignore case ]
This will create the file “C:\Main.TAB” where all occurrences of “country” are replaced with the string “world”. The case is ignored, so that also a string “Country” will be substituted.
Example 2
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:
Substitute In File [ Select ; $ErrorCode ; gSourceFilePath ; gDestFilePath ;
gSearchStr ; gReplaceStr ) ]
This will create a new file gDestFilePath where all occurrences of gSearchStr are replaced with the contents of gReplaceStr.
Related script step
Insert in File |
Related function
TrFile_Substitute |
Related topics
Troi File Plug-in online help (overview)