Thursday, October 30, 2014

#30 : Using Progressbar with a purpose

Progressbar is simply achieved in Powershell with Write-progress cmdlet. This cmdlet can be used with below example -

Write-Progress -Activity "File-Copy" -PercentComplete 55 -Status "Something" -Id 1

Running the above command in Powershell prompt is not meaningful, it will not display anything. I saw so many articles about Write-Progess, but all of them are using a dummy example to use Progressbar, so I am presenting an example which will show you a real utilization of Write-progress.
Example of using Write-Progress

The example demonstrates the utilization of Write-Progress. In this example, we will copy files from a source location to a destination and we will display progessbar for this. The status of progressbar will show the status of task. It does not show the file-copy status of individual file, but it shows the percent status of file-copy task as a whole.
I will use SOURCE_PATH and TARGET_PATH variables and if you are testing it on your computer, you must change the path. For god sake, do not use a disk where operating system is installed and do not use a disk which has low disk space. Anyways, a professional will not do such mistake.


#---------------------------------------------------------------------------------------------#
#--Script to copy files from one location to another with a cool Progressbar --#
# If you want to test it - Change the source path and target path with a relevant directory
# in your computer -
# $SOURCE_PATH
# $TARGET_PATH
#---------------------------------------------------------------------------------------------#
# Note : The script will copy only files from source to target, please use it carefully
# if the low disk space problem in target drive.
# Do not use C:\ drive or drive where Operating system in installed.
#---------------------------------------------------------------------------------------------#
function Copy-Files([string]$SOURCE_PATH, [string]$TARGET_PATH)
{
$FILES_COLLECTION=ls $SOURCE_PATH | where { $_.Mode -notcontains "d----" } | foreach { $_.FullName}$FILES_COUNT=$FILES_COLLECTION.count
for ( $t =0; $t -lt $FILES_COUNT ; $t++)
{
$status = [int] (($t/$FILES_COUNT) * 100)
#--Get the full-file name and only leaf name of the file --#
$THIS_FILE_FULL_NAME = $FILES_COLLECTION[$t] $THIS_FILE_NAME = Split-Path $THIS_FILE_FULL_NAME -leaf
#--Set the target location --3
$THIS_FILE_TARGET = "${TARGET_PATH}\${THIS_FILE_NAME}"
#--write progress with progressbar --#
Write-Progress -id 1 -Activity "File-Copy Status - [$status]" -Status "Copying to $THIS_FILE_NAME to $TARGET_PATH " -PercentComplete $status
#--Copy Step --#
Copy-Item $THIS_FILE_FULL_NAME $THIS_FILE_TARGET
}
}
#---------------------------------------------------------------------------------------------#
Copy-Files "D:\Dir1" "D:\Dir2"
#---------------------------------------------------------------------------------------------#
# End of Code
#---------------------------------------------------------------------------------------------#
view raw progressbar.ps1 hosted with ❤ by GitHub



Isn't this easy to use Write-Progress in your other scripts!
Enjoy Scripting!

No comments:

Post a Comment

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...