-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFolderTags.psm1
More file actions
184 lines (169 loc) · 6.31 KB
/
FolderTags.psm1
File metadata and controls
184 lines (169 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<# .SYNOPSIS
Add-Tag function
.DESCRIPTION
Adds a tag to the specified directory
#>
function Add-Tag {
[CmdletBinding()]
param(
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[PSDefaultValue(Help = 'Current directory', Value = '.')]
[SupportsWildcards()]
[ValidateScript({ Test-Path $_ -PathType Container }, ErrorMessage = "Path {0} does not exist.")]
[String[]]$Path = '.',
[Parameter(
Mandatory = $true,
ValueFromPipelinebyPropertyName = $true)]
[String[]]$Tag
)
begin {}
process {
$Path | ForEach-Object {
$iniFilePath = Join-Path $_ 'desktop.ini'
# create ini if it does not exist
if (-Not (Test-Path $iniFilePath)) {
$iniFile = New-Item $iniFilePath -ItemType File
$iniFile.Attributes += "Hidden"
}
# read ini
$iniContent = Get-IniContent $iniFilePath | Initialize-DesktopIni
$null = $iniContent['{F29F85E0-4FF9-1068-AB91-08002B27B3D9}']['Prop5'] -match '31,(.*)'
$iniTag = ($Matches[1] -split '\s*;\s*' | Where-Object { $_ })
if ($iniTag) {
$Tag = $iniTag + $Tag
}
$iniContent['{F29F85E0-4FF9-1068-AB91-08002B27B3D9}']['Prop5'] = ($Tag | Select-Object -Unique | Join-String -Separator '; ' -OutputPrefix '31,')
Set-IniContent $iniFilePath $iniContent
}
}
end {}
}
<# .SYNOPSIS
Remove-Tag function
.DESCRIPTION
Removes a tag to the specified directory
#>
function Remove-Tag {
[CmdletBinding()]
param(
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[PSDefaultValue(Help = 'Current directory', Value = '.')]
[SupportsWildcards()]
[ValidateScript({ Test-Path $_ -PathType Container }, ErrorMessage = "Path {0} does not exist.")]
[String[]]$Path = '.',
[Parameter(
Mandatory = $true,
ValueFromPipelinebyPropertyName = $true)]
[String[]]$Tag
)
begin {}
process {
$Path | ForEach-Object {
$iniFilePath = Join-Path $_ 'desktop.ini'
# create ini if it does not exist
if (-Not (Test-Path $iniFilePath)) {
$iniFile = New-Item $iniFilePath -ItemType File
$iniFile.Attributes += "Hidden"
}
# read ini
$iniContent = Get-IniContent $iniFilePath | Initialize-DesktopIni
$null = $iniContent['{F29F85E0-4FF9-1068-AB91-08002B27B3D9}']['Prop5'] -match '31,(.*)'
$iniTag = $Matches[1] -split '\s*;\s*'
$iniTag = $iniTag | Select-Object -Unique | Where-Object { $_ -and $_ -notin $Tag }
if ($iniTag.count -gt 0) {
$iniContent['{F29F85E0-4FF9-1068-AB91-08002B27B3D9}']['Prop5'] = ( $iniTag | Join-String -Separator '; ' -OutputPrefix '31,')
}
else {
# no tags, remove property
$iniContent['{F29F85E0-4FF9-1068-AB91-08002B27B3D9}'].Remove('Prop5')
if ($iniContent['{F29F85E0-4FF9-1068-AB91-08002B27B3D9}'].count -eq 0) {
$iniContent.Remove('{F29F85E0-4FF9-1068-AB91-08002B27B3D9}')
}
}
# check if the file would either be empty or only contain [.ShellClassInfo]
if ($null -eq $iniContent -or ( `
$iniContent.count -eq 1 `
-and -not $null -eq $iniContent['.ShellClassInfo'] `
-and $iniContent['.ShellClassInfo'].count -eq 0)) {
# the ini file is empty, so remove it
Remove-Item $iniFilePath -Force
}
else {
Set-IniContent $iniFilePath $iniContent
}
}
}
end {}
}
<# .SYNOPSIS
.DESCRIPTION
Ensures the necessary entries exist in the desktop ini
#>
function Initialize-DesktopIni ([Parameter(ValueFromPipeline = $true)]$iniContent) {
# add section .ShellClassInfo if it does not exist
if ($null -eq $iniContent['.ShellClassInfo']) {
$iniContent['.ShellClassInfo'] = @{}
}
# add section {F29F85E0-4FF9-1068-AB91-08002B27B3D9} if it does not exist
if ($null -eq $iniContent['{F29F85E0-4FF9-1068-AB91-08002B27B3D9}']) {
$iniContent['{F29F85E0-4FF9-1068-AB91-08002B27B3D9}'] = @{}
}
# add key Prop5 containing the tags if it does not exist
if ($null -eq $iniContent['{F29F85E0-4FF9-1068-AB91-08002B27B3D9}']['Prop5']) {
$iniContent['{F29F85E0-4FF9-1068-AB91-08002B27B3D9}']['Prop5'] = '31,'
}
return $iniContent
}
<#
Code from
Use PowerShell to Work with Any INI File - by Doctor Scripto, August 20th, 2011
https://devblogs.microsoft.com/scripting/use-powershell-to-work-with-any-ini-file/
#>
function Get-IniContent ($filePath) {
$ini = @{}
switch -regex -file $FilePath {
“^\[(.+)\]” {
# Section
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
“^(;.*)$” {
# Comment
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = “Comment” + $CommentCount
$ini[$section][$name] = $value
}
“(.+?)\s*=(.*)” {
# Key
$name, $value = $matches[1..2]
$ini[$section][$name] = $value
}
}
return $ini
}
function Set-IniContent ($filePath, $iniContent) {
$fileContent = '[.ShellClassInfo]'
if ($iniContent['.ShellClassInfo']) {
foreach ($propertyHash in $iniContent['.ShellClassInfo'].GetEnumerator() ) {
$fileContent += "`r`n" + $propertyHash.Name + '=' + $propertyHash.Value
}
}
foreach ($sectionHash in $iniContent.GetEnumerator()) {
if ($sectionHash.Name -eq '.ShellClassInfo') {
continue;
}
$fileContent += "`r`n[" + $sectionHash.Name + ']'
foreach ($propertyHash in $sectionHash.Value.GetEnumerator() ) {
$fileContent += "`r`n" + $propertyHash.Name + '=' + $propertyHash.Value
}
}
Set-Content $filePath $fileContent
}