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?

ディレクトリ維持したままコピーする

Posted at
# 退避先のルートディレクトリを指定
$destinationRoot = "C:\Backup"

# 退避したいファイルの絶対パスを配列で指定
$filesToBackup = @(
    "C:\SourceRoot\Folder1\File1.txt",
    "C:\SourceRoot\Folder2\Subfolder\File2.txt",
    "C:\SourceRoot\Folder3\File3.txt"
)

# 共通の接頭辞ディレクトリを指定(例:C:\SourceRoot\)
$commonPrefix = "C:\SourceRoot\"

# 各ファイルを処理
foreach ($file in $filesToBackup) {
    # 元のファイルパスから相対パスを取得
    $relativePath = $file.Substring($commonPrefix.Length)
    
    # 退避先のフルパスを作成
    $destinationPath = Join-Path $destinationRoot $relativePath
    
    # 退避先のディレクトリが存在しない場合は作成
    $destinationDir = Split-Path $destinationPath -Parent
    if (!(Test-Path $destinationDir)) {
        New-Item -ItemType Directory -Path $destinationDir -Force | Out-Null
    }
    
    # ファイルをコピー
    Copy-Item $file $destinationPath -Force
    
    Write-Host "Backed up: $file to $destinationPath"
}
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?