Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

powershell - Function search from the files 20.0.0.1 to 20.0.0.21

I have 21 files from 20.0.0.0001 to 20.0.0.0021 in my directory D:Updates3 . I have a function "search" with 3 parameters: the directory, le first number of my file, the last number of my file My problem is when I enter the 3 parameters in my function search: the directory: D:Updates3 the first number: 20 the las number: 1 I have 3 results 20.0.0.0001 20.0.0.0011 20.0.0.0021

whereas, I want only the result 20.0.0.0001 What is the solution please?

here is my code:

$UpdatePath = "D:Updates3"

function search {
    param (
        [string]$UpdatePath,
        [int]$M,
        [int]$j
    )
    $UpdatePath = "D:Updates3"
    $Updates = (Get-ChildItem -Path $UpdatePath).BaseName
    
foreach ($item in $Updates) {
   
    if ($item -like "$M.0.0.???$j" ) {
        write-host $item
    }
}

}
search $Updates 20 1
question from:https://stackoverflow.com/questions/65947607/function-search-from-the-files-20-0-0-1-to-20-0-0-21

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...