Rhythcypher
Rhythcypher

Reputation: 23

How to make shorcuts on the current directory of a usb drive using vbscript?

I'm new to coding, and my objective today is to create a shortcut(s) on the current directory of a usb thumb/flash drive. I made some progress with the script, but I'm having problems with setting the target path of the shortcut, because when usb flash drives are plugged-in to a computer they are automatically are assigned a drive letter, so the target path for shortcut is different every time. I don't know how would I work this out. It will be much appreciated if you guys would help me out with this one, thanks!

    Set oWS = WScript.CreateObject("WScript.Shell")
    sLinkFile = "A.lnk"
    Set oLink = oWS.CreateShortcut(sLinkFile)
      oLink.TargetPath = "(What the usb drive letter should be):\A.vbs" 
      '  oLink.Arguments = ""
      '  oLink.Description = "MyProgram"   
      '  oLink.IconLocation = "C:\xxx\notporno"
      '  oLink.WindowStyle = "1"
    oLink.Save

Upvotes: 0

Views: 347

Answers (1)

Rhythcypher
Rhythcypher

Reputation: 23

Try using relative path. – forsajt

Thanks forsajt ! you really helped me with this one, here is part of my solution:

strLinkFile = "A.lnk" 

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

strPath = oWS.CurrentDirectory 

If fso.FileExists (strPath & "\Target.file") Then 
oLink.TargetPath = strPath & "\Target.file" 
oLink.Save 

End if

Upvotes: 1

Related Questions