1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【PowerShell】ディレクトリ構成のみコピーする

Last updated at Posted at 2021-06-09

↓のDOSコマンドと同じことをPowerShellでやりたい場合の覚え書き

xcopy /t /e "コピー元フォルダ" "コピー先フォルダ"

ソース

# コピー元
$source_dir = "D:\HOGE\HOGE"
$source_dir_name = Split-Path $source_dir -Leaf
# コピー先
$target_dir = "D:\FUGA"
# コピー先の存在有無
$target_dir_exsist = Test-Path $target_dir
# 全てのファイルを除外してコピー
Copy-Item $source_dir $target_dir -Recurse -Exclude *.*

# コピー先フォルダが先に存在した場合、コピー先フォルダの中にコピー元フォルダを含んだ階層が作られてしまう
# コピー先が存在しなかった場合と動作を合わせるためにコピー後に一つ上の階層に移動させる
if( $target_dir_exsist )
{
    $source_dir = Join-Path $target_dir $source_dir_name
    Get-ChildItem -Path $source_dir | Copy-Item -Destination $target_dir -Recurse -Container
    Remove-Item $source_dir -Recurse
}
1
4
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
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?