atyrrell has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I currently have a program that will search through a given directory and will collect all the sub-directories (folders) and their timestamps. I use this to see if any folders have been added/changed/deleted. It works beautifly, except it doesn't work for a folder that is actually a shortcut to another folder.

I was looking at Detecting if a folder is a symbolic link and thought maybe the -l (symbolic link) check might work for this, but it doesn't. I want to do some kind of check to see if it is a shortcut or not.

Another idea I had to solve this is to somehow identify the type of file it is, but I am having a hard time finding how to do this as well.

This is what my code looks like right now.

if (-d $directory) { #continue processing } else { print "$directory does not exist"; }
With this code, the shortcut folders say that the directory does not exist. I want to make another section so my code does something like this:
if (-d $directory) { #continue processing } elsif (SOMETHING) #if it is a shortcut { print "$directory is a shortcut. Will not process."; } else { print "$directory does not exist"; }
Any ideas what that SOMETHING could be?

I am using Windows OS.

Replies are listed 'Best First'.
Re: Determining if folder is a shortcut
by psini (Deacon) on Jun 25, 2008 at 20:00 UTC

    Try and see this: Win32::Shortcut.

    I don't know if there is a method to tell if a file is a shortcut in windows (apart from looking for .lnk extensions), but with this module you should be able to follow the link, if it is a link.

    Remember that in windows links are regular files, not directories; so you should check for files to be a shortcut, not dirs.

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: Determining if folder is a shortcut
by ikegami (Patriarch) on Jun 25, 2008 at 20:40 UTC

    No folders are shortcuts. You're asking the wrong question. You should be asking how to determine if a shortcut (.lnk) references a folder.

    Others have suggested Win32::Shortcut. It is indeed the solution. Extract the target ($link->{'Path'}) and use -d.

Re: Determining if folder is a shortcut
by ww (Archbishop) on Jun 25, 2008 at 20:19 UTC
    ...it doesn't work for a folder that is actually a shortcut to another folder.

    TTBOMK, Windows shortcuts are not directories (they are files which get special handling from the OS). And, again TTBOMK, W32 does not do (support) symlinks.

    If this is correct (and probably, even if it is not), your search for appropriate content for your elseif clause is not apt to produce useful results and you'll do better to go directly from the if to the else.

    Alternately, if you've handrolled your method of walking your tree, File::Find may be helpful and you may wish to see (merely for example) kyle's reply in Glob vs File::Util vs File::Find or Count folder/files in directory... and you may wish to familiarize yourself with the use of the Monastery's "Search" and Super_Search capabilities.

Re: Determining if folder is a shortcut
by pc88mxer (Vicar) on Jun 25, 2008 at 19:55 UTC
    I'm not a Windows expert, but I did find cpan:://Win32::Shortcut. Perhaps you can use that to determine if a .lnk file is a shortcut to a directory or application.