the puzzle is, how to assign a match in regex search to a new variable: to test-path it and eventually create a folder, if the test fails.
Use the -match
with your regex and find the matches in the build-in $matches
hash table!
- $matches
- PowerShell stores matches from the
-match
operator in the$matches[]
hash table. - Example:
# 2.3.4 | TEST the path and create the new folder if the year changes
If ($filePrefix.Node.InnerText -match $regexFilePath) {
$filePath = $matches[0]
If (-Not (test-path $filePath)) {
New-Item -ItemType Directory -Path $filePath
}
}