Replaces a pattern string with a new pattern in one or more files.
FILESTRREP("filespec", "patstr", "repstr", recurse, caseflag, maxmatches, wildcards)
"filespec" |
- |
A string. A list of files to process, can include wildcard characters such as * and ?. |
||||||
"srcstr" |
- |
A string. The source string. |
||||||
"patstr" |
- |
A string. The pattern to search for. |
||||||
"repstr" |
- |
A string. The replacement pattern. |
||||||
recurse |
- |
Optional. An integer, recursively process files in sub-directories.
|
||||||
caseflag |
- |
Optional. An integer, the case sensitivity flag.
|
||||||
maxmatches |
- |
Optional. An integer, the maximum number of matches to process. Defaults to -1, process all matches. |
||||||
wildcards |
- |
Optional. An integer, process * (any sequence) and ? (single character) as wildcard characters or use regular expressions (regex) in "patstr".
|
An integer, the number of files in which the replacement occurred.
filestrrep("\temp\*.txt", "January", "April")
Returns the count of the number of files in the "\temp" folder with extension ".txt" where the text "January" was replaced with the text "April".
The pattern and replacement strings are case sensitive.
If the pattern is not found in a file, the file remains unchanged.
When wildcard is set to 1, use * in "patstr" to match any character sequence and ? to match a single character. To search for a literal * or ? character instead of treating them as wildcards, escape them by prefixing a backslash: \* or \?.
When wildcard is set to 2, the search pattern "patstr" is interpreted a regular expression. Regular expressions (regex) provide far more flexibility than simple wildcards. A regular expression can indicate character classes (match a character from a set), quantifiers (how many times a pattern repeats), anchors (position in the string) and logical operators. For example, the pattern:
[0-9]{4}
matches exactly 4 digits.
Consider the file MyFile.txt that contains the following two lines:
a12,123,1234,1a2,987
a12,123,1234,1a2,1987
The command:
filestrrep("D:\temp\MyFile.txt", "[0-9]{4}", "FOUR", 0, 1, -1, 2)
converts the text to:
a12,123,FOUR,1a2,987
a12,123,FOUR,1a2,FOUR
Below is a summary of regular expression patterns when wildcards is 2:
Category |
Character |
Description / Matches |
|---|---|---|
Character Matches |
|
Any single character except a newline (\n). |
Character Matches |
|
Any digit character (equivalent
to |
Character Matches |
|
Any alphanumeric "word" character (letters, numbers, underscore). |
Character Matches |
|
Any whitespace character (spaces, tabs, line breaks). |
Character Classes |
|
Any single character inside the brackets (a, b, or c). |
Character Classes |
|
Any single character NOT inside the brackets. |
Quantifiers |
|
0 or more repetitions of the preceding token. |
Quantifiers |
|
1 or more repetitions of the preceding token. |
Quantifiers |
|
0 or 1 repetition (makes the preceding token optional). |
Quantifiers |
|
Between |
Anchors |
|
The start of the string or line. |
Anchors |
|
The end of the string or line. |
Anchors |
|
A word boundary (the edge position between a \w and a non-\w character). |
Logic / Groups |
|
Alternation operator (acts like a logical OR statement). |
Logic / Groups |
|
Capturing group. Isolates tokens and remembers the match context. |
Digits only: ^[0-9]+$
Uppercase letters: ^[A-Z]+$
Alphanumeric: ^[A-Za-z0-9]+$
Floating‑point number: ^[+-]?\d+(\.\d+)?$
Email (simple): ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$
When wildcard is 2, the replacement string "repstr", supports backreferences and substitution tokens.
Token |
Meaning |
Example call |
Result |
|---|---|---|---|
$0 |
Whole match |
strrep("hello","(\w+)","[$0]",1,-1,2) |
"[hello]" |
$1 .. $9 |
Numbered capture group |
strrep("John Smith","(\w+) (\w+)","$2 $1",1,-1,2) |
"Smith John" |
$10, $11 … |
Multi-digit group number |
strrep("abcdefghijk","(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)","$11",1,-1,2) |
"k" |
${name} |
Named capture group |
strrep("hello world","(?<a>\w+) (?<b>\w+)","${b} ${a}",1,-1,2) |
"world hello" |
\$ |
Literal dollar sign |
strrep("100","[0-9]+","\$$0",1,-1,2) |
"$100" |
\\ |
Literal backslash |
strrep("hello","\w+","\\",1,-1,2) |
"\" |
See STRREPLACE to replace a pattern in a string.