This repository was archived by the owner on Dec 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathCopy-DirectoryWithProgress.ps1
More file actions
210 lines (146 loc) · 6.64 KB
/
Copy-DirectoryWithProgress.ps1
File metadata and controls
210 lines (146 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<# Copyright (C) 2008-2018 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#>
Function Copy-DirectoryWithProgress {
<#
.SYNOPSIS
Copies a directory and its contents to a new location, CopyOptions can be specified,
and the possibility of notifying the application of its progress through a callback function.
#>
[OutputType([Alphaleonis.Win32.Filesystem.CopyMoveResult])]
Param(
[String]$SourceFolderFullPath,
[String]$DestinationFolderFullPath,
[Alphaleonis.Win32.Filesystem.CopyOptions]$CopyOptions,
[Alphaleonis.Win32.Filesystem.CopyMoveProgressRoutine]$Callback
)
Process {
[Alphaleonis.Win32.Filesystem.Directory]::Copy($SourceFolderFullPath, $DestinationFolderFullPath, $CopyOptions, $Callback, $Null)
}
}
Function UnitSizeToText {
<#
.SYNOPSIS
Converts a number to a string, suffixed with a unit size.
#>
[OutputType([String])]
Param(
[Long]$numberOfBytes
)
[Array]$Private:sizeFormats = "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
[Int]$Private:index = 0
while ($numberOfBytes -gt 1kb)
{
$numberOfBytes /= 1kb
$index++
}
"{0:N0} {1}" -f $numberOfBytes, $sizeFormats[$index]
}
[ScriptBlock]$FolderCopyProgressHandler = {
<#
.SYNOPSIS
The folder copy progress handler.
#>
[OutputType([Alphaleonis.Win32.Filesystem.CopyMoveProgressResult])]
Param(
# The total size of the file, in bytes.
[Long]$TotalFileSize,
# The total number of bytes transferred from the source file to the destination file since the copy operation began.
[Long]$TotalBytesTransferred,
# The total size of the current file stream, in bytes.
[Long]$StreamSize,
# The total number of bytes in the current stream that have been transferred from the source file to the destination file since the copy operation began.
[Long]$StreamBytesTransferred,
# A handle to the current stream. The first time CopyProgressRoutine is called, the stream number is 1.
[Int]$StreamNumber,
# The reason that CopyProgressRoutine was called.
[Int]$CallbackReason,
[Object]$userData
)
Process {
# Another part of the data file was copied.
#[Int]$Private:CALLBACK_CHUNK_FINISHED = 0
# Another stream was created and is about to be copied. This is the callback reason given when the callback routine is first invoked.
[Int]$Private:CALLBACK_STREAM_SWITCH = 1
# Add size of all copied files.
If ($CallbackReason -eq $CALLBACK_STREAM_SWITCH) { $Script:AllBytesTransferred += $TotalFileSize }
[Double]$Private:percent = ($TotalBytesTransferred / $TotalFileSize * 100)
[String]$Private:activity = ('Copying folder. Copied: {0}' -f (UnitSizeToText($AllBytesTransferred)))
# Write-Progress can slow things down CONSIDERABLY; Disable when there is no need for progress report.
Write-Progress -Activity $activity -Status ('{0:N2}%' -f $percent) -PercentComplete $percent
# Possible Write-Progress alternative.
#$Host.UI.RawUI.WindowTitle = $activity
return [Alphaleonis.Win32.Filesystem.CopyMoveProgressResult]::Continue
}
}
Function DemoCopy-DirectoryWithProgress {
<#
2018-02-25 Tested on:
Name Value
---- -----
PSVersion 5.1.14409.1005
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1005
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
#>
Begin {
Clear-Host
$Error.Clear()
# Specify an existing folder to copy.
[String]$Private:srcFolderFullPath = 'C:\A Big Folder'
[String]$Private:dstFolderFullPath = "$Env:Temp\Copy-DirectoryWithProgress-Test"
# Demo code: Used for assertion.
[Long]$Script:AllBytesTransferred = 0
[Bool]$Private:failed = $True
# Allow copy to overwrite an existing file.
$Private:copyOptions = [Alphaleonis.Win32.Filesystem.CopyOptions]::None
}
Process {
$Private:cmr = Copy-DirectoryWithProgress `
-SourceFolderFullPath $srcFolderFullPath `
-DestinationFolderFullPath $dstFolderFullPath `
-CopyOptions $copyOptions `
-Callback $FolderCopyProgressHandler
}
End {
# Assert no $Error's.
$failed = $Error.Count -gt 0
If ($failed) { Write-Warning -Message 'Errors encountered.' }
# Assert not $Null.
$failed = $Null -eq $cmr
If ($failed) { Write-Warning -Message 'Result is null.' }
Else {
# Assert bytes transferred.
$failed = $Script:AllBytesTransferred -ne $cmr.TotalBytes
If ($failed) { Write-Warning -Message ('The number of bytes copied does not match: {0:N0} vs {1:N0}' -f $Script:AllBytesTransferred, $cmr.TotalBytes) }
Else {
If ($cmr.ErrorCode -eq 0) { Write-Host -ForegroundColor Green ('Folder copied successfully. Message: {0}' -f $cmr.ErrorMessage) -NoNewline }
Else { Write-Host -ForegroundColor Red ('Folder copied with errors. Message: {0}' -f $cmr.ErrorMessage) -NoNewline }
}
$cmr
}
}
}
Import-Module -Name 'PATH TO\AlphaFS.dll'
DemoCopy-DirectoryWithProgress