File Management
Self-taught, AI-assisted project to categorize thousands of files according to naming conventions
Code snippet for migrating unsorted PDF files to mimic organization of sorted Adobe Illustrator files:
# Prompt for the location of unsorted PDFs
$sourceFolder = Read-Host "Enter the location of unsorted PDFs"
# Prompt for the location of AI files
$aiFolder = Read-Host "Enter the location of AI files"
# Prompt for the destination folder for the sorted PDFs
$destinationFolder = Read-Host "Enter the destination folder for sorted PDFs"
# Get a list of all PDF files in the source folder
$pdfFiles = Get-ChildItem -Path $sourceFolder -Filter *.pdf
# Loop through each PDF file
foreach ($pdfFile in $pdfFiles) {
# Get the PDF file name without extension
$pdfFileName = [System.IO.Path]::GetFileNameWithoutExtension($pdfFile.Name)
# Check if the PDF file name contains a suffix like "_R1"
$suffix = $pdfFileName -replace '^.*_([Rr]\d+)$', '$1'
if ($suffix -ne $pdfFileName) {
# Remove the suffix from the file name
$pdfFileName = $pdfFileName -replace "_$suffix$"
} else {
$suffix = $null
}
# Search for matching AI file in the AI folder
$matchingAiFile = Get-ChildItem -Path $aiFolder -Recurse -Filter "$pdfFileName.ai" -File | Select-Object -First 1
if ($matchingAiFile) {
# Get the subdirectory containing the matching AI file
$subdirectory = $matchingAiFile.Directory.Name
# Construct the destination path
$destinationPath = Join-Path -Path $destinationFolder -ChildPath $subdirectory
# Create the destination directory if it doesn't exist
if (-not (Test-Path -Path $destinationPath)) {
New-Item -Path $destinationPath -ItemType Directory | Out-Null
}
# Move the PDF file to the corresponding subdirectory in the destination folder
Move-Item -Path $pdfFile.FullName -Destination (Join-Path -Path $destinationPath -ChildPath "$pdfFileName$suffix.pdf")
Write-Host "Successfully moved $($pdfFile.Name) to $($destinationPath)"
} else {
Write-Host "No matching AI file found for $($pdfFile.Name)"
}
}
Write-Host "Script execution completed."
Code snippet for organizing PDFs into subdirectories, mimicking Adobe Illustrator files’ organization:
# Input folder paths (replace with your actual paths)
$aiFolder = Read-Host "Enter the location of the AI files"
$pdfFolder = Read-Host "Enter the location of the PDF files"
# Function to move PDFs with matching names to PDF subdirectories
function Move-MatchingPDFs {
param (
[string]$aiFolder,
[string]$pdfFolder
)
try {
# Get a list of PDF files
$pdfFiles = Get-ChildItem -Path $pdfFolder -Filter "*.pdf" -File -Recurse
# Iterate through AI files in subdirectories
Get-ChildItem -Path $aiFolder -File -Recurse -Filter "*.ai" | ForEach-Object {
$aiFile = $_
$aiFileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($aiFile.Name)
$pdfNamePrefix = $aiFileNameWithoutExtension
# Find matching PDF files (including revisions)
$matchingPdf = $pdfFiles | Where-Object {
$pdfBaseName = [System.IO.Path]::GetFileNameWithoutExtension($_.Name)
$pdfBaseName.StartsWith($pdfNamePrefix) -and ($pdfBaseName -eq $pdfNamePrefix -or $_.Name -match "_R\d*.pdf")
}
foreach ($pdf in $matchingPdf) {
# Determine the AI subdirectory path
$aiSubdirectory = Join-Path -Path $aiFolder -ChildPath $aiFile.Directory.Name
# Determine the PDF subdirectory path
$pdfSubdirectory = Join-Path -Path $pdfFolder -ChildPath $aiFile.Directory.Name
# Create the PDF subdirectory if it doesn't exist
if (-not (Test-Path -Path $pdfSubdirectory -PathType Container)) {
New-Item -Path $pdfSubdirectory -ItemType Directory -Force
}
# Move the PDF file to the PDF subdirectory
$pdfDestination = Join-Path -Path $pdfSubdirectory -ChildPath $pdf.Name
Move-Item -Path $pdf.FullName -Destination $pdfDestination -Force
Write-Host "Moved $($pdf.Name) to $($pdfDestination)"
}
}
} catch {
Write-Host "An error occurred: $($_.Exception.Message)"
}
}
# Call the function to move matching PDFs
Move-MatchingPDFs -aiFolder $aiFolder -pdfFolder $pdfFolder
# Confirmation message
Write-Host "Processing complete."