Automatically replace the folder icon with the icon of the exe-file inside the folder

124 Views Asked by At

I wrote a batch file which looks through all the subdirectories of a directory, then copies the basename of the .exe file from each subdirectory, creates a desktop.ini file inside that subdirectory, and inserts the relative filename into it, (as the IconResource).

The problem is that the desktop.ini is created correctly, but only in one subdirectory. I need it to be created in all subdirectories of the initial directory, which should be the parent of the running batch file.

@echo off

for /d %%D in (*) do (
  set "exefile="
  for /f "delims=" %%F in ('dir /b /a-d "%%D\*.exe"') do (
    set "exefile=%%F"
    set "foldpath=%%D"
    goto :next
  )
  :next
  if defined exefile (
    echo [.ShellClassInfo]>>"%foldpath%\desktop.ini"
    echo ConfirmFileOp=0 >>"%foldpath%\desktop.ini"
    echo IconResource=.\%exefile%>>"%foldpath%\desktop.ini"
    echo IconIndex=0 >>"%foldpath%\desktop.ini"
    attrib +s +h "%foldpath%\desktop.ini"
  )
)

This is the entry in desktop.ini:

[.ShellClassInfo]
ConfirmFileOp=0 
IconResource=.\AltTabTer.exe
IconIndex=0 

example.

1

There are 1 best solutions below

0
Burav On
@echo off
for /D %%I in (*) do set "exefile=" & for %%J in ("%%I\*.exe") do if not defined exefile (
    set "exefile=1"
    (echo [.ShellClassInfo]
     echo ConfirmFileOp=0
     echo IconResource=.\%%~nxJ
     echo IconIndex=0
    ) >"%%I\desktop.ini"
    %SystemRoot%\System32\attrib.exe +s +h "%%I\desktop.ini"
)

How it works

Executing a batch file