1
0 Comments

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.

Askify Moderator Edited question April 29, 2023