Windows .lnk
2023-01-01 perl .lnk windows Win32::Shortcut Path::ClassHere is quick perl script to check that target of .lnk
file exists.
use 5.16.3;
use Path::Class qw(dir file);
use Win32::Shortcut;
my $input_dir = shift or die "syntax: perl lnk.pl dir\n";
for my $file (grep { /\.lnk$/i } dir($input_dir)->children) {
my $lnk = Win32::Shortcut->new();
$lnk->Load($file);
my $target = $lnk->{Arguments} || $lnk->{Path};
say join ",", $file->basename, $target, -e $target ? "Target file exists" : "File missing";
}
The script accepts input directory, like this:
perl lnk.pl c:\Users\Me\Desktop
It scans the directory for .lnk
files and loads each of them with Win32::Shortcut. When loaded, the $lnk
contains following fields:
- Arguments
- Description
- File
- Hotkey
- IconLocation
- IconNumber
- Path
- ShortPath
- WorkingDirectory
Each .lnk
file is then printed with its target (either the argument or the basic path itself, if the argument is empty). Last column is check whether the target still exists, as sometimes it leads to a share or other area that might be removed. The output might look somehow like this (formatted into a table)
Lnk | Target | Exists? |
---|---|---|
Interface Descriptions.lnk | D:\Files\Resources\Interface Descriptions | Target file exists |
ProjectA.sln.lnk | D:\Files\ProjectA\trunk\ProjectA.sln | Target file exists |
notes.md.lnk | D:\Files\Resources\notes.md | Target file exists |
ProjectB.sln.lnk | D:\Files\ProjectB\trunk\ProjectB.sln | Target file exists |
Wireshark.lnk | C:\Program Files\Wireshark\Wireshark.exe | Target file exists |