forked from ChrisTitusTech/winutil
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCopy-Files.ps1
More file actions
79 lines (69 loc) · 3.31 KB
/
Copy-Files.ps1
File metadata and controls
79 lines (69 loc) · 3.31 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
function Copy-Files {
<#
.DESCRIPTION
Copies the contents of a given ISO file to a given destination
.PARAMETER Path
The source of the files to copy
.PARAMETER Destination
The destination to copy the files to
.PARAMETER Recurse
Determines whether or not to copy all files of the ISO file, including those in subdirectories
.PARAMETER Force
Determines whether or not to overwrite existing files
.EXAMPLE
Copy-Files "D:" "C:\ISOFile" -Recurse -Force
#>
param (
[string]$Path,
[string]$Destination,
[switch]$Recurse = $false,
[switch]$Force = $false
)
try {
$files = Get-ChildItem -Path $path -Recurse:$recurse
Write-Host "Copy $($files.Count) file(s) from $path to $destination"
foreach ($file in $files) {
$status = "Copying file {0} of {1}: {2}" -f $counter, $files.Count, $file.Name
Write-Progress -Activity "Copy disc image files" -Status $status -PercentComplete ($counter++/$files.count*100)
$restpath = $file.FullName -Replace [regex]::Escape($path), ''
if ($file.PSIsContainer -eq $true) {
$targetPath = Join-Path $destination $restpath
Write-Debug "Creating $targetPath"
New-Item $targetPath -Force:$force -Type Directory -ErrorAction SilentlyContinue
} else {
$targetPath = Join-Path $destination $restpath
Write-Debug "Copy from $($file.FullName) to $targetPath"
try {
Copy-Item $file.FullName $targetPath -ErrorAction Stop -Force:$force
# Remove ReadOnly attribute using attrib for consistency
& attrib -R $targetPath 2>$null
# Force garbage collection to release file handles
$copiedFile = $null
} catch {
Write-Debug "Failed to copy $($file.FullName): $($_.Exception.Message)"
# Try alternative method if standard copy fails
try {
[System.IO.File]::Copy($file.FullName, $targetPath, $force)
# Remove ReadOnly attribute using attrib for consistency
& attrib -R ($destination\$restpath) 2>$null
} catch {
Write-Debug "Alternative copy method also failed: $($_.Exception.Message)"
}
}
}
}
Write-Progress -Activity "Copy disc image files" -Status "Ready" -Completed
# Force cleanup to release any remaining file handles
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
[System.GC]::Collect()
Write-Host "File copy completed. Released file handles for unmount."
} catch {
Write-Host "Unable to Copy all the files due to an unhandled exception" -ForegroundColor Yellow
Write-Host "Error information: $($_.Exception.Message)`n" -ForegroundColor Yellow
Write-Host "Additional information:" -ForegroundColor Yellow
Write-Host $PSItem.Exception.StackTrace
# Write possible suggestions
Write-Host "`nIf you are using an antivirus, try configuring exclusions"
}
}