-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy-File.ps1
More file actions
165 lines (138 loc) · 4.57 KB
/
Copy-File.ps1
File metadata and controls
165 lines (138 loc) · 4.57 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
function Copy-File
{
<#
.SYNOPSIS
Kopiert eine Datei mit Fortschrittsanzeige
Version: 0.1
++++++++++++
.DESCRIPTION
Kopiert eine Datei mit Fortschrittsanzeige.
Mit Copy-File kann nur eine einzelne Datei kopiert werden. Die Verwendung von Wildcards ist nicht möglich.
Sofern die Datei im Ziel schon existiert, wird nicht kopiert.
.PARAMETER Source
Legt die Quelle fest.
.PARAMETER Destination
Legt das Ziel fest.
.PARAMETER BufferSize
Legt die Größe des Speicherpuffer für den Kopiervorgang fest.
Per Vorgabe ist die Größe auf 8192 Bytes (8KB) festgelegt.
.PARAMETER AutoBufferSize
Durch die Verwendung des Schalterparameter AutoBufferSize, wird der Puffer in Abhängigkeit der Dateigröße
automatisch reserviert. Eine ggf. vorhandene Angabe in BufferSize wird ignoriert.
.PARAMETER OBuffer
Mit dem Parameter Buffer kann ein Speicherbereich übergeben werden. In dem Fall reserviert Copy-File
keinen Speicher, sondern verwendet den übergebenen Speicher für die Kopieraktion. Dies ist insbesondere
beim Kopieren von mehreren Dateien hilfreich, da damit nicht jedesmal aufs Neue Speicher allokiert
werden muss.
.EXAMPLE
Copy-File -Source C:\MyDir1\datei1.bin -Destinatination C:\MyDir1\Kopie_datei1.bin
dir *.avhdx | `
% {Copy-File -Source $_.FullName -Destination (Join-Path -Path C:\SaveMyVM\Kopie\ -ChildPath $_.Name) -AutoBufferSize -Verbose}
#>
param(
[Parameter(Mandatory=$true)]
[String] $Source,
[Parameter(Mandatory=$true)]
[String] $Destination,
[long] $BufferSize = 8KB,
[byte[]] $OBuffer = $null,
[Switch] $AutoBufferSize
)
$copyProgress = $false
$workDirectory = Get-Location
$items = $Source.Split('\')
if($null -ne $items -and $items.Length -eq 1)
{
$Source = Join-Path -Path $workDirectory -ChildPath $Source
}
$items = $Destination.Split('\')
if($null -ne $items -and $items.Length -eq 1)
{
$Destination = Join-Path -Path $workDirectory -ChildPath $Destination
}
$result = Test-Path -Path $Source
if($result -eq $false)
{
Write-Error -Message "$Source nicht gefunden."
return
}
$result = Test-Path -Path $Destination
if($result -eq $true)
{
Write-Warning -Message "$Destionation schon vorhanden!"
return
}
try
{
$hsource = [io.file]::OpenRead($Source)
$hdest = [io.file]::OpenWrite($Destination)
if($hsource -eq $null)
{
Write-Error "$Source konnte nicht geöffnet werden."
return
}
if($hdest -eq $null)
{
Write-Error "$Destination konnte nicht geöffnet werden."
return
}
# Speicher für Puffer allokieren, sofern kein OutBuffer
if($AutoBufferSize -eq $true -and $null -eq $OBuffer)
{
$f = Get-Item -Path $Source
$fileSize = $f.Length
$BufferSize = 64KB
if($fileSize -ge 10MB)
{
$BufferSize = 500KB
}
if($fileSize -ge 100MB)
{
$BufferSize = 12MB
}
if($fileSize -ge 1GB)
{
$BufferSize = 128MB
}
}
$buffer = $null
if($null -ne $OBuffer)
{
$buffer = $OBuffer
$BufferSize = $OBuffer.Length
}
else {
[byte[]]$buffer = New-Object Byte[] $BufferSize
}
Write-Verbose "Pufferspeicher: $BufferSize Byte"
$total = 0
$counter = 0
$copyProgress = $true
do
{
$counter = $hsource.Read($buffer, 0, $buffer.Length)
$hdest.Write($buffer, 0, $counter)
$total += $counter
# Division durch Null vermeiden
if($hSource.Length -gt 0)
{
Write-Progress -Activity "Kopiere $Source" -PercentComplete ($total/$hsource.Length*100)
}
} while($counter -gt 0)
}
finally
{
if($null -ne $hsource)
{
$hsource.Dispose()
}
if($null -ne $hdest)
{
$hdest.Dispose()
}
if($copyProgress -eq $true)
{
Write-Progress -Activity "Kopiere $Source" -Completed
}
}
}