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

#1
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.
 


Josephur

Windows Forum Admin
Staff member
Premium Supporter
#2
@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.
 


Josephur

Windows Forum Admin
Staff member
Premium Supporter
#3
Oh and as far as copying it depending if its a file/folder, look into xcopy or robocopy and its options.
 


#4
@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.
Thanks
 


toan

New Member
#5
Oh and as far as copying it depending if its a file/folder, look into xcopy or robocopy and its options.
I agree with the comments you have made
 


This website is not affiliated, owned, or endorsed by Microsoft Corporation. It is a member of the Microsoft Partner Program.