Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Changing Windows Shortcut Icons

by Albannach (Prior)
on Jul 08, 2004 at 21:46 UTC ( #372969=note: print w/ replies, xml ) Need Help??


in reply to Changing Windows Shortcut Icons

Have a look at Win32::Shortcut (part of libwin32) which you probably already have if you installed ActiveState. Something like:

my $shortcut = Win32::Shortcut->new($file); $shortcut->{IconLocation} = 'C:\Perl\bin\perl.exe'; $shortcut->{IconNumber} = 0; $shortcut->Save($file);
should do the trick. Now if you can tell me how to determine which files in a given list are in fact shortcuts (apart from testing the ShortcutPath using Win32::Shortcut) then I'd be interested to hear it!

--
I'd like to be able to assign to an luser


Comment on Re: Changing Windows Shortcut Icons
Download Code
Re^2: Changing Windows Shortcut Icons
by mdog (Pilgrim) on Jul 08, 2004 at 22:23 UTC
    Many, many thanks! This is exactly what I was looking for...I should have checked CPAN but I didn't think there would be anything specific for windows and shortcuts. Goes to show how much I know.
Re^2: Changing Windows Shortcut Icons
by bbfu (Curate) on Jul 08, 2004 at 22:59 UTC

    Now if you can tell me how to determine which files in a given list are in fact shortcuts (apart from testing the ShortcutPath using Win32::Shortcut) then I'd be interested to hear it!

    I could be missing something, but can't you tell simply from the '.lnk' extension?

    bbfu
    Black flowers blossom
    Fearless on my breath

      Thanks! It appears (through some experiments on several different versions of Windows - sorry for the delay) that having the extension 'lnk' is sufficient proof that a file is indeed a shortcut file. I had thought of that but assumed (foolishly it seems) that one could re-name a shortcut without destroying the shortcutting capability. It seems that this is not the case - you can't rename a shortcut to eliminate the '.lnk' (short of editing the directory I imagine).

      However: if you rename any file to add '.lnk', the file seems to be permanently changed (since you can't remove the '.lnk') to a shortcut, though it's content is retained and can still be manipulated. In this case, the shortcut properties are inaccessible which makes me think that it isn't really a shortcut after all. Now, if you use Win32::Shortcut's Save() on such a file that isn't really a shortcut, its contents are then destroyed and replaced with the guts of a standard .lnk file and it becomes a real shortcut. Just to be weirder, I tried Save() on a normal file without '.lnk', and it too had it's content replaced, but the Explorer didn't recognise it as a shortcut as the extension was not 'lnk'. I think I'll stop now! It would be better if it were simply impossible to add '.lnk' to an existing normal file.

      --
      I'd like to be able to assign to an luser

        Yeah, unlike *nix, Windows relies pretty heavily on file extensions; a hold-over from it's DOS days. And since there was no capacity for symlink-style referencing in the old FAT32 system, MS adopted the .lnk "special" extension. Personally, I think they should've added real symlinking when they came up with, or switched to, NTFS. But I guess "Shortcuts" worked well enough for them.

        Windows treats certain file extensions (and file names) specially. Obviously .lnk is one of them, but CLSID's-as-file-extensions are another special case. (That's how the Recycle Bin, My Computer, etc, icons are handled on the desktop. Well, not really, but it does allow you to add those special "icons" to any folder, not just the desktop.)

        Special file extensions are never shown in Windows Explorer, even if you have the "Hide known extensions" option disabled. Also, when renaming a file with a special extension, Explorer doesn't let you change the extension. However, you can still see and change the extension via the command line, since it doesn't treat the extensions specially. Adding a .lnk extension to a non-shortcut file doesn't actually change the file data in any way, of course, but it does change how Explorer displays and manipulates the file. Similarly, removing the .lnk extension from an actual shortcut will cause Windows to no longer recognize it as a shortcut, but it still contains the binary data that comprises a "Windows Shortcut."

        In short, Explorer (mostly) only cares whether the file has a .lnk extension or not, while Windows only cares whether the file can be successfully read by the LoadShortcutFile() (or whatever it's called) system call.

        bbfu
        Black flowers blossom
        Fearless on my breath

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://372969]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2014-12-27 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls