Copy shortcuts with VB

Posted on 2006-06-01
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


    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

    y not?

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

    LVL 35

    Expert Comment


    You could always use windows scripting host:

     Dim wShell, wShortcut, wShortcut2
     Set wShell = CreateObject("")
     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

    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

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    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…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now