Windows 7 Script (or other mechanism) to copy shortcut target to a new folder

Discussion in 'Programming and Scripting' started by goshenBleeping, May 23, 2013.

  1. goshenBleeping

    goshenBleeping Active Member

    Joined:
    May 23, 2013
    Messages:
    9
    Likes Received:
    0
    I am looking for a Windows 7 script (perhaps in vbs) or another mechanism that will copy the target of a shortcut to a new folder.


    If the shortcut points to a single file, copy the file to the new folder
    If the shortcut points to a folder, copy that folder to the new folder
    If the shortcut points to a folder & the folder has children, copy that folder & all of its children to the new folder


    Thank you.
     
  2. Josephur

    Josephur Windows Forum Admin
    Staff Member Premium Supporter

    Joined:
    Aug 3, 2010
    Messages:
    1,019
    Likes Received:
    125
    @echo OFF
    SET Shortcut=%~1
    ECHO SET WshShell = WScript.CreateObject("WScript.Shell")>DecodeShortCut.vbs
    ECHO SET Lnk = WshShell.CreateShortcut(WScript.Arguments.Unnamed(0))>>DecodeShortCut.vbs
    ECHO wscript.Echo Lnk.TargetPath>>DecodeShortCut.vbs
    SET vbscript=cscript //nologo DecodeShortCut.vbs
    for /f "delims=" %%T in ( ' %vbscript% "%Shortcut%" ' ) do set target=%%T
    DEL DecodeShortCut.vbs
    ECHO Shortcut: %shortcut%
    ECHO Target: %target%

    I forget where I got this from, but it has to date been the easiest way to do this from command prompt, if you don't want it to be a batch file and prefer vbs, I'm sure you can parse the vbs which is just piped to a temp file and deleted afterwards to get the desired result.

    Works fine on shortcuts (.lnk files) to files as well as folders.
     
    shopchvvn likes this.
  3. Josephur

    Josephur Windows Forum Admin
    Staff Member Premium Supporter

    Joined:
    Aug 3, 2010
    Messages:
    1,019
    Likes Received:
    125
    Oh and as far as copying it depending if its a file/folder, look into xcopy or robocopy and its options.
     
  4. shopchvvn

    shopchvvn New Member

    Joined:
    May 29, 2014
    Messages:
    1
    Likes Received:
    0
    Thanks
     
  5. toan

    toan New Member

    Joined:
    Jun 9, 2014
    Messages:
    1
    Likes Received:
    0
    I agree with the comments you have made
     

Share This Page

Loading...