Hello to all,
I have a question, maybe someone here can help me.
I mount my ISO file and copy the files into a directory.
Now I want to copy this directory "all Files and Folders" to my USB stick, USB drive is read out before.
Question:
How can I use Copy-Item to copy my files to the USB stick incl. a progressbar for the graphical progress on my form.
I am grateful for any help.
Powershell Copy-Item with progressBar
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: Powershell Copy-Item with progressBar
I would recommend using "Robocopy" to batch copy as it returns a marker for each block of files copied. Copy-Item has no way to do that. It is a blocking call and returns nothing. You can also write your own file copy mechanism using file primitives but that would require knowing how to write a systems level program. Robocopy has all of the pieces to trigger a ProgressBar.
Robocopy Documentation
Robocopy Documentation
-
- Posts: 1
- Last visit: Mon Aug 22, 2022 11:29 am
Re: Powershell Copy-Item with progressBar
I didn't author this function but I do have this here:
Hope it helps
- param( [string]$from, [string]$to)
- $ffile = [io.file]::OpenRead($from)
- $tofile = [io.file]::OpenWrite($to)
- Write-Progress -Activity "Copying file" -status "$from -> $to" -PercentComplete 0
- try {
- [byte[]]$buff = new-object byte[] 4096
- [long]$total = [int]$count = 0
- do {
- $count = $ffile.Read($buff, 0, $buff.Length)
- $tofile.Write($buff, 0, $count)
- $total += $count
- if ($total % 1mb -eq 0) {
- Write-Progress -Activity "Copying file" -status "$from -> $to" `
- -PercentComplete ([long]($total * 100 / $ffile.Length))
- }
- } while ($count -gt 0)
- }
- finally {
- $ffile.Dispose()
- $tofile.Dispose()
- Write-Progress -Activity "Copying file" -Status "Ready" -Completed
- }
Re: Powershell Copy-Item with progressBar
hello
i make with robocopy
my progressbar on my fom doesn't work, it only shows the 100% at the end of the copy, but not the progress. Can someone help me with this ?
i make with robocopy
Code: Select all
process {
#count the source files
$outputBox1.text = " Robocopy wird vorbereitet. Bitte warten..."
if ($InputSource.Text -notlike $null) {
$sourcefiles=robocopy.exe $Letter $Volume /L /S /NJH /BYTES /FP /NC /NDL /TS /XJ /R:0 /W:0
If ($sourcefiles[-5] -match '^\s{3}Files\s:\s+(?<Count>\d+).*') {$filecount=$matches.Count}
$i = 1
}
$outputBox1.Focus()
$run = robocopy.exe $Letter $Volume $switchNP $switchMIR $switchLogfile | foreach {
$ErrorActionPreference = "silentlycontinue"
#calculate percentage
$i++
[int]$pct = ($i/$filecount)*100
#update the progress bar
$progressbar.Value = ($pct)
$outputBox1.AppendText($_ + "`r`n")
[void] [System.Windows.Forms.Application]::DoEvents()
}
}
end {$progressbar.Value = 100}
} #end robocopy function
Re: Powershell Copy-Item with progressBar
To use RoboCopy with a ProgressBar you need to use a job and the JobTracker custom control set. If you search for examples, you will find many.
Here is one example: viewtopic.php?t=11452
Here is one example: viewtopic.php?t=11452
Re: Powershell Copy-Item with progressBar
thank you for something like this I had been looking for, everything worked I thank you.