Progressbar directory recursive

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
Post Reply
GusFrings
Posts: 6
Joined: Thu Nov 15, 2018 3:24 am

Progressbar directory recursive

Post by GusFrings » Thu Dec 06, 2018 4:13 am

I'm using this code to show a progressbar for copying files.

Code: Select all

		$selectedPath = "c:\test"		
		$files = Get-ChildItem $selectedPath -Filter *.txt
		
		if ($files -eq $null -or $files.Count -eq 0)
		{

			#No files to backup
			return
		}
		
		#Initialize the Progress Bar
		$progressbar1.Maximum = $files.Count
		$progressbar1.Step = 1
		$progressbar1.Value = 0
		
		#Create the Backup Folder
		$destination = ('{0}\\Backup' -f $selectedPath)
		[System.IO.Directory]::CreateDirectory($destination)
		
		#Copy the files and update the progress bar
		foreach ($file in $files)
		{
			Copy-Item ('{0}\\{1}' -f $selectedPath, $file) -Destination $destination
			$progressbar1.PerformStep()
		}
Now I would like to do the same thing with the recursive copy of directories

Code: Select all

        $files = Get-ChildItem -Path $selectedPath | Where-Object { $_.Name -match "@" }
	foreach ($file in $files)
	{
		Copy-Item ('{0}\\{1}' -f $selectedPath, $file) -Destination $destination -Recurse -Force
	$progressbar1.PerformStep()
	}
The directories have thousands of files but I see the progressbar only increase when the copy of the entire dir is completed while I would like the progression to be shown when copying all the files. How could I modify the script to get all this? Thank you

User avatar
jvierra
Posts: 12950
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Progressbar directory recursive

Post by jvierra » Thu Dec 06, 2018 5:12 am

That is correct. A recursive copy blocks the thread until it is complete. Use the Job Tracker custom control set to create a job that displays a progress indicator.

See: https://info.sapien.com/index.php/guis/gui-advanced-tips/powershell-studio-creating-responsive-forms

GusFrings
Posts: 6
Joined: Thu Nov 15, 2018 3:24 am

Re: Progressbar directory recursive

Post by GusFrings » Thu Dec 06, 2018 5:23 am

I read what you indicated to me but I can not apply it to my case.
Could you tell me how to do it?
Thanks for your help.

User avatar
jvierra
Posts: 12950
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Progressbar directory recursive

Post by jvierra » Thu Dec 06, 2018 5:37 am

The link I posted has complete instructions and an explanation that is better than I can do in a post. If this is your first time using PowerShell with forms you may need to take some time to read other articles on forms development.

If you just create a new form and add the control it serves as a demo of how the Job Tracker works.

I should also note that you file copy code does not make much sense. Why use recurse with a single file? Why do you need to rebuild the file name to copy it. Just copy the file.

It is much faster and more efficient to use RoboCopy to copy a large number of files.

GusFrings
Posts: 6
Joined: Thu Nov 15, 2018 3:24 am

Re: Progressbar directory recursive

Post by GusFrings » Thu Dec 06, 2018 6:57 am

Ok thanks for your help

Post Reply