#include-once

; #INDEX# =======================================================================================================================
; Title .........: _FolderSetIcon
; AutoIt Version.: 3.0.1++
; Language.......: English
; Description....: Function that changes a Folder Icon.
; ===============================================================================================================================

; #FUNCTION# ====================================================================================================================
; Name...........: _FolderSetIcon()
; Description ...: Set a new Icon for an given Folder
; Syntax.........: _FolderSetIcon($FolderPath, $IconDll[, $IconIndex])
; Parameters ....: $FolderPath                   - the complete Path to Folder (if the folder not exists it will be created)
;                   $IconDll                     - The Path to an dll-file containig the Icon (when not set, then the Standard Folder-Icon from Shell32.dll is set)
;                   $IconIndex                   - Index of the Icon in the dll-file, default is 1
; Return values .: Success:                      - 1
;                  Failure:                      - 0
;				   @error						 - 1
;				   @Extended					 - 1 = given .dll-/.ico-File don't exist
;												 - 2 = can't create Folder
;												 - 3 = can't set attributes of the Folder (+S)
;												 - 4 = can't write in ini-Section of Desktop.ini
;												 - 5 = can't set attributes of Desktop.ini (+SH)
; Author ........: Schnuffel at autoit.de
; Modified.......:
; Remarks .......: no remarks
; Related .......:
; Link ..........:
; Example .......: Yes
; Example 1......: _FolderSetIcon("C:\new", @SystemDir & "\Shell32.dll", 23)
;                    changes the icon for the given Folder, if the folder not exists it will be created
;
; Example 2......: _FolderSetIcon(@ScriptDir & "\" & CHR(160), @SystemDir & "\Shell32.dll", 50)
;                    creates an Folder with "NoName" and "NoIcon", Foldername = CHR(160)
;
; Example 3......: _FolderSetIcon(@ScriptDir&"\" & "My New Folder",@WindowsDir & "\SetupIcon.ico")
;                    changes the icon for the given Folder, if the folder not exists it will be created. The given Icon can be an .ico File
; ===============================================================================================================================

Func _FolderSetIcon($pFSI, $fFSI = "%SystemRoot%\system32\SHELL32.dll", $nIconFI = 0)
	Local $bFSIFC = False, $sFSIOD, $sFSIODHdl, $nFSIErr = 0, $pFSIDO = $pFSI & "\Desktop.ini"
    If $fFSI = "%SystemRoot%\system32\SHELL32.dll" Then $nIconFI = 4
	If StringRight($fFSI,3) = "ico" Then $nIconFI = 0
	Do
		If Not FileExists($fFSI) Then
			$nFSIErr = 1
			ExitLoop
		EndIf
		If Not FileExists($pFSI) Then
			DirCreate($pFSI)
			If @error Then
				$nFSIErr = 2
				ExitLoop
			EndIf
			$bFSIFC = True
		Else
			If FileExists($pFSIDO) Then $sFSIOD = FileRead($pFSIDO)
		EndIf
		FileSetAttrib($pFSI, "+S")
		If @error Then
			$nFSIErr = 3
			ExitLoop
		EndIf
		Switch @OSVersion
			Case "WIN_2008R2", "WIN_7", "WIN_2008", "WIN_VISTA"
				IniWriteSection($pFSIDO, ".ShellClassInfo", "LocalizedResourceName=" & $fFSI & "," & $nIconFI)
				If @error Then
					$nFSIErr = 4
					ExitLoop
				EndIf
			Case "WIN_2003", "WIN_XP", "WIN_XPe", "WIN_2000"
				IniWriteSection($pFSIDO, ".ShellClassInfo", "IconFile=" & $fFSI & @LF & "IconIndex=" & $nIconFI)
				If @error Then
					$nFSIErr = 4
					ExitLoop
				EndIf
		EndSwitch
		FileSetAttrib($pFSIDO, "+SH")
		If @error Then
			$nFSIErr = 5
			ExitLoop
		EndIf
		ExitLoop
	Until 1
	If $nFSIErr = 0 Then
		Return 1
	Else
		If $bFSIFC = True Then
			DirRemove($pFSI,1)
		ElseIf $sFSIOD <> "" Then
			$sFSIODHdl = FileOpen($pFSIDO,2)
			FileWrite($sFSIODHdl, $sFSIOD)
			FileClose($sFSIODHdl)
		EndIf
		Return SetError(1,$nFSIErr,0)
	EndIf
EndFunc