LoginSignup
0
0

More than 3 years have passed since last update.

PowerShellで空のフォルダに.gitkeepを作成する

Last updated at Posted at 2018-01-25

Gitで空のフォルダを管理するために、PowerShellで.gitkeepを作成するスクリプトを作った。

New-GitKeep.ps1
# 空のフォルダに.gitkeepファイルを作成する 

Param(
  [string]$path="." # .gitkeepファイルを作成するパス
)

if( [String]::IsNullOrEmpty($path) ){
  # 空のときはカレントにする
  $path = "."
}

if(Test-Path $path){
  # 指定のパス配下の一覧を取得する。
  $collection=Get-ChildItem $path -Recurse 
  foreach ($item in $collection) {
    # $itemがディレクトリでファイル数とディレクトリ数が0個の場合に .gitkeepファイルを作成する。
    if ($item.PSIsContainer -and !$item.GetFiles().Count -and !$item.GetDirectories().Count){
      $gitkeep=Join-Path $item.FullName .gitkeep
      New-Item $gitkeep -Force -type file
    }
  }
  exit 0
}
else {
  Write-Host $path is not found! 
  exit 1
}

実行例

example
PS C:\Work> ls -r -n
PS
TEST
PS\New-GitKeep.ps1
TEST\Folder1
TEST\Folder2
TEST\Folder1\Folder1-sub
PS C:\Work> .\PS\New-GitKeep.ps1 .\TEST


    ディレクトリ: C:\Work\TEST\Folder2


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        2018/01/25     13:13          0 .gitkeep


    ディレクトリ: C:\Work\TEST\Folder1\Folder1-sub


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        2018/01/25     13:13          0 .gitkeep


PS C:\Work> ls -r -n
PS
TEST
PS\New-GitKeep.ps1
TEST\Folder1
TEST\Folder2
TEST\Folder1\Folder1-sub
TEST\Folder1\Folder1-sub\.gitkeep
TEST\Folder2\.gitkeep

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