Need to change icons on 400 + shortcuts

Discussion in 'Perl Misc' started by cjohnsonuk, Sep 7, 2007.

  1. cjohnsonuk

    cjohnsonuk Guest

    We have menus with 400 + shortcuts on them that are on a network
    drive. We use folder redirection in a group policy in the AD to get
    all PCs to look here for their menus. We are looking for a better way
    as there is a 10+ second delay after clicking start|programs before
    anything displays) but until then I need to do two things

    1) check that the path to the icons file is to a valid file
    2) change the path to a local .ico file with all the icons in it.(and
    eventually set up the menus to be copied locally too) (Can't use
    mandatory profiles as users require cached details to log in to
    laptops at home.)

    I've used the following code that I've copied /combined off the net to
    walk the program files directory tree and return the icon for
    each .lnk file it finds.

    It has two problems

    1) Not all folders are recognised as folders. There doesn't seem to
    be a pattern here. I can't see why some are listed as DIRs and some
    as FILEs. Those recognised as files obviously don't get walked.
    2) I think the read_shortcut subroutine requires an absolute path but
    my current working directoy (&cwd) only appears to return the parent
    dir.

    I'm running v5.8.8 built for MSWin32-x86-multi-thread on WIndows XP
    SP2
    The program files are on a windows share on a 2k3 server.

    Any ideas appreciated.

    Output
    ======================
    DIR>> Business & Economics
    LNK>> Business Plan.lnk
    path : Y:/StartMenu/Programs/Business & Economics/Business Plan.lnk
    Icon : C:\WINDOWS\explorer.exe,10
    LNK>> Economics Today Volumes 7 8 and 9.lnk
    path : Y:/StartMenu/Programs/Economics Today Volumes 7 8 and 9.lnk
    Icon : ,0
    LNK>> Go Chancellor!!!.lnk
    path : Y:/StartMenu/Programs/Go Chancellor!!!.lnk
    Icon : ,0
    LNK>> Nuffield BP.lnk
    path : Y:/StartMenu/Programs/Nuffield BP.lnk
    Icon : ,0
    FILE CorelDRAW Graphics Suite 12
    ======================

    ======================
    Code
    ======================
    #!/usr/bin/perl -s
    use Win32::OLE;
    use Cwd; # module for finding the current working directory

    my $wsh = new Win32::OLE 'WScript.Shell';

    sub read_shortcut {
    my $lnk_path = shift; # path of existing .lnk file

    my $shcut = $wsh->CreateShortcut($lnk_path) or die;
    # $shcut->TargetPath
    $shcut->IconLocation
    }

    # This subroutine takes the name of a directory and recursively scans
    # down the filesystem from that point looking for files named "core"
    sub ScanDirectory{
    my ($workdir) = shift;

    my ($startdir) = &cwd; # keep track of where we began

    chdir($workdir) or die "Unable to enter dir $workdir:$!\n";
    opendir(DIR, ".") or die "Unable to open $workdir:$!\n";
    my @names = readdir(DIR) or die "Unable to read $workdir:$!\n";
    closedir(DIR);

    foreach my $name (@names){
    next if ($name eq ".");
    next if ($name eq "..");

    if (-d $name){ # is this a directory?
    print "DIR>> ".$name."\n";
    # $name =~ s/ /\\ /;
    # print ">>DIR ".$name."\n";

    &ScanDirectory($name);
    next;
    }
    if ($name =~ m/.lnk/) { # does this filename
    have .lnk in it
    print "LNK>> ".$name."\n";
    $mypath=&cwd."/".$name;
    print "path : ".$mypath."\n";
    print " Icon : ".read_shortcut("$mypath")."\n";
    }
    else {
    print " FILE ".$name."\n";
    }
    chdir($startdir) or
    die "Unable to change to dir $startdir:$!\n";
    }
    }

    &ScanDirectory("Y:\\StartMenu");


    ======================
     
    cjohnsonuk, Sep 7, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan C Douglas
    Replies:
    5
    Views:
    419
    Dan C Douglas
    Jul 22, 2003
  2. Mark Parnell
    Replies:
    6
    Views:
    8,740
    Quasimido CSS
    Oct 6, 2005
  3. Richie Williams
    Replies:
    5
    Views:
    1,556
    Richie Williams
    Oct 25, 2007
  4. bahnfire
    Replies:
    2
    Views:
    383
    bahnfire
    Oct 31, 2008
  5. Diego Bernardes

    Change emblem from folders/icons from ruby

    Diego Bernardes, Mar 14, 2010, in forum: Ruby
    Replies:
    1
    Views:
    92
    Diego Bernardes
    Mar 14, 2010
Loading...

Share This Page