亀の甲羅2

今日もまた朝とく起きて励まなん窓に明るきありあけの月

SQL分のINみたいなことはできないのか?(-in)

SQL分のINみたいなことはできないのか?

Get-ChildItemでファイル列挙するときに、拡張子txt、csvを除いて列挙したかった。

目次

本文

1. 解1

> Get-ChildItem -Recurse | %{if($_.Name -notlike "*.txt" -and $_.Name -Notlike "*.csv"){$_.Name}}
ccc.xlsm
ddd.xlsx

かなりダサい。古い。

2. 解2

> ls

    ディレクトリ: C:\temp\test

Mode                 LastWriteTime         Length Name                                                                                                                                                                        
----                 -------------         ------ ----                                                                                                                                                                        
-a----        2021/07/10     20:53              0 aaa.txt                                                                                                                                                                     
-a----        2021/07/10     20:53              0 bbb.csv                                                                                                                                                                     
-a----        2021/07/10     20:54              0 ccc.xlsm                                                                                                                                                                    
-a----        2021/07/10     20:54              0 ddd.xlsx                                                                                                                                                                    

> Get-ChildItem -Recurse | %{if($_.Extension -notin @(".txt", ".csv")){$_.Name}}
ccc.xlsm
ddd.xlsx

$_.Extension は当該ファイルの拡張子(ドット付き)を出力していくれるので、-notinで除外した上で表示している。  


 
ifに関するリファレンスは下記参照。

docs.microsoft.com  
 


バージョン情報

> $PSVersionTable

Name                           Value                                                                                                          
----                           -----                                                                                                          
PSVersion                      5.1.19041.1023