ファイル・フォルダの存在確認 Test-Path

ファイルが存在するか?

 Test-Path .\1.txt -PathType Leaf

フォルダが存在するか?

 if (!(Test-Path .\dir1 -PathType Container)) {
   echo "Not"
 }

パスが存在するか?

 $dir = Join-Path $env:USERPROFILE "Dropbox"
 $file = 1.txt
 $path = Join-Path $dir $file
 
 if (Test-Path $path) {
   echo "OK"
 }

いずれか1つはファイルが存在するか?

 $files = @("1.txt", "2.txt", "3.txt")
 
 if (($files | % { Test-Path $_}) -contains $true) {
     echo "Ok"
 } else {
     echo "Not"
 }

参考

Test-Path
https://technet.microsoft.com/en-us/library/hh849776.aspx
Using Test-Path to Verify the Existence of an Object
https://technet.microsoft.com/en-us/library/ff730955.aspx

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS

Last-modified: 2016-08-10 (水) 08:49:16