1

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

2
  • Hi there. You need to post some code for your progress bar - make an attempt at it before posting here and saying "I don't know how to start". That's fine conversationally with colleagues but here at SO people expect more up front. Otherwise people will downvote you. Commented Dec 21, 2019 at 17:24
  • Hi! Yes, of course, I agree. I was in a hurry Commented Dec 23, 2019 at 10:21

2 Answers 2

2

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)
    }
Sign up to request clarification or add additional context in comments.

1 Comment

this not work in ForEach-Object with -Parallel argument
1

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].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.