Insecticide
Insecticide

Reputation: 13

Adding a Write Progress bar to a script Powershell

I'm actually writing a powershell script that sort pictures and videos by dates. The script works fine but i would like to add a progress bar, and i don't know where to start, this might be a bit tricky for me, that's why i'm looking for help.

Here is the function that sort pictures

foreach ($file in $Images) 
{
    $Directory = $destinationDirectory + "Pictures\" + $file.LastWriteTime.Date.ToString('yyyy') + "\" + $file.LastWriteTime.Date.ToString('MMM') 

if (!(Test-Path $Directory))
{

    New-Item $directory -type directory
}

Copy-Item $file.fullname $Directory 
}

I read the documentation about the Write-progress function but i really don't know how i'm supposed to manage it in this script

Upvotes: 1

Views: 2312

Answers (2)

Booga Roo
Booga Roo

Reputation: 1781

The Write-Progress cmdlet needs a bit of math to make it work for your situation. This should do it:

$i = 1
foreach ($file in $Images)
{
    $Directory = $destinationDirectory + "Pictures\" + $file.LastWriteTime.Date.ToString('yyyy') + "\" + $file.LastWriteTime.Date.ToString('MMM') 

    if (!(Test-Path $Directory))
    {
    New-Item $directory -type directory
    }
Copy-Item $file.fullname $Directory
[int]$Percent = $i / $Images.count * 100
Write-Progress -Activity "Copying photos" -Status "$Percent% Complete:" -PercentComplete $Percent
$i++
}

First, start with a counter variable $i and set it to 1 to represent the first item. This is setup outside the loop so that it doesn't reset back to 1 every time the loop runs.

Then, inside the loop the $Percent variable is defined by dividing the value of $i by the number of items and multiplying it by 100. Then, Write-Progress is called with the necessary parameters to display the progress. Then the counter is incremented upward by one using $i++.

Side note: $Percent is set to be an integer by placing [int] in front of it, which forces it to display whole numbers. If you want to see fractional values, just remove the [int].

Upvotes: 1

Glenn
Glenn

Reputation: 1855

Use a variable to hold the count of files copied, increment it for each operation, then display the percentage. The example for Write-Progress has a pretty good example.

I'd recommend using the PowerShell pipeline as well instead of a foreach.

Something like this (remove the -WhatIfs when you're ready):

$images |
    ForEach-Object 
    -Begin { 
        $filesCopied = 0
    } `
    -Process {
        $Directory = "$destinationDirectory\Pictures\$($_.LastWriteTime.Date.ToString('yyyy'))\$($_.LastWriteTime.Date.ToString('MMM'))"

        if (!(Test-Path $Directory)) {
            New-Item $directory -type directory -WhatIf
        }

        Copy-Item $_.fullname $Directory -WhatIf

        $filesCopied += 1
        Write-Progress -Activity "Copied $_.FullName" `
            -CurrentOperation "Copying $_" `
            -Status "Progress:" `
            -PercentComplete (($filesCopied / $Images.Count) * 100)
    }

Upvotes: 2

Related Questions