script to copy target files behind shortcuts

cosmo58

Distinguished
Sep 2, 2008
1
0
18,510
Hi all,

I'm trying to find a nice, simple way to copy the target file of a shortcut, not the shortcut itself. Ultimately I want to be able to copy a folder and all its contents, whether they be shortcuts or 'real' files, to a thumbdrive, etc.

I found a vbscript that does it (see http://groups.google.com/group/microsoft.public.windowsxp.general/msg/f2e8cac8e7870625), but this copies only the shortcut files (not real files in the same folder), and isn't too user-friendly: it needs to be edited for source and destination folders. I don't know anything about WSH, so can someone help with the following?:

1. Make it copy all files in the source folder, not just *.lnk files.
2. Make it ask the user for the source and destination folder. Preferably a nice navigable FileOpen box or whatever, but I suppose a text-only "Enter source path..." type deal would suffice.

3. Or, is there a program out there already that does this?

Here's the code from the above link, just in case:

[fixed]' Note the trailing backslash!
sToFolder = "c:\test\dest\"

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

' If the files exist in the folder from before (from a previous run),
' none of them must be RO or the script below will err. To avoid this,
' script runs the attrib command to remove any RO flags.
oShell.Run "attrib.exe /s -r " & sToFolder & "*", 0, True

' Using the desktop folder as an example
'sFolderWithLinks = oShell.SpecialFolders("Desktop")

' For a hard coded path, uncomment the following line and edit the path
sFolderWithLinks = "c:\test\source"

Set oFiles = oFSO.GetFolder(sFolderWithLinks).Files

For Each oFile In oFiles
If LCase(oFSO.GetExtensionName(oFile)) = "lnk" Then
Set oShellLink = oShell.CreateShortcut(oFile.Path)
sLinkTargetPath = oShellLink.TargetPath
If oFSO.FileExists(sLinkTargetPath) Then
oFSO.CopyFile sLinkTargetPath, sToFolder, True
End If
End If
Next[/fixed]

Thanks!
 

twohawks

Distinguished
Mar 10, 2010
2
0
18,510
Hi cosmo,
Although the below tags link to a member-site (popular), this is really not meant to come across as advertising.. I had been looking for some time into this similar issue, and information on the net seems not only spotty and incomplete, but the solution is very elusive... and it shouldn't be.

What's really tough to find is way to select objects at random, right-click on them, and then copy their targets ...whenever and wherever you wish to do this. Most scripts require coding the paths and such and launching the script... but I wanted more freedom.

The following links demonstrate some scripting methods and alternatives using simple windows scripting methods.
The first link discusses the technical issues in detail, as well as providing a very simple approach to how this can be handled. I am not posting that all here because due to its detail its a bit lengthy (yes, I wrote it).

I post all this here in the spirit of seeding the best answers I could find in places where seekers -like myself- may find them ...places I arrived at before in search of the answer ;^)
Cheers,
TwoHawks


http://www.experts-exchange.com/articles/Programming/Languages/Visual_Basic/VB_Script/Script-to-copy-or-move-collection-of-selected-files-plus-shortcut-target-files-referenced-by-lnk.html

see this for folder recursion etc
vbs to work on folder contains shortcuts to copy the source to another folder.
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_22498609.html

see this too
Batch File To Copy Pictures
http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_24556493.html

powershell possibility
http://www.vistax64.com/powershell/171506-passing-arguments-bat-powershell-script.html