Zend certified PHP/Magento developer

desktop.ini folder icon – batch file – vba

The issue is not very complicated, it’s just a little complex so it’s hard to explain. I’ll do my best to be as clear as possible.

I downloaded a batch file that allows you to drag a folder onto it and it will set a new folder icon. It does this by creating a desktop.ini file and sets the necessary file and folder attributes.

This is the code in the batch file:

If [%1] == [] goto :eof  
ECHO [.ShellClassInfo] >%1desktop.in  
ECHO IconResource=J:PRESETSAUTOHOTKEY SCRIPTSVSAICONSGREENfolderico-green.ico,0 >>%1desktop.in  
move %1desktop.in %1desktop.ini  
attrib +S +H %1desktop.ini  
attrib +R %1  

Even though this works, I added a line at the end to refresh the Explorer Cache:

start "C:WindowsSystem32" ie4uinit.exe -show  

I wanted to run this batch file programmatically in VBA so I couldn’t use the drag-and-drop function of it. So first, I changed all the “%1” to “%~dp0” so that I can have VBA create a batch file in any folder and it would run using that folder’s path.

The VBA function checks if a client’s balance is >=0. If it is, the client folder will get a green icon. If he’s in debt, it gets a red folder icon.

The VBA function will create the .bat file as shown before in the client folder and run it. Then it will delete the .bat file.

Here’s 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] >%~dp0desktop.in"  

                    If TotalALL >= 0 Then  
                        Print #fnum, "ECHO IconResource=J:PRESETSAUTOHOTKEY SCRIPTSVSAICONSGREENfolderico-green.ico,0 >>%~dp0desktop.in"  
                    Else    
                        Print #fnum, "ECHO IconResource=J:PRESETSAUTOHOTKEY SCRIPTSVSAICONSREDfolderico-red.ico,0 >>%~dp0desktop.in"  
                    End If  

                    Print #fnum, "move %~dp0desktop.in %~dp0desktop.ini"  
                    Print #fnum, "attrib +S +H %~dp0desktop.ini"  
                    Print #fnum, "attrib +R %~dp0"  
                    Print #fnum, "start ""C:WindowsSystem32"" 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   

Here’s the problem:

If I manually copy the batch file to a client folder and manually run it there, it works fine. The desktop.ini file is created and after about 20 seconds, the folder icon changes.

But when the same file is created and run by the VBA function, the desktop.ini file gets created but the folder icon doesn’t change.

I hope my question was clear.