none
Script to Find and replace .lnk shortcut

    Question

  • We have users with an old shortcut on their desktops. I'd like to create a script to determine the users who do have the old link. Once I know this, I'd like to replace the .lnk shortcut file with the correct one. Can anyone help me with a script for this?

    A batch file or vbscript would be nice.

    Thanks,

    Scott

    Tuesday, September 21, 2010 6:12 PM

Answers

  • I found a powershell diddy on the net which looks for a string in the path and changes it to something else...

    $psshell = new-object -com wscript.shell
    gci $home\Desktop\TTest -filter *.lnk -recurse | foreach {
     
     $lnk = $psshell.CreateShortcut($_.fullname)
     $oldpath = $lnk.TargetPath

      if($oldpath -match "some string here"){
       $newtarget = $oldpath.replace("Q:\Server1\Folder\programy.exe",`
       "Q:\Some Other Server\Folder\programy.exe")

       $lnk.TargetPath = $newtarget
       $lnk.Save()
     }
    }

    • Marked as answer by scottyp55 Wednesday, September 22, 2010 2:00 PM
    Tuesday, September 21, 2010 8:51 PM
  • You could do this by simplifying the code suggested by arectech:

    xcopy  /d /y "\\Server1\shortcuts\New Short*" "%userprofile%\desktop\"

    The /d switch forces a file date comparison which has the effect you desire.

    • Marked as answer by scottyp55 Wednesday, September 22, 2010 2:00 PM
    Tuesday, September 21, 2010 9:10 PM

All replies

  • Something like this

    IF EXIST "%UserProfile%\Desktop\Old Short*" (del /s /q "%UserProfile%\Desktop\Old Short*")
    copy "\\Server1\shortcuts\New Short*" "%userprofile%\desktop\"

    Tuesday, September 21, 2010 6:43 PM
  • Thank you! This is great.

    Unfortunately, the old link and new link have the same name, but they point to a different location. Is there a way to determine the target location of the link and if it's incorrect, then delete it?

    Thanks a lot,
    Scott

    Tuesday, September 21, 2010 6:51 PM
  • I found a powershell diddy on the net which looks for a string in the path and changes it to something else...

    $psshell = new-object -com wscript.shell
    gci $home\Desktop\TTest -filter *.lnk -recurse | foreach {
     
     $lnk = $psshell.CreateShortcut($_.fullname)
     $oldpath = $lnk.TargetPath

      if($oldpath -match "some string here"){
       $newtarget = $oldpath.replace("Q:\Server1\Folder\programy.exe",`
       "Q:\Some Other Server\Folder\programy.exe")

       $lnk.TargetPath = $newtarget
       $lnk.Save()
     }
    }

    • Marked as answer by scottyp55 Wednesday, September 22, 2010 2:00 PM
    Tuesday, September 21, 2010 8:51 PM
  • You could do this by simplifying the code suggested by arectech:

    xcopy  /d /y "\\Server1\shortcuts\New Short*" "%userprofile%\desktop\"

    The /d switch forces a file date comparison which has the effect you desire.

    • Marked as answer by scottyp55 Wednesday, September 22, 2010 2:00 PM
    Tuesday, September 21, 2010 9:10 PM
  • Thanks guys. These will work for me.
    Wednesday, September 22, 2010 2:00 PM