webu
Full Member
Posts: 149
|
Post by webu on Oct 14, 2022 23:03:19 GMT 1
Hi all,
i need the target for a given LNK-file.
If i drag an LNK-file from the desktop to my appliktion. I get the path and filename to the LNK-file in _DosCmd$. But i need the target-file for work.
Anybody any idea? It would be good if it could not be done via Powershell or WScript.
Thank you
|
|
|
Post by (X) on Oct 16, 2022 19:25:18 GMT 1
Not sure if this meets with:"It would be good if it could not be done via Powershell or WScript." Here goes (updated: set objects to nothing, just in case):
'################################################################################# ' ' DISCLAIMER: THIS CODE IS OFFERED AS IS FOR EDUCATIONAL PURPOSES. ' YOU ARE FREE TO USE, COPY AND DISTRIBUTE. ' THE USE OF THIS CODE IS AT YOUR OWN RISK. ' I ACCEPT NO LIABILITY NOR MAKE ANY CLAIM OF ' CORRECTNESS OR SUITABILITY IN PART OR IN WHOLE. ' '################################################################################# ' ' FILENAME Lib_GetShortcutTarget.G32 ' ' DESCRIPTION A Shortcut file (.lnk) has a target. The GetShortcutTarget() ' function returns it. ' ' AUTHOR (X) ' EMAIL xman.gb32@gmail.com ' WEBSITE https://gfatrix.proboards.com/ ' STARTED 2022-10-16 ' UPDATED <ISO date> ' ' $Export "Lib_GetShortcutTarget"
Function GetShortcutTarget(ByRef scfn$) As String $Export Proc GetShortcutTarget "(ByRef ShortcutFullPath$)" Try If IsLink(scfn) Dim oShell Dim oFolder Dim oFolderItem Dim oLink Set oShell = CreateObject("Shell.Application") Set oFolder = oShell.NameSpace(Get_Path(scfn)) Set oFolderItem = oFolder.ParseName(Get_File(scfn)) Set oLink = oFolderItem.GetLink Dim Target$ = oLink.Target.path Set oLink = Nothing Set oFolderItem = Nothing Set oFolder = Nothing Set oShell = Nothing Return Target Else Return "" EndIf Catch Trace Err$ Return "" EndCatch EndFunc
Function Get_Path(sFullPath As String) As String Get_Path = Left(sFullPath, RInStr(sFullPath, "\")) EndFunc
Function Get_File(sFullPath As String) As String Get_File = Right(sFullPath, Len(sFullPath) - RInStr(sFullPath, "\")) EndFunc
Function HasExtension(s$) As Bool Return Left(Right(s$, 4)) == "." EndFunc
Function IsLink(ByRef s$) As Bool If HasExtension(s) If (Upper(Right(s, 3)) == "LNK") Return True Else MsgBox s & #10" is not a Shortcut (.lnk) file." Return False EndIf Else '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' If the filename does not include a ".lnk" extension then Exist() will not ' find it even if the file actually exists, so, we need to add a ".lnk" extension ' just in case the passed filename may indeed be a Shortcut file. ' s = s & ".lnk" If Exist(s) Return True Else MsgBox s & #10" is not a Shortcut (.lnk) file." Return False EndIf EndIf EndFunc
Just compile, put in your program's directory and include this line at the head of your code:
$Library "Lib_GetShortcutTarget"
|
|
webu
Full Member
Posts: 149
|
Post by webu on Oct 17, 2022 0:09:20 GMT 1
Thank you, it works fine
I have short it a little bit:
Message LinkTarget("C:\Users\ad\Desktop\Roul.lnk")
Function LinkTarget(lnk$) As String Dim bsl% = RInStr(lnk$, "\") //backslash-position Dim oShell : Set oShell = CreateObject("Shell.Application") Return oShell.NameSpace(Left$(lnk$, bsl)).ParseName(Mid$(lnk$, bsl + 1)).GetLink.Target.path EndFunc
Is a "Set oShell = Nothing" important? I missed it.
I think, that your CreateObject("Shell.Application") is not CreateObject("Wscript.Shell"). Or is it?
|
|
|
Post by (X) on Oct 17, 2022 0:43:41 GMT 1
|
|
|
Post by (X) on Oct 17, 2022 0:51:27 GMT 1
Message LinkTarget("C:\Users\ad\Desktop\Roul.lnk")
One pitfall I did not expect is: If ".lnk" is not specified, the extension is not assumed and this causes an error.
I therefore check if the filename had no extension, I added ".lnk" to it and also rejected all cases of other extensions.
|
|
webu
Full Member
Posts: 149
|
Post by webu on Oct 17, 2022 13:34:13 GMT 1
I know. My short-version works, it is not for understanding! :-)
In my Windows desktop environments, links without ".lnk" at the end do not produce the same results as with ".lnk". You could not double-click on it successfully, because that generates an error.
|
|
|
Post by (X) on Oct 17, 2022 14:06:28 GMT 1
This has nothing to do with Drag & Drop, but, I started wondering about a few possibilities...
If you select a ".lnk" file from the ShowOpen dialog; the returned cd.FileName contains the Link target's full file path, not the Shortcut's filename listed in the directory.
Ocx CommDlg cd
cd.FileName = "" cd.FileTitle = "" cd.DefExt = "FILE" cd.Filter = "Files (*.FILE)|*.FILE|All Files (*)|*" cd.Title = "Select a File" cd.Flags = cdoHideReadOnly + cdoFileMustExist cd.ShowOpen
Message cd.FileName
In contrast, if you open the file:"somelink.lnk" for input, it is the Shortcut file that is actually opened, not the target.
Dim a$ Open "C:\somelink.lnk" for Input As # 1 'Open cd.FileName for Input As # 1 Input # 1, a$ Close # 1
Print Print a$
|
|
webu
Full Member
Posts: 149
|
Post by webu on Oct 17, 2022 16:43:43 GMT 1
You use the fileselector box of the system and this box resolves the lnk file and returns the destination file directly. This has nothing to do with the application that calls the box.
Du benutzt die fileselector-Box des Systems und diese Box löst die lnk-Datei auf und liefert direkt die Ziel-Datei zurück. Das hat nichts mit der Anwendung zu tun, welche die Box aufruft.
|
|
|
Post by (X) on Oct 17, 2022 19:03:50 GMT 1
I agree, any Dialog box called by any application written in any programming language should behave the same way.
|
|