<< Click to Display Table of Contents >> Navigation: 3. Script Language > Filesystem-Commands > FIL. - File-Commands > FIL. - File-Commands |
MiniRobotLanguage (MRL)
FIL.Replace in File
Replaces specified string in a file, with options for start position and replace behavior
Intention
The FIL.Replace in File command is used to replace occurrences of a specific string in a file with another string.
The command provides options to control the start position of the replace operation and the behavior of the replace operation through a set of flags.
This example below replaces the first occurrence of "oldString" with "newString" in the file "C:\myFile.txt",
starting from the 10th character of the file.
The search for "oldString" is case sensitive and "oldString" is only replaced if it's a whole word.
FIL.Replace in File|C:\myFile.txt|oldString|newString|10|7
The FIL.Replace in File command is designed with safety in mind. It first creates a temporary copy of the original file in the same directory,
performs the replace operations on this copy, and only if all operations are successful, it renames the original file and replaces it with the modified copy.
This approach minimizes the risk of data loss or file corruption due to interruptions during the replace operations.
Syntax
FIL.Replace in File|P1|P2|P3[|P4][|P5][|P6]
FIL.Rif|P1|P2|P3[|P4][|P5][|P6]
Parameter Explanation
•P1: This is the filename or path of the file you want to modify. It should be a string that points to a valid file on your system.
•P2: This is the string you want to replace in the file. It should be a string that exists in your file.
•P3: This is the string you want to replace P2 with. It can be any string.
•P4: This is the position in the file where you want to start the replace operation. If it's positive, it represents the position from the start of the file. If it's negative, it represents the position from the end of the file. If it's omitted or zero, the replace operation starts from the beginning of the file.
•P5: This is a set of flags that control the behavior of the replace operation. It's a bitwise OR of the following options:
•1 - If this bit is set, the search for P2 is case sensitive.
•2 - If this bit is set, P2 is only replaced if it's a whole word (i.e., surrounded by spaces or at the start/end of the file).
•4 - If this bit is set, only the first occurrence of P2 is replaced. If it's not set, all occurrences of P2 are replaced.
•P6: This is a variable that will contain the result of the operation as follows:
- 0 if the operation was successful,
- 1 if there was an error opening the file for reading, and
- 2 if there was an error opening the file for writing.
Example
'***********************************
'
'***********************************
Remarks
-
Limitations:
The FIL.Replace in File command reads the entire file into memory to perform the replace operations fast as possible,
which means the maximum file size it can handle is limited by the available memory.
Please note that extremely large files may exceed this limit.
Additionally, this command does not support DirectNTFS paths with more than 32767 characters.
See also:
• IEF. / NEF. - If - exist - File