Ansgar Wiechers's helpful answer provides an effective solution.
To focus on the more general question of how to strip (remove) part of a file name (string):
Use PowerShell's -replace
operator, whose syntax is:
<stringOrStrings> -replace <regex>, <replacement>
:
<regex>
is a regex (regular expression) that matches the part to replace,
<replacement>
is replacement operand (the string to replace what the regex matched).
- In order to effectively remove what the regex matched, specify
''
(the empty string) or simply omit the operand altogether - in either case, the matched part is effectively removed from the input string.
For more information about -replace
, see this answer.
Applied to your case:
$db = 'DATABASE_PARTIAL' # sample input value
PS> $db -replace '_PARTIAL$', '' # removes suffix '_PARTIAL' from the end (^)
DATABASE
PS> $db -replace '_PARTIAL$' # ditto, with '' implied as the replacement string.
DATABASE
Note:
-replace
is case-insensitive by default, as are all PowerShell operators. To explicitly perform case-sensitive matching, use the -creplace
variant.
By contrast, the [string]
type's .Replace()
method (e.g., $db.Replace('_PARTIAL', '')
:
- matches by string literals only, and therefore offers less flexibility; in this case, you couldn't stipulate that
_PARTIAL
should only be matched at the end of the string, for instance.
- is invariably case-sensitive in the .NET Framework (though .NET Core offers a case-insensitive overload).
Building on Ansgar's answer, your script can therefore be streamlined as follows:
Param($xmlfile)
$db = ((Get-ChildItem C:\Files -Filter $xmlfile).BaseName -replace '_PARTIAL$').ToUpper()
Note that in PSv3+ this works even if $xmlfile
should match multiple files, due to member-access enumeration and the ability of -replace
to accept an array of strings as input, the desired substring removal would be performed on the base names of all files, as would the subsequent uppercasing - $db
would then receive an array of stripped base names.