Skip to content

Instantly share code, notes, and snippets.

@jinnosux
Created March 13, 2025 22:11
Show Gist options
  • Save jinnosux/404c0d2b868db090b0ae8ec6418f8991 to your computer and use it in GitHub Desktop.
Save jinnosux/404c0d2b868db090b0ae8ec6418f8991 to your computer and use it in GitHub Desktop.
# Assignment 1 - Vahid Konicanin
#
# Script to move files with specified extension while preserving directory structure
# Usage: .\assignment1.ps1 -SourceDirectory "C:\Source" -TargetDirectory "C:\Target" -FileExtension "pdf"
param (
[string]$SourceDirectory,
[string]$TargetDirectory,
[string]$FileExtension = "*"
)
# Prompt for source directory if not provided
if (-not $SourceDirectory) {
$SourceDirectory = Read-Host "Please enter the source directory path"
}
# Ensure source directory exists
if (-not (Test-Path -Path $SourceDirectory -PathType Container)) {
Write-Host "Error: Source directory '$SourceDirectory' does not exist."
exit 1
}
# Prompt for target directory if not provided
if (-not $TargetDirectory) {
$TargetDirectory = Read-Host "Please enter the target directory path"
}
# Ensure target directory exists, create if not
if (-not (Test-Path -Path $TargetDirectory -PathType Container)) {
try {
New-Item -Path $TargetDirectory -ItemType Directory -Force | Out-Null
Write-Host "Created target directory: $TargetDirectory"
}
catch {
Write-Host "Error: Failed to create target directory '$TargetDirectory'. $_"
exit 1
}
}
# Prepare file extension filter
$filter = "*"
if ($FileExtension -ne "*") {
# Normalize file extension format (ensure it starts with a dot)
if (-not $FileExtension.StartsWith(".")) {
$FileExtension = ".$FileExtension"
}
$filter = "*$FileExtension"
}
Write-Host "Starting file migration..."
Write-Host "Source: $SourceDirectory"
Write-Host "Target: $TargetDirectory"
Write-Host "Extension filter: $filter"
# File counters
$filesFound = 0
$filesMoved = 0
$filesSkipped = 0
# Get all files with the specified extension
try {
$files = Get-ChildItem -Path $SourceDirectory -Filter $filter -Recurse -File -ErrorAction Stop
$filesFound = $files.Count
Write-Host "Found $filesFound file(s) matching the filter"
foreach ($file in $files) {
# Get relative path to preserve directory structure
$relativePath = $file.FullName.Substring($SourceDirectory.Length)
$relativePath = $relativePath.TrimStart("\", "/")
$targetPath = Join-Path -Path $TargetDirectory -ChildPath $relativePath
$targetDir = Split-Path -Path $targetPath -Parent
# Create target directory if it doesn't exist
if (-not (Test-Path -Path $targetDir -PathType Container)) {
New-Item -Path $targetDir -ItemType Directory -Force | Out-Null
}
# Check if target file already exists
if (Test-Path -Path $targetPath -PathType Leaf) {
Write-Host "Skipping: File already exists at target location: $targetPath"
$filesSkipped++
continue
}
# Move the file
try {
Move-Item -Path $file.FullName -Destination $targetPath -Force
Write-Host "Moved: $($file.FullName) -> $targetPath"
$filesMoved++
}
catch {
Write-Host "Error moving file $($file.FullName): $_"
$filesSkipped++
}
}
}
catch {
Write-Host "Error scanning source directory: $_"
exit 1
}
# Summary logs
Write-Host "Migration complete!"
Write-Host "Files found: $filesFound"
Write-Host "Files moved: $filesMoved"
Write-Host "Files skipped: $filesSkipped"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment