How to Rename fileName using Win32_ShortcutFile - WMI schema method rename and Jacob

125 Views Asked by At

Being a Jacob newbie, I am not able to use rename method of Win32_ShortcutFile schema for changing filename. I want to use it done via schema method calling only. Please suggest how I should proceed.

My code looks like this:

    public static void main(String[] args) {
    ComThread.InitMTA();
    ActiveXComponent os = null;

    try {
        ActiveXComponent wmi = new ActiveXComponent("winmgmts:\\\\.");
                    String newName = "jacobtest";
        Variant instances = wmi.invoke("InstancesOf", "Win32_ShortcutFile");
        Enumeration<Variant> en = new EnumVariant(instances.getDispatch());

        while (en.hasMoreElements()) {
            ActiveXComponent bb = new ActiveXComponent(en.nextElement()
                    .getDispatch());
            String file = bb.getPropertyAsString("FileName");
            System.out.println(file);

            if (file.equals("filename")) {
                os = new ActiveXComponent(en.nextElement                                        ().getDispatch());
                os.invoke("Rename", newName);
            }

        }
    }

    finally {
        ComThread.Release();
    }

}
1

There are 1 best solutions below

0
On

Finally I got it.... :)

Minor mistake while renaming the file, it should be as shown below:

  String newName = "D:\\jacobtest.docx";
  if (file.equals("filename")) {
            os = new ActiveXComponent((en.nextElement).getDispatch());
            os.invoke("Rename", newName);
        }