Reputation: 1645
I want to create desktop.ini files within folders, that reference a .jpg in a subfolder in order to give the parent folder a custom image. This works doing the process manually (right click folder > properties > customize > choose FILE).
Assume a folder structure like so:
C:\Test
---> Folder1
---> Fullsize
---> image.jpg
---> Folder2
---> Fullsize
---> image.jpg
Before the manual process, the parent folder ('Folder1') has attributes of: 'Directory'
.
After the manual process, the folder has attributes of 'ReadOnly, Directory'
. The 'Fullsize' subfolder retains the original attribute of 'Directory'
.
The desktop.ini file is created with content like so:
[ViewState]
Mode=
Vid=
FolderType=Generic
Logo=C:\Test\Folder1\Fullsize\image.jpg
Additionally, encoding is UTF-8, and the file properties are set to HSA ('Hidden, System, Archive'
).
Immediately after clicking ok, the folder shows a preview of the image.
Turning this into the code equivalent for another folder, we have:
$folder2 = 'C:\Test\Folder2'
$folder2Image = "$folder2\Fullsize\image.jpg"
$ini = @"
[ViewState]
Mode=
Vid=
FolderType=Generic
Logo=$Folder2Image
"@
Set-ItemProperty $Folder2 -Name Attributes -Value 'ReadOnly'
# Out-File creates UTF-8 with BOM so use [System.IO.File]
[System.IO.File]::WriteAllLines("$Folder2\desktop.ini", $ini)
$inifile = Get-Item "$Folder2\desktop.ini"
$inifile.Attributes = 'Archive, System, Hidden'
Despite this, folder2 will not show an image preview.
Checking the attributes of Folder1 and Folder2 (Get-Item $path | Select Attributes), and the desktop.ini files them show them to be identical.
The encoding and actual content of desktop.ini is identical except for the path to the image. The path to the image is correct. Files contain CRLF at the end of each line. I have even ensured the path to the image is in the correct case (not that it should matter).
Thoroughly stumped. What else could the manual process be doing that I'm missing?
Windows 10: Version 1607 (OS Build 14986.1001)
Upvotes: 3
Views: 4486
Reputation: 5777
The real trick is to perform this operation on the folder:
(Get-Item $Folder -Force).Attributes = 'ReadOnly, Directory'
Antoher cool tip when working with hidden files: In any explorer window, to toggle hidden files by pressing these keys consecutively: Alt, A, H, H
Upvotes: 0
Reputation: 73736
Desktop.ini must be updated using a Shell API method in order for the Shell/Explorer to be notified:
function Set-FolderImage($folder, $imagePath) {
# make a temporary folder with desktop.ini
$tmpDir = (Join-Path $env:TEMP ([IO.Path]::GetRandomFileName()))
mkdir $tmpDir -force >$null
$tmp = "$tmpDir\desktop.ini"
@"
[ViewState]
FolderType=Generic
Logo=$imagePath
"@ >$tmp
(Get-Item -LiteralPath $tmp).Attributes = 'Archive, System, Hidden'
# use a Shell method to move that file into the destination
$shell = New-Object -com Shell.Application
$shell.NameSpace($folder).MoveHere($tmp, 0x0004 + 0x0010 + 0x0400)
# FOF_SILENT 0x0004 don't display progress UI
# FOF_NOCONFIRMATION 0x0010 don't display confirmation UI, assume "yes"
# FOF_NOERRORUI 0x0400 don't put up error UI
del -LiteralPath $tmpDir -force
}
Usage:
Set-FolderImage 'R:\Temp' 'R:\33983.jpg'
Upvotes: 1