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

Wondering if Perl would be a good solution for this. I want to find all instances of a shortcut named "abc.lnk" and rename it to "def.lnk", for example. I know that the shortcut will appear somewhere within the C:\Documents and Settings folder. Things I won't know: how many instances of the shortcut exist, and the exact path(s) to the shortcut. In other words, the shortcut could exist at C:\Documents and Settings\user1\startmenu\abc, or C:\Documents and Settings\user2\shortcuts\abc, and so on.
  • Comment on find instances of a file without knowing specific path

Replies are listed 'Best First'.
Re: find instances of a file without knowing specific path
by ikegami (Patriarch) on Aug 09, 2007 at 18:03 UTC

    Yup, see File::Find::Rule

    Update: Code:

    use File::Find::Rule qw( ); use File::Basename qw( dirname ); use File::Spec::Functions qw( catfile ); my $rule = File::Find::Rule ->file ->name('abc.lnk') # Wildcards allowed ->start('C:\\Documents and Settings'); while (defined(my $src = $rule->match()) { my $dst = catfile(dirname($match), 'def.lnk'); if (-e $dst) { warn("Unable to rename \"$src\": " . "Destination \"$dst\" already exists\n"); next; } if (!rename($src, $dst)) { warn("Unable to rename \"$src\": $!\n"); next; } print("Renamed \"$src\" to \"$dst\"\n") if $verbose; }
Re: find instances of a file without knowing specific path
by wind (Priest) on Aug 09, 2007 at 19:54 UTC
    Just use File::Find:
    use File::Find; my $dir = 'C:\Documents and Settings'; my $from = 'abc.lnk'; my $to = 'def.lnk'; find(sub { if ($_ eq $from) { print "$File::Find::name -> $to\n"; rename($_, $to); } }, $dir);
    - Miller