The matter at hand is not overly intricate, but it is somewhat convoluted, making it difficult to articulate. Nonetheless, I will endeavor to provide a lucid explanation.
I obtained a batch file that permits you to drag a folder onto it, resulting in the assignment of a new icon to that folder. This is accomplished by generating a desktop.ini file and configuring the requisite file and folder attributes.
The following is the code included in the batch file:
If [%1] ==  goto :eof ECHO [.ShellClassInfo] >%1\desktop.in ECHO IconResource=J:\PRESETS\AUTOHOTKEY SCRIPTS\VSA\ICONS\GREEN\folderico-green.ico,0 >>%1\desktop.in move %1\desktop.in %1\desktop.ini attrib +S +H %1\desktop.ini attrib +R %1
Although this approach is functional, I appended an additional command at the conclusion to update the Explorer Cache:
start "C:\Windows\System32" ie4uinit.exe -show
I aimed to execute this batch file programmatically using VBA, which necessitated the exclusion of the drag-and-drop feature. As a result, I replaced all instances of “%1” with “%~dp0” to enable the creation of a batch file in any folder via VBA, which would execute using that folder’s path.
The VBA function verifies if a client’s account balance is greater than or equal to zero. If it is, the corresponding client folder is assigned a green icon. If the client is in debt, the folder receives a red icon.
The VBA function then creates the .bat file within the client folder, runs it, and subsequently removes the file from the folder.
Below is the code for the VBA function:
Sub ChangeClientFolderIcon(ByVal ClientName As String, ByVal TotalALL As Currency) Dim substrings() As String Dim NewClientName As String substrings = Split(ClientName) NewClientName = substrings(2) & "_" & substrings(0) & "_" & substrings(1) Dim fso As New FileSystemObject Dim f As Folder, sf As Folder Set f = fso.GetFolder("M:\DIGITAL_ALBUMS\") For Each sf In f.SubFolders If sf.name = NewClientName Then Dim MyFile As Variant Dim fnum As Variant MyFile = sf & "\cmdcode.bat" fnum = FreeFile() Open MyFile For Output As #fnum If TotalALL >= 0 Then Print #fnum, "If [%~dp0] ==  goto :eof" Print #fnum, "ECHO [.ShellClassInfo] >%~dp0\desktop.in" If TotalALL >= 0 Then Print #fnum, "ECHO IconResource=J:\PRESETS\AUTOHOTKEY SCRIPTS\VSA\ICONS\GREEN\folderico-green.ico,0 >>%~dp0\desktop.in" Else Print #fnum, "ECHO IconResource=J:\PRESETS\AUTOHOTKEY SCRIPTS\VSA\ICONS\RED\folderico-red.ico,0 >>%~dp0\desktop.in" End If Print #fnum, "move %~dp0\desktop.in %~dp0\desktop.ini" Print #fnum, "attrib +S +H %~dp0\desktop.ini" Print #fnum, "attrib +R %~dp0" Print #fnum, "start ""C:\Windows\System32"" ie4uinit.exe -show" Close #fnum ' Run bat-file: Shell MyFile, vbNormalFocus ' optional, remove bat-file: 'Sleep for 5 seconds Application.Wait (Now + TimeValue("0:00:05")) Kill sf & "\cmdcode.bat" Exit For End If Next End Sub
This is the issue at hand:
When I manually transfer the batch file to a client folder and execute it there, it performs as expected. The desktop.ini file is produced, and approximately 20 seconds later, the folder icon is modified.
However, when the VBA function creates and runs the same file, the desktop.ini file is generated, but the folder icon remains unaltered.
I hope I have presented my inquiry in a comprehensible manner.
The Problem with VBA Batch File and Desktop.ini Folder Icon
The process of assigning a new icon to a folder in Windows is not complicated, but it does require a desktop.ini file and some configuration of the file and folder attributes. To automate this process, a batch file can be used to generate the desktop.ini file and set the necessary attributes. However, when trying to execute this batch file programmatically using VBA, there are some issues that can arise.
One issue that can occur is with the desktop.ini file itself. When the batch file generates the desktop.ini file, it may not be created with the correct encoding or formatting, which can result in errors or unexpected behavior when trying to assign the new icon to the folder.
Another issue that can arise is with the batch file itself. If the batch file is not configured correctly, it may not be able to assign the new icon to the folder, or it may assign the wrong icon or no icon at all.
To overcome these issues, it is important to understand the underlying processes involved in assigning a new icon to a folder in Windows, as well as the specific syntax and configuration required for the batch file and desktop.ini file.
In the following sections, we will explore these processes in more detail and provide guidance on how to create a VBA function that can successfully assign new icons to folders using a batch file.
Desktop.ini File Encoding and Formatting
The desktop.ini file is a hidden system file that contains configuration settings for a folder, including the folder icon. When the batch file generates the desktop.ini file, it must be created with the correct encoding and formatting to be recognized by Windows.
The desktop.ini file should be encoded in ANSI format, which is the default encoding for Windows text files. It should also be formatted using Windows line breaks (CRLF), rather than Unix line breaks (LF), which can cause issues with the file’s recognition.
To ensure that the desktop.ini file is encoded and formatted correctly, it is recommended to use a text editor that allows for the specification of encoding and line break settings, such as Notepad++ or Sublime Text.
Batch File Configuration
The batch file used to assign a new icon to a folder must be configured with the correct syntax and parameters to generate the desktop.ini file and set the necessary attributes.
The following is an example of a batch file that can be used to assign a new icon to a folder:
If [%1] ==  goto :eof ECHO [.ShellClassInfo] >%1desktop.ini ECHO IconResource=C:PathToIconIcon.ico,0 >>%1desktop.ini attrib +s +h %1 attrib -r %1
In this example, the batch file checks if a folder path has been passed as a parameter (%1). If a path has been passed, it generates the desktop.ini file with the necessary configuration settings, including the path to the new icon file (IconResource).
The batch file then sets the necessary attributes for the folder, including the system and hidden attributes (+s +h) and removes the read-only attribute (-r).
It is important to note that the path to the new icon file must be specified correctly in the batch file, including the correct file path and name, as well as the index of the icon within the file (0 for the first icon).
VBA Function to Assign Folder Icons
To automate the process of assigning new icons to folders using a batch file in VBA, a function can be created that takes in the folder path and the client’s account balance as parameters. The function then checks the account balance and assigns the appropriate icon to the folder.
The following is an example of a VBA function that can be used to assign folder icons based on account balance:
Sub ChangeClientFolderIcon(ByVal ClientName As String, ByVal TotalALL As Currency) Dim substrings() As String Dim NewClientName As String substrings = Split(ClientName) NewClientName = substrings(2) & "_" & substrings(0) & "_" & Format(TotalALL, "Currency") Dim batFile As String batFile = ClientName & "ChangeFolderIcon.bat" Open batFile For Output As #1 Print #1, "ECHO [.ShellClassInfo]>" & ClientName & "desktop.ini" Print #1, "ECHO IconResource=C:PathToIconIcon.ico,0>>" & ClientName & "desktop.ini" Print #1, "attrib +s +h " & ClientName Print #1, "attrib -r " & ClientName Close #1 Shell batFile, vbHide Kill batFile End Sub
In this example, the function takes in the client’s name and account balance as parameters. It then creates a new client name based on the account balance, using the Format function to format the currency value.
The function then creates a file path for the batch file using the client name, and opens the file for output. It writes the necessary configuration settings to the batch file, including the path to the new icon file.
The function then executes the batch file using the Shell function, hiding the command prompt window, and deletes the batch file once it has been executed.
Assigning new icons to folders in Windows using a batch file can be a useful way to automate the process of configuring folder attributes. However, when trying to execute the batch file programmatically using VBA, there are some issues that can arise with the desktop.ini file encoding and formatting, as well as the batch file configuration.
To overcome these issues, it is important to use a text editor that allows for the specification of encoding and line break settings, as well as to configure the batch file with the correct syntax and parameters. By creating a VBA function to automate the process of assigning new icons to folders based on account balance, it is possible to streamline the process and ensure consistent and accurate folder configurations.
The problem you are experiencing is likely due to the permissions on the folder where you are trying to create the batch file. When you run the batch file manually, you are doing so with your user account, which has the necessary permissions to create the file. However, when you run the VBA function, it is being run under the context of the current user, which may not have the necessary permissions to create a file in the specified folder.
One solution to this problem would be to ensure that the current user has the necessary permissions to create files in the specified folder. You can do this by modifying the folder’s permissions to allow the current user to have write access.
Alternatively, you could try running the VBA function as a different user with the necessary permissions. To do this, you can use the
RunAs command in your batch file to run the VBA function as a different user.
For example, you could modify your batch file to include the following line at the beginning:
runas /user:username "c:\path\to\excel.exe" "c:\path\to\workbook.xlsm" /x ChangeClientFolderIcon
This will prompt you to enter the password for the specified user, and then run the VBA function as that user.
I hope this helps! Let me know if you have any other questions.
I have found the solution.
The batch file being generated is configuring the desktop.ini file to be both System and Hidden (+S +H). Additionally, it sets the folder itself to be Read-Only (+R). To enable a custom .ini file to function correctly, the folder must also be designated as System.
Therefore, after making the following adjustment:
Print #fnum, "attrib +R %~dp0"
Print #fnum, "attrib +R +S %~dp0"
All works perfectly!