練習がてら書いてみた。
PowerShellだけで書きたかったけど。
CopyItemWithProgress.ps1
function Copy-ItemWithProgress ([string]$source, [string]$destination, [string]$title) {
$count = (Get-ChildItem $source -Recurse).Count
$i=0
Get-ChildItem $source -Recurse | % {
$p = $_.FullName.replace($source, "")
$to = $destination+$p
if ([IO.File]::Exists($_.FullName)) {
Copy-Item $_.FullName $to
} else {
if (-not ([IO.Directory]::Exists($to))) {
[void][IO.Directory]::CreateDirectory($to)
}
}
$i++
$status = "コピー {0} / {1}: {2}" -f $i,$Count,$_.Name
Write-Progress -Activity $title -Status $status -PercentComplete ($i / $count*100)
}
}
Copy-Itemの-PassThruを見つけたのでパパッと書いてみた
未テスト
Write-Progressの-PercentCompleteのとこでで怒られる
$countの値がおかしい?
Function Copy-ItemWithProgress([string] $Source, [string] $Destination, [string] $Title)
{
$count = (Get-ChildItem $Source -Recurse).Length
$i=0
Copy-Item $Source $Destination -PassThru -Recurse -Force | % {
$i++
$status = "コピー {0} / {1}: {2}" -f $i,$count,$_.Name
Write-Progress -Activity $title -Status $status -PercentComplete ($i / $count*100)
} | Out-Null
$i=0
}