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?

ディレクトリ作成 あるなし判定あり スクリプト

Last updated at Posted at 2025-04-02

キッティング作業のエビデンスとしてログを出力させて、設定値があっているか確認することがある
その際、ログ出力させるフォルダないしファイルが必要となる
業務でPowershellを使用することがあるのでそちらを紹介
ほかにやり方はあるかもしれんが慣れているものとして


フォルダのパスを指定
$folderPath = "C:\Work\Log"

フォルダが存在するか確認
if (-Not (Test-Path -Path $folderPath)) {
フォルダが存在しない場合は作成
New-Item -ItemType Directory -Path $folderPath
Write-Host "フォルダ '$folderPath' を作成しました。"
} else {
フォルダが既に存在する場合のメッセージ
Write-Host "フォルダ '$folderPath' は既に存在しています。"
}

①の説明
指定するディレクトリにフォルダを作成させるためのもの
②の説明
if関数を使うことによってフォルダがあるかないかを判定している
なければ作成するし、なければありますよとメッセージとして出力する

実際にPowershellではどんな挙動になるかは下記の画像で

フォルダ作成と判定のスクリプトの挙動.jpg

0
0
0

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?