Copy shortcuts with VB

Posted on 2006-06-01
Medium Priority
Last Modified: 2010-04-30
I have code which copies files and folders using FSO, but does not copy actual shortcuts.  Instead, it copies the file that the shortcut points to.  How can I copy a shortcut with VB6?
Question by:CCIG2006
LVL 10

Expert Comment

ID: 16831375

FSO has a property that returns the path to the object. you need to save just the path (shortcut to the file) and not the FSO object it self. modify your code to store just the path returned by FSO.


LVL 26

Accepted Solution

EDDYKT earned 600 total points
ID: 16851329
y not?

Private Sub Command1_Click()
     Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CopyFile "C:\Shortcut to abc.lnk", "C:\Shortcut to abc1.lnk", false
End Sub

LVL 35

Expert Comment

ID: 16851734

You could always use windows scripting host:

 Dim wShell, wShortcut, wShortcut2
 Set wShell = CreateObject("wscript.shell")
 Set wShortcut = wShell.CreateShortcut("C:\YourShortcut.lnk") 'creates new shortcut if it doesnt exist, sets to existing if it does
 Set wShortcut2 = wShell.CreateShortcut("C:\Copy of YourShortcut.lnk") 'the copy
 With wShortcut2
  .Arguments = wShortcut.Arguments
  .Description = wShortcut.Description
  .Hotkey = wShortcut.Hotkey
  .IconLocation = wShortcut.IconLocation
  .TargetPath = wShortcut.TargetPath
  .WindowStyle = wShortcut.WindowStyle
  .WorkingDirectory = wShortcut.WorkingDirectory
 End With
 MsgBox "'" & wShortcut2.FullName & "' target=" & wShortcut2.TargetPath
 Set wShell = Nothing
 Set wShortcut = Nothing
 Set wShortcut2 = Nothing


Author Comment

ID: 16875427
This is part of the code I use to backup a file specified in the tmpData(j) variable.

        If tmpData(J) <> "" Then
            FSO.CreateFolder BackupPath & "\BF_" & J
            'FSO.CopyFile tmpData(J), BackupPath & "\BF_" & J & "\"
        End If

If I use the modified code below, it won't work.

        If tmpData(J) <> "" Then
            FSO.CreateFolder BackupPath & "\BF_" & J
            FSO.CopyFile tmpData(J) & ".lnk", BackupPath & "\BF_" & J & "\"
        End If

How can I have it differentiate between an actual file and a shortcut?

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question