or1ko's diary

日々を書きます

3秒間ごとにコピーするスクリプト

指定したフォルダ($src)から指定したフォルダ($dest)にコピーするスクリプト
ただし、ファイルやフォルダを一つコピーするたびに3秒待つ。
また、フォルダの更新日付はコピーした日付となる。

$src = "<コピー元のフォルダパス>"
$dest = "<コピー先のフォルダパス>"
$waitSeconds = 3 # 待ち時間(秒)

Get-ChildItem $src -Recurse |
ForEach-Object {
    $isDir = $_.GetType().ToString().Contains("DirectoryInfo")
    $file = $dest + $_.FullName.Substring($src.Length)
    Write-Host "check $file"
    $exists = Test-Path -LiteralPath $file
    if (-not $exists) {
        if ($isDir) {
            Write-Host "make directory -> $file"
            New-Item -Path $file -ItemType Directory
        } else {
            Write-Host "copy file -> $file"
            Copy-Item -Path $_.FullName -Destination $file
        }
        Write-Host "wait $waitSeconds seconds"
        Start-Sleep -Seconds $waitSeconds
    }
}