Untitled
unknown
plain_text
a year ago
1.6 kB
18
Indexable
# Define the folder path
$folderPath = "E:\DOCS\Wallpapers"
# Check if the folder exists
if (-not (Test-Path -Path $folderPath -PathType Container)) {
Write-Host "Error: The folder '$folderPath' does not exist. Please check the path and try again." -ForegroundColor Red
exit
}
# Initialize counter for renamed files
$renamedCount = 0
# Part 1: Rename all files with random 5-digit names
$files = Get-ChildItem -Path $folderPath -File
foreach ($file in $files) {
# Generate random 5-digit number
$randomNumber = Get-Random -Minimum 10000 -Maximum 99999
$newName = "$randomNumber$($file.Extension)"
$newPath = Join-Path -Path $folderPath -ChildPath $newName
# Ensure unique name by checking if file exists
while (Test-Path -Path $newPath) {
$randomNumber = Get-Random -Minimum 10000 -Maximum 99999
$newName = "$randomNumber$($file.Extension)"
$newPath = Join-Path -Path $folderPath -ChildPath $newName
}
Rename-Item -Path $file.FullName -NewName $newName
$renamedCount++
}
# Part 2: Rename files to Wall-01, Wall-02 format
$files = Get-ChildItem -Path $folderPath -File | Sort-Object Name
$counter = 1
foreach ($file in $files) {
$newName = "Wall-{0:D2}$($file.Extension)" -f $counter
$newPath = Join-Path -Path $folderPath -ChildPath $newName
Rename-Item -Path $file.FullName -NewName $newName
$counter++
}
# Output the total number of files renamed
Write-Host "File renaming completed successfully!" -ForegroundColor Green
Write-Host "Total files renamed: $renamedCount" -ForegroundColor GreenEditor is loading...
Leave a Comment