Discussion:
Relative Path in Create Shortcut Script
(too old to reply)
andrew
2005-11-07 12:31:02 UTC
Permalink
Hi

I need a script that will create a shortcut with the target path relative to
the folder where the script is run. I have the following script which
creates a shortcut using absolute paths:-

Set oWS = WScript.CreateObject("WScript.Shell")

sLinkFile = "MyFile.LNK"

Set oLink = oWS.CreateShortcut(sLinkFile)

oLink.TargetPath = "C:\Folder1\LocationOfScript\SubFolder\MyFile.exe"
oLink.Description = "Shortcut to MyFile.exe"
oLink.IconLocation = "C:\Folder1\LocationOfScript\SubFolder\MyFile.exe"
oLink.Save


What I actually want to be able to do is to replace
"C:\Folder1\LocationOfScript" with ".", but this then creates a target path
of "C:\SubFolder\MyFile.exe" instead of the full path. How can I get this to
create the shortcut with a target path of
"Drive:\NewFolderStructure\LocationOfScript\SubFolder\MyFile.exe" from with
whatever directory the script is run?


Thanks for your help


Andrew
andrew
2005-11-08 09:53:02 UTC
Permalink
In case anyone finds it helpful, I have managed to solve this using
FileSystemObject.FileExists as follows:-

Dim fso
Dim oWS
Dim oLink
Dim strLinkFile
Dim strPath

strLinkFile = "MyFile.lnk"

Set fso = CreateObject("Scripting.FileSystemObject")
Set oWS = WScript.CreateObject("WScript.Shell")
Set oLink = oWS.CreateShortcut(strLinkFile)

strPath = oWS.CurrentDirectory

If fso.FileExists (strPath & "\SubFolder\MyFile.exe") Then
oLink.TargetPath = strPath & "\SubFolder\MyFile.exe"
oLink.Description = "Shortcut to MyFile.exe"
oLink.IconLocation = strPath & "\SubFolder\MyFile.exe"

oLink.Save

Else Wscript.Echo "The file was not in the " & strPath & "\SubFolder\ folder"

End If


Andrew
Post by andrew
Hi
I need a script that will create a shortcut with the target path relative to
the folder where the script is run. I have the following script which
creates a shortcut using absolute paths:-
Set oWS = WScript.CreateObject("WScript.Shell")
sLinkFile = "MyFile.LNK"
Set oLink = oWS.CreateShortcut(sLinkFile)
oLink.TargetPath = "C:\Folder1\LocationOfScript\SubFolder\MyFile.exe"
oLink.Description = "Shortcut to MyFile.exe"
oLink.IconLocation = "C:\Folder1\LocationOfScript\SubFolder\MyFile.exe"
oLink.Save
What I actually want to be able to do is to replace
"C:\Folder1\LocationOfScript" with ".", but this then creates a target path
of "C:\SubFolder\MyFile.exe" instead of the full path. How can I get this to
create the shortcut with a target path of
"Drive:\NewFolderStructure\LocationOfScript\SubFolder\MyFile.exe" from with
whatever directory the script is run?
Thanks for your help
Andrew
Continue reading on narkive:
Loading...