I'm trying to rename my files "Introduction _ C# _ Tutorial 1"
to something like "01.Introduction"
. It needs a -replace
operator as well as a -f
operator to zero pad the index number. My code is like:
$string = "Introduction _ C# _ Tutorial 1"
if ($string -match "^([^_]+)_[^\d]+(\d{1,2})$") {
$Matches[0] -replace "^([^_]+) _[^\d]+(\d{1,2})$", ("{0:d2}. {1}" -f '$2', '$1')
}
The output, however, is like the -f
operator being absent:
1. Introduction
How can I get the expected result?
By the way, is there a simple way to get the $matches
result without a -match
statement before it or combine the -match
statement to a one-liner code?