Joe R.
Joe R.

Reputation: 2052

Show progress bar when executing shell script in Windows

My users were manually backing up a huge file every day. I automated this process by creating a DOS Batch script, but the command line window does not display the slow progress for this process. Can a GUI version for this process be created which shows the same progress bar displayed when you manually copy and paste a large file?

Upvotes: 0

Views: 1189

Answers (1)

filimonic
filimonic

Reputation: 4644

If you copy (or anyhow else you process) files one-by-one, you can do this.

And if you copy files using wildcard mask, (copy c:\* d:\), you can't

Progressbars in powershell are done like this:

$local:fileList = @( Get-ChildItem -Recurse -Path 'd:\_MEDIA\VIDEO' | Where-Object {$_.psIsContainer -eq $false} )
$local:fileTotalSize = $( $local:fileList | Measure-Object -Property 'Length' -Sum).Sum
$local:currentlyProcessedBytes = 0;
$local:onePercentSize = $local:fileTotalSize / 100
$local:progressActivity = 'Making backup'

ForEach ($file in $local:fileList ) {
    Write-Progress -Activity $local:progressActivity -Status "Processing $($file.Name)" -PercentComplete $( [math]::Floor( $local:currentlyProcessedBytes / $local:onePercentSize) )
    Write-Host "Working with file [$($file.FullName)]"
    #work
        #Placeholder for do some job with file
        Start-Sleep -Milliseconds 300
    $local:currentlyProcessedBytes += $file.Length

}

Write-Progress -Activity $local:progressActivity -Completed

There is a guy, that made an chimera of robocopy ad powershell

If you have one-big-file, use RoboCopy - it will show percentage

robocopy D:\_Media\Video d:\ "The.Boy.In.The.Striped.Pajamas.barm.*"

Upvotes: 1

Related Questions