It is a pitty your function doesn't really return anything and only writes to the console the file(s) found.
Why not turn this into a function that actually returns a FileInfo object you can get all kinds of properties from and if needed use its methods?
Also, try using the Powershell Verb-Noun
naming convention for functions.
The below code doesn't cast the the basename of each file into a [version] type, but uses regex to both ensure the file's BaseName is in the format you describe, ignoring other files, but also it captures the first and last numbers to compare against:
function Find-File {
param (
[string]$Path,
[int]$M,
[int]$j
)
Get-ChildItem -Path $Path -File |
Where-Object {$_.BaseName -match '^(d+).d+.d+.(d+)$'} |
Where-Object {[int]$matches[1] -eq $M -and [int]$matches[2] -eq $j}
}
# find and (if found) return the file as FileInfo object
$theFile = Find-File "D:Updates3" 20 1
Regex details
^ Assert position at the beginning of the string
( Match the regular expression below and capture its match into backreference number 1
d Match a single digit 0..9
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
. Match the character “.” lterally
d Match a single digit 0..9
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
. Match the character “.” literally
d Match a single digit 0..9
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
. Match the character “.” literally
( Match the regular expression below and capture its match into backreference number 2
d Match a single digit 0..9
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
$ Assert position at the end of the string (or before the line break at the end of the string, if any)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…