Copy files with progress indicator with Powershell
Copy files with progress indicator with Powershell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$sourcePath = 'C:\folder1' $destinationPath = 'C:\folder2' $files = Get-ChildItem -Path $sourcePath -Recurse $filecount = $files.count $i=0 Foreach ($file in $files) { $i++ Write-Progress -activity "Moving files..." -status "($i of $filecount) $file" -percentcomplete (($i/$filecount)*100) # Determine the absolute path of this object's parent container. This is stored as a different attribute on file and folder objects so we use an if block to cater for both if ($file.psiscontainer) {$sourcefilecontainer = $file.parent} else {$sourcefilecontainer = $file.directory} # Calculate the path of the parent folder relative to the source folder $relativepath = $sourcefilecontainer.fullname.SubString($sourcepath.length) # Copy the object to the appropriate folder within the destination folder copy-Item $file.fullname ($destinationPath + $relativepath) } |
I.
Thank For This code.
But.. If I want Exclude a folder… ?
Exemple :
$sourcepatch = “C:\Users\$Users\” [In this Folder I don’t want ‘APPDATA’ folder]
Thank
I can’t be sure, and can’t test right now, but I think all you have to do is add “-exclude to line 2 above on the Get-Childitem
You can use something like (-Exclude)
$Exclude = (Get-ChildItem $DestinationPath -Recurse -ErrorAction SilentlyContinue)
copy-Item $file.fullname -exclude $exclude ($destinationPath + $relativepath)