Advertisement
Guest User

Untitled

a guest
Apr 28th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. # Define the FolderIconSetter class
  2. Add-Type -TypeDefinition @"
  3. using System;
  4. using System.Runtime.InteropServices;
  5.  
  6. public class FolderIconSetter {
  7. [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
  8. public static extern int SHGetSetFolderCustomSettings(ref SHFOLDERCUSTOMSETTINGS pfcs, string pszPath, int dwReadWrite);
  9.  
  10. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  11. public struct SHFOLDERCUSTOMSETTINGS {
  12. public int dwSize;
  13. public uint dwMask;
  14. public IntPtr pvid;
  15. public string pszWebViewTemplate;
  16. public uint cchWebViewTemplate;
  17. public string pszWebViewTemplateVersion;
  18. public string pszInfoTip;
  19. public uint cchInfoTip;
  20. public IntPtr pclsid;
  21. public uint dwFlags;
  22. public string pszIconFile;
  23. public uint cchIconFile;
  24. public int iIconIndex;
  25. public string pszLogo;
  26. public uint cchLogo;
  27. }
  28.  
  29. public const uint FCSM_ICONFILE = 0x00000010;
  30. public const uint FCS_FORCEWRITE = 0x00000002;
  31. public const uint FCS_READ = 0x00000001;
  32.  
  33. public static void SetFolderIcon(string folderPath, string iconFileName) {
  34. SHFOLDERCUSTOMSETTINGS settings = new SHFOLDERCUSTOMSETTINGS();
  35. settings.dwSize = Marshal.SizeOf(settings);
  36. settings.dwMask = FCSM_ICONFILE;
  37. settings.pszIconFile = iconFileName;
  38. settings.cchIconFile = (uint)iconFileName.Length;
  39.  
  40. int result = SHGetSetFolderCustomSettings(ref settings, folderPath, (int)(FCS_READ | FCS_FORCEWRITE));
  41. }
  42. }
  43. "@
  44.  
  45. # Function to set folder icon and create or update Desktop.ini
  46. function Set-CustomFolderIcons {
  47. # Get all subfolders
  48. $subfolders = Get-ChildItem -Path $scriptDirectory -Recurse -Directory
  49.  
  50. $customizedFoldersCount = 0
  51. $nonConformingFolders = @() # Array to store non-conforming folder names
  52. $nonConformingFoldersFixedCount = 0 # Count of non-conforming folders fixed
  53. $alreadyCustomizedFoldersCount = 0 # Count of folders already customized
  54.  
  55. # Counter for progress indication
  56. $foldersProcessed = 0
  57. $totalFoldersWithIcons = 0
  58.  
  59. # Initialize start time
  60. $start = Get-Date
  61.  
  62. foreach ($folder in $subfolders) {
  63. try {
  64. $iconFiles = Get-ChildItem -Path $folder.FullName -Filter "*.ico" -Force
  65. if ($iconFiles.Count -gt 0) {
  66. $totalFoldersWithIcons++
  67. $folderIndex = $totalFoldersWithIcons
  68.  
  69. $folderName = $folder.Name
  70. $truncatedFolderName = $folderName.Substring(0, [Math]::Min($folderName.Length, 100))
  71. if ($folderName.Length -gt 90) {
  72. $truncatedFolderName += "..."
  73. }
  74.  
  75. Write-Host "Processing folder ${folderIndex}/${totalFoldersWithIcons}" -ForegroundColor Cyan
  76. Write-Host " Folder Name: " -ForegroundColor Green -NoNewline
  77. Write-Host "${truncatedFolderName}" -ForegroundColor White
  78.  
  79. # Select a random icon from the folder
  80. $selectedIcon = Get-Random -InputObject $iconFiles
  81. $iconFileName = $selectedIcon.Name
  82.  
  83. if ($iconFileName -eq "icon.ico") {
  84. Write-Host " Selected Icon: " -ForegroundColor Green -NoNewline
  85. Write-Host "${iconFileName} " -ForegroundColor White -NoNewline
  86. Write-Host "(default icon 'icon.ico' found)" -ForegroundColor Yellow
  87. } else {
  88. Write-Host " Selected Icon: " -ForegroundColor Green -NoNewline
  89. Write-Host "${iconFileName}" -ForegroundColor White
  90. }
  91.  
  92. Write-Host "" # Add a blank line after Selected Icon
  93.  
  94. $folderPath = $folder.FullName
  95.  
  96. # Check if Desktop.ini file exists
  97. $desktopIniPath = Join-Path $folderPath "Desktop.ini"
  98. if (!(Test-Path $desktopIniPath -PathType Leaf)) {
  99. # Set the folder icon using the custom function
  100. [FolderIconSetter]::SetFolderIcon($folderPath, $iconFileName)
  101.  
  102. # Create Desktop.ini file
  103. @"
  104. [.ShellClassInfo]
  105. IconResource=$iconFileName,0
  106. "@ | Out-File -Encoding ascii -FilePath $desktopIniPath -Force
  107.  
  108. # Set attributes for Desktop.ini file
  109. Set-ItemProperty -Path $desktopIniPath -Name Attributes -Value "ReadOnly, Hidden"
  110.  
  111. $customizedFoldersCount++
  112. } else {
  113. $alreadyCustomizedFoldersCount++
  114. # Read existing Desktop.ini content
  115. $desktopIniContent = Get-Content -Path $desktopIniPath -Raw
  116.  
  117. # Check if Desktop.ini content matches expected format
  118. if ($desktopIniContent -notmatch "\[.ShellClassInfo\]`r?`nIconResource=.+") {
  119. # Add folder name to non-conforming folders list
  120. $nonConformingFolders += $folderName
  121.  
  122. # Check if existing Desktop.ini contains localizedresourcename
  123. $localizedResourceName = $null
  124. if ($desktopIniContent -match "(?m)localizedresourcename=.+") {
  125. $localizedResourceName = $matches[0]
  126. }
  127.  
  128. # Delete existing Desktop.ini
  129. Remove-Item -Path $desktopIniPath -Force
  130.  
  131. # Create new Desktop.ini file
  132. @"
  133. [.ShellClassInfo]
  134. IconResource=$iconFileName,0
  135. $localizedResourceName
  136. [ViewState]
  137. FolderType=Videos
  138. "@ | Out-File -Encoding ascii -FilePath $desktopIniPath -Force
  139.  
  140. # Set attributes for Desktop.ini file
  141. Set-ItemProperty -Path $desktopIniPath -Name Attributes -Value "ReadOnly, Hidden"
  142.  
  143. # Increment count of non-conforming folders fixed
  144. $nonConformingFoldersFixedCount++
  145. }
  146. }
  147. }
  148. } catch {
  149. # Ignore errors related to file access for Desktop.ini
  150. if ($_.Exception.Message -match "Access to the path.*Desktop.ini.*is denied.") {
  151. continue
  152. } else {
  153. Write-Output "Error setting folder icon for: $folderName"
  154. Write-Output $_.Exception.Message
  155. }
  156. }
  157. }
  158.  
  159. # Calculate new folders customized
  160. $newFoldersCount = $totalFoldersWithIcons - $alreadyCustomizedFoldersCount
  161.  
  162. # Output summary information
  163. Write-Host "Finished customizing folders!" -ForegroundColor Green
  164. $end = Get-Date
  165. $timeTaken = $end - $start
  166. $formattedTime = ""
  167. if ($timeTaken.TotalSeconds -ge 60) {
  168. $minutes = [Math]::Floor($timeTaken.TotalMinutes)
  169. $seconds = $timeTaken.TotalSeconds % 60
  170. if ($minutes -lt 2) {
  171. $formattedTime = "$minutes minute"
  172. } else {
  173. $formattedTime = "$minutes minutes"
  174. }
  175. if ($seconds -ne 0) {
  176. $formattedTime += " and " + "{0:N2}" -f $seconds + " seconds"
  177. }
  178. } else {
  179. $formattedTime = "{0:N2}" -f $timeTaken.TotalSeconds + " seconds"
  180. }
  181. Write-Host "Time taken: $formattedTime" -ForegroundColor Green
  182. Write-Host "Folders processed: $totalFoldersWithIcons" -ForegroundColor Green
  183. Write-Host "New folders customized: $newFoldersCount" -ForegroundColor Green
  184. Write-Host "Folders already customized: $alreadyCustomizedFoldersCount" -ForegroundColor Green
  185. Write-Host "Non-conforming folders fixed: $nonConformingFoldersFixedCount" -ForegroundColor Green
  186. if ($nonConformingFolders.Count -gt 0) {
  187. Write-Host "" # Blank line here
  188. Write-Host "Non-conforming folders:" -ForegroundColor Yellow
  189. $nonConformingFolders | ForEach-Object {
  190. $truncatedName = $_.Substring(0, [Math]::Min($_.Length, 100))
  191. if ($_.Length -gt 100) {
  192. $truncatedName += "..."
  193. }
  194. Write-Host $truncatedName -ForegroundColor Yellow
  195. }
  196. }
  197. }
  198.  
  199. # Get the directory containing the script
  200. $scriptDirectory = Split-Path -Parent $PSCommandPath
  201.  
  202. # Call the function to set custom folder icons and create/update Desktop.ini files
  203. Set-CustomFolderIcons
  204.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement