0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Powershell 近似包含演算子はないらしく配列を使う必要があるらしい Powershell Function Approximate inclusion operator Using Array Variables

Posted at

https://docs.microsoft.com/ja-jp/powershell/scripting/learn/deep-dives/everything-about-arrays?view=powershell-7
https://docs.microsoft.com/ja-jp/powershell/scripting/learn/deep-dives/everything-about-if?view=powershell-7
PowerShellでもっといい条件文の書き方5選
https://qiita.com/nimzo6689/items/6c6443a7494674e44949

このなかで

function Test01_Good($fruit) {
    # リンゴ、イチゴ、サクランボであれば赤と判定する。
    if ($fruit -in @('リンゴ', 'イチゴ', 'サクランボ')) {
        return '赤'
    }
}

if ($fruit -in @('リンゴ', 'イチゴ', 'サクランボ')) {
    # もし `$fruit` がリンゴ、イチゴ、サクランボの中に含まれていれば
}

if (@('リンゴ', 'イチゴ', 'サクランボ') -contains $fruit) {
    # もしリンゴ、イチゴ、サクランボが`$fruit`を含んでいれば
}

とこのように配列を使う方法が紹介されています。
ところが、リンゴやイチゴやサクランボに似たら赤っぽいと判定する
という使い方はできません。

function Test01_Goodlike($fruit) {
    # リンゴ、イチゴ、サクランボであれば赤と判定する。
    # 動かない
    if ($fruit -like @('リンゴ', 'イチゴ', 'サクランボ')) {
        return '赤っぽいかも'
    }
}

結から言うと
一つの値に対して複数の値をLikeで比較するためには配列を回す必要がある

# 近似包含演算子
$ar = @('*リン*', 'イチ*', '*サクラ*')
function Test01_like($fruit) {
    # リンゴ、イ7ゴ、サクランボに近ければ赤と判定する。
    $bl=$false
    for ($i=0;$i -lt $ar.Length;$i++){
    if ($fruit -like $ar[$i] ) {
     Write-Host 'hit' $i
     Write-Host $ar[$i]
       $bl = $true ; Break
    }
    }
   if($bl -eq $true){Return '赤っぽいかも...'}Else{Return 'よくわかりません'} 
}
Test01_like 'イントロクイズ' 
Test01_like 'リン酸' 
Test01_like 'サクラカゲチヨ' 

ところでCountにするとヒットする件

# 近似包含演算子
$ar = @('*リン*', 'イチ*', '*サクラ*')

function Test01_like($fruit) {
    # リンゴ、イ7ゴ、サクランボに近ければ赤と判定する。
    # この関数はウソです
    $bl=$false
    for ($i=0;$i -lt $ar.length;$i++){
    if ($fruit -like $ar[$i] ) {
     #Write-Host 'hit' $i
     #Write-Host $ar[$i]
       $bl = $true ; Break
    }
    }
   if($bl -eq $true){Return '赤っぽいかも... may be red'}Else{Return 'よくわかりません Sorry, unspeakable Something-like red or not, it is not clear. '} 
}
Test01_like 'イントロクイズ' 
Test01_like 'リン酸' 
Test01_like 'サクラカゲチヨ' 

型指定を[String]にしないように、必ず[String[]]配列型指定した方がいいみたいです

[String]にするとなぜか文字列が1文字1文字で配列になります。
このためワイルドカード1文字がHitして何でも近似します。
これをやらかすと[String]を消しても戻りません。
なのでArrayには必ずこれをつけましょう。[String[]]
一度間違えると間違えたままになるみたいです。

使うところ

これを使うのはOfficeの拡張子だけを取り出す場合です
VISIOを別にすると
@('.doc*', '.xls*', '*.ppt*',.md?,.accd*, '.pub')
となります。
get-childitem をカレントのフォルダで実行します。
foreachで拡張子をとって関数に入れます。

# 包含演算子 Officeのファイルか判定する関数(Visio等は除く)
[String[]]$ar = @('.doc*', '.xls*', '*.ppt*', '.md?', '.acc*', '.pub') #関数は必ずこの配列を使用します
function Test-OfficeFile {
    param(
        [Parameter(Mandatory)]
        [ValidateNotNull()]
        $strExtension
    ) 
    $bl=$false
    for ($i=0;$i -lt $ar.length;$i++){
    if ($strExtension -like $ar[$i] ) {
     #Write-Host 'hit' $i
     #Write-Host $ar[$i]
       $bl = $true ; Break
    }
    }
   if($bl -eq $true){Return $true }Else{Return $false} 
}
### End Function Test-OfficeFile
Get-ChildItem  | ForEach-Object{ Test-OfficeFile $_.Extension}

参考文献

【PowerShell】配列(Array)についてまとめてみる
https://soma-engineering.com/coding/powershell/what-is-array/2018/05/29/

0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?