Skip to content

Instantly share code, notes, and snippets.

@psiborg
Created January 7, 2025 02:13
Show Gist options
  • Save psiborg/92401d5473bccf13c007c872c5ec7cb1 to your computer and use it in GitHub Desktop.
Save psiborg/92401d5473bccf13c007c872c5ec7cb1 to your computer and use it in GitHub Desktop.
Rename all booklist to _booklist_
# Define the target directory
$targetDirectory = "C:\Path\To\Your\Directory"
# Initialize counters for each file type
$counterPDF = 0
$counterTXT = 0
# Process both "booklist.pdf" and "booklist.txt"
foreach ($fileType in @("booklist.pdf", "booklist.txt")) {
Get-ChildItem -Path $targetDirectory -Recurse -Filter $fileType | ForEach-Object {
$oldPath = $_.FullName
$newPath = Join-Path $_.DirectoryName ("_booklist_" + $_.Extension)
Write-Host "Renaming: $oldPath`n"
Rename-Item -Path $oldPath -NewName $newPath -Force
# Update counters
if ($_.Extension -eq ".pdf") { $counterPDF++ }
if ($_.Extension -eq ".txt") { $counterTXT++ }
}
}
# Display summary
Write-Host "`nSummary:" -ForegroundColor Green
Write-Host "Renamed $counterPDF booklist.pdf file(s)." -ForegroundColor Cyan
Write-Host "Renamed $counterTXT booklist.txt file(s)." -ForegroundColor Magenta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment