問題描述
Powershell 檢查文件類型 (Powershell check file type)
我正在編寫一個修改文件夾中圖像的腳本,在大多數情況下,我將 Get‑ChildItem
與目錄一起使用。
所以我我目前正在做一個名為
$fileExtension = [IO.Path]::GetExtension($files[$counter].Name)
的變量,其 if 語句類似於:
if($fileExtension ‑eq '.png' or
$fileExtensiono ‑eq '.jpg'. . . )
有更好的方法嗎?如果文件是圖像,我真的希望它執行這個修改圖像的功能,一個文件夾可以包含其他項目,例如一個 zip,如果它拾取它會導致腳本崩潰。
參考解法
方法 1:
try Something like this:
get‑childitem 'c:\temp\*' ‑file ‑include '*.png', '*.jpg'
(by l3l00、Esperento57)