Skip to content

Instantly share code, notes, and snippets.

@tomesparon
Created October 8, 2025 15:31
Show Gist options
  • Select an option

  • Save tomesparon/6843f5f2f97ff8c941096f9874046824 to your computer and use it in GitHub Desktop.

Select an option

Save tomesparon/6843f5f2f97ff8c941096f9874046824 to your computer and use it in GitHub Desktop.
robocopy off of
# Define the volume label of the USB drive
$targetLabel = "cyc"
$destinationRoot = "\\FSERVER\Archive\test" # Replace with your actual network share path
# Change filter to *.dcm if these are DICOM files
$fileFilter = "*.raw"
# Find the USB volume by label (DriveType 2 = Removable)
$usbDrive = Get-CimInstance Win32_Volume |
Where-Object { $_.Label -eq $targetLabel -and $_.DriveType -eq 2 } |
Select-Object -First 1
if (-not $usbDrive) {
Write-Host "USB drive with label '$targetLabel' not found."
Read-Host "Press Enter to close..."
exit 1
}
$sourceRoot = $usbDrive.Name
$logFile = Join-Path $sourceRoot "dicom_copy_log.txt"
# Ensure destination exists (robocopy can create, but this also checks access)
if (-not (Test-Path -LiteralPath $destinationRoot)) {
try {
New-Item -ItemType Directory -Path $destinationRoot -Force | Out-Null
} catch {
Write-Error "Destination '$destinationRoot' is not accessible or cannot be created. $_"
Read-Host "Press Enter to close..."
exit 2
}
}
# Build robocopy arguments
# Syntax: robocopy <Source> <Dest> [File] [Options]
$robocopyArgs = @(
$sourceRoot,
$destinationRoot,
$fileFilter, # Only these files
"/S", # Include subdirs (skip empty)
"/R:1", "/W:1", # Retry once, wait 1s
"/ETA", # Show estimated time remaining
"/TEE", # Show output in console as well as log
"/LOG:`"$logFile`"" # Log to USB (quote path in case of spaces)
)
# Run robocopy and stream output to the console
# (Out-Host ensures the text is written live to the screen)
& robocopy @robocopyArgs | Out-Host
$rc = $LASTEXITCODE
# Robocopy exit code semantics: 0-7 = success/ok, 8+ = failure
if ($rc -ge 8) {
Write-Error "Robocopy failed with exit code $rc. See log: $logFile"
} else {
Write-Host "Backup complete. Robocopy exit code $rc (success). Log: $logFile"
}
# Keep the window open so you can read the messages
Read-Host "Press Enter to close..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment