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

Hello All,

I am trying to write a script that would parse thru a windows directory supplied and return the object type encountered (File, Directory, Link, etc), size, atime, ctime, and mtime.

I located some code on the internet that kinda works, but when it gets to a windows link/shortcut (.lnk), the script fails with:

Use of uninitialized value in bitwise and (&) at C:\temp\showmodes.pl line 5.

Can't stat() 'C:\Users\admin\Favorites\MSN Websites\MSN Autos'

Any thoughts on how to parse thru all object type and it return the appropriate information?

#!/usr/bin/perl use strict; use warnings; my $name = shift or die "Usage: $0 file|directory\n"; my $mode = ( ( lstat($name) )[2] ) & 07777 or die "Can't stat() '$name +'\n"; printf "%s %04o %s\n", ( -f _ ) ? 'f' : ( -l _ ) ? 'l' : ( -d _ ) ? 'd' : ( -b _ ) ? 'b' : ( -c _ ) ? 'c' : ( -p _ ) ? 'p' : ( -S _ ) ? 's' : '?', $mode, $name; 1;

Replies are listed 'Best First'.
Re: parsing the Windows directories
by frozenwithjoy (Priest) on Aug 19, 2014 at 20:28 UTC

    I think you might need to use lstat instead of stat.

    At the bottom of the stat docs, it says:

    To get status info for a symbolic link instead of the target file behind the link, use the lstat function.

    And in lstat:

    Does the same thing as the stat function (including setting the special _ filehandle) but stats a symbolic link instead of the file the symbolic link points to. If symbolic links are unimplemented on your system, a normal stat is done.
Re: parsing the Windows directories
by wilmer_t (Novice) on Aug 19, 2014 at 22:16 UTC
    As a general advice, do not be afraid of use perl standard modules distributed and installed on your system. At times you may have non-standard additions on CPAN that will take you all the way, without reinventing the wheel :)

    In this case, look into File:: ... modules with extended Windows support. File::Find might even do what you want, with platform support for windows.

    As for the problem at hand, windows links; they are not really links according to the POSIX (*nix if you like) way - they are "regular" files containing paths to the original file element.
    Sorry though, but I have either the time nor environment to go look for further details on supportive modules for your specific problem :)

      As for the problem at hand, windows links; they are not really links according to the POSIX (*nix if you like) way - they are "regular" files containing paths to the original file element.

      As best I can figure, .lnk files are only treated as links by "Windows Explorer" (and maybe Internet Explorer, not sure). Applications that try to open a .lnk file will end up with a file handle to the contents of the .lnk file, not the file it points to. Therefore, it appears that applications have to be written to recognize .lnk files and handle the redirection themselves (probably there's a library to do this, but I've never "met" it).

      FYI, Windows Vista and newer do implement actual symbolic links - however, the command that creates symbolic links is very obscure and seems to require administrative privilege to use.

Re: parsing the Windows directories
by dasgar (Priest) on Aug 20, 2014 at 17:03 UTC

    Building off comments from others, here's something that I came up that I think does what you're wanting.

    Below is a sample output from the code above. There are two Windows shortcuts to cmd.exe included in the output, which did not cause any problems. It kind of looks like you were trying to parse through the folder that holds the "Favorites" from Internet Explorer. I tested the script above against the Favorites folder on my system and I didn't encounter any issues.