Skip to content

Instantly share code, notes, and snippets.

@ryu2012
Created April 24, 2024 04:39
Show Gist options
  • Save ryu2012/32f60bf3357c5848e0a27250510c10eb to your computer and use it in GitHub Desktop.
Save ryu2012/32f60bf3357c5848e0a27250510c10eb to your computer and use it in GitHub Desktop.
送信元フォルダ内に含まれる複数のProxyフォルダを探してその配下をフォルダツリーを保ったまま送信先にコピーする
$sourceFolder = "D:\TEST\Source"
$destFolder = "D:\TEST\Dest"
# 送信元フォルダ内の全てのファイルを取得
$files = Get-ChildItem -Path $sourceFolder -Recurse
# Proxyフォルダに含まれるファイルのみを送信先フォルダにコピー
foreach ($file in $files) {
if ($file.FullName -like "*\Proxy\*") {
Write-Host "処理中: $($file.FullName)"
$relativePath = $file.FullName.Substring($sourceFolder.Length + 1)
$destFilePath = Join-Path -Path $destFolder -ChildPath $relativePath
$destDirectory = Split-Path -Path $destFilePath -Parent
if (!(Test-Path -Path $destDirectory)) {
New-Item -Path $destDirectory -ItemType Directory -Force | Out-Null
}
# 送信先ファイルと比較して同じファイルが存在する場合はスキップ
if (!(Test-Path -Path $destFilePath) -or (Get-FileHash -Path $file.FullName).Hash -ne (Get-FileHash -Path $destFilePath).Hash) {
Copy-Item -Path $file.FullName -Destination $destFilePath -Force
Write-Host "コピー完了: $($file.FullName) -> $($destFilePath)"
} else {
Write-Host "スキップ: 同じファイルが送信先フォルダに既に存在します。"
}
}
}
# スクリプトの実行が終了した際にウィンドウが閉じないようにする
Read-Host "処理が終了しました。何か入力してください。"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment