2

My goal is to change every folder's icon of my movie library to an icon I have for every folder in Windows using Java.

Every folder has a 256x256 icon in it named after the folder but with the appropriate extension. For example the folder called 5cm Per Second has the file 5cm Per Second.ico in it.

I figured I could do this by modifying the desktop.ini file in the folder. All the folders have that file in them because the icons inside each folder used to be the actual icon of the folder, but after I changed the path of my movie library the path of the icon in desktop.ini didn't get updated.

Every desktop.ini looks like this:

[.ShellClassInfo]

IconResource=F:\Anime\Movies\5cm Per Second\5cm Per Second.ico,0

Now the path of where the icon is at is this: E:\Movies\5cm Per Second\5cm Per Second.ico so I figured all I have to do is change the desktop.ini to this:

[.ShellClassInfo]

IconResource=E:\Movies\5cm Per Second\5cm Per Second.ico,0

That did not work at all then I figured I also should make Windows know that desktop.ini is a system file, I added this to my code but that did not work at all.

My code for that sample folder:

import java.io.File;
import java.io.IOException;
import org.ini4j.Wini;

public class ListFiles {

    public static void main(String[] args) throws IOException {

        // Directory path here
        String path = "E:\\Movies\\5cm Per Second";

        String fileName;
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                if ("ini".equals(getExtension(listOfFiles[i])))
                {
                    File theFile = listOfFiles[i];
                    Wini ini = new Wini(theFile);
                    String iconPath = theFile.getParent() + ".ico";
                    String field = iconPath + ",0";
                    ini.put(".ShellClassInfo", "IconResource", field);
                    Runtime.getRuntime().exec("attrib +H " + theFile.getAbsolutePath());
                }
            }
        }
    }

    public static String getExtension(File theFile) {
        String extension = null;
        String fileName = theFile.getName();
        int i = fileName.lastIndexOf('.');

        if (i > 0 && i < fileName.length() - 1) {
            extension = fileName.substring(i + 1).toLowerCase();
        }

        if (extension == null) {
            return "";
        }
        return extension;
    }
}

As you can see I edit the IconResource field to the one I need it to be using ini4j library and then I change the attributes of desktop.ini to Hidden and System.

It looks like this is not enough and I really don't know what else to do.

Aki K
  • 1,222
  • 1
  • 27
  • 49
  • were you able to make this work without the library? By manually editing the .ini file? – Jayan Oct 07 '12 at 02:47
  • Related to http://stackoverflow.com/questions/9330512/java-how-to-change-icon-of-folder – Jayan Oct 07 '12 at 02:47
  • @jayan yes I am able to manually write in the ini file using notepad++, as for the related question I had asked the same thing but now that I have made progress I am only missing one thing, the reason for why ini4j doesn't work – Aki K Oct 07 '12 at 13:15

1 Answers1

3

You are not calling ini.store(), which probably writes the change to disk.

Jayan
  • 18,003
  • 15
  • 89
  • 143