LoginSignup
3
4

More than 5 years have passed since last update.

ファイルコピーとプログレス

Last updated at Posted at 2014-04-04

練習がてら書いてみた。

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
}
3
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
3
4