Copy shortcuts with VB

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?
CCIG2006Asked:
Who is Participating?
 
EDDYKTCommented:
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

etc
0
 
harisrashidCommented:
hi,

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.

Regards,

Haris
0
 
mvidasCommented:
CCIG,

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
  .Save
 End With
 MsgBox "'" & wShortcut2.FullName & "' target=" & wShortcut2.TargetPath
 Set wShell = Nothing
 Set wShortcut = Nothing
 Set wShortcut2 = Nothing

Matt
0
 
CCIG2006Author Commented:
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?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.