Skip to content

Instantly share code, notes, and snippets.

@DineshSolanki
Last active February 14, 2024 16:59
Show Gist options
  • Save DineshSolanki/84edeb6e72e867b02239247a794e0dff to your computer and use it in GitHub Desktop.
Save DineshSolanki/84edeb6e72e867b02239247a794e0dff to your computer and use it in GitHub Desktop.
FoliCon v4.0 uses different naming scheme of icon names, therefore deletion of old files or manual rename is necessary
function Rename-FolderIcons {
param(
[string]$folderPath,
[int]$depth = 0
)
$subDirectories = Get-ChildItem -Path $folderPath -Directory
$totalDirs = $subDirectories.Count
for ($i = 0; $i -lt $totalDirs; $i++) {
$subDir = $subDirectories[$i]
Write-Progress -Activity "Processing directories" -Status "$($subDir.FullName)" -PercentComplete (($i / $totalDirs) * 100) -Id $depth
Rename-FolderIcons -folderPath $subDir.FullName -depth ($depth + 1)
}
# Check if desktop.ini files are there
if (Test-Path "$folderPath\desktop.ini") {
# Change the attributes to Normal
attrib -S -H -R "$folderPath\desktop.ini"
$desktopFileContent = Get-Content "$folderPath\desktop.ini"
$iconFileName = [regex]::Matches($desktopFileContent[1], '(?<=IconResource=)[^\,]*', 'IgnoreCase').Value
# If icon file with IconFileName exists, rename it and update desktop.ini
if (Test-Path "$folderPath\$iconFileName") {
# Rename .ico file
Rename-Item -Path "$folderPath\$iconFileName" -NewName "folicon.ico" -ErrorAction SilentlyContinue
# Update IconResource in desktop.ini
$desktopFileContent = $desktopFileContent -replace $iconFileName, 'folicon.ico'
$desktopFileContent | Set-Content "$folderPath\desktop.ini"
attrib +S +H "$folderPath\desktop.ini"
}
}
if ($depth -eq 0) {
Write-Progress -Activity "Processing directories" -Status "Done" -Completed
}
}
# Check for command line arguments
if (!$args[0]) {
# Get the current directory
$currentDirectory = Get-Location
# Ask the user if they want to proceed with the current directory
$userInput = Read-Host "No directory path was provided. Would you like to proceed with current directory ($currentDirectory)? [y/n]"
if ($userInput -eq "y") {
# If user agrees, run the function with the current path
Rename-FolderIcons -folderPath ${currentDirectory.ToString()}
} else {
Write-Host "User input was not 'y'. Exiting."
}
} else {
# If argument is provided, run with it
Rename-FolderIcons -folderPath $args[0]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment