How do I create a shortcut via command-line in Windows?

后端 未结 9 868
忘掉有多难
忘掉有多难 2020-11-28 04:17

I want my .bat script (test.bat) to create a shortcut to itself so that I can copy it to my windows 8 Startup folder.

I have written this line of code to copy the f

9条回答
  •  借酒劲吻你
    2020-11-28 04:59

    I created a VB script and run it either from command line or from a Java process. I also tried to catch errors when creating the shortcut so I can have a better error handling.

    Set oWS = WScript.CreateObject("WScript.Shell")
    shortcutLocation = Wscript.Arguments(0)
    
    'error handle shortcut creation
    On Error Resume Next
    Set oLink = oWS.CreateShortcut(shortcutLocation)
    If Err Then WScript.Quit Err.Number
    
    'error handle setting shortcut target
    On Error Resume Next
    oLink.TargetPath = Wscript.Arguments(1)
    If Err Then WScript.Quit Err.Number
    
    'error handle setting start in property
    On Error Resume Next
    oLink.WorkingDirectory = Wscript.Arguments(2)
    If Err Then WScript.Quit Err.Number
    
    'error handle saving shortcut
    On Error Resume Next
    oLink.Save
    If Err Then WScript.Quit Err.Number
    

    I run the script with the following commmand:

    cscript /b script.vbs shortcutFuturePath targetPath startInProperty
    

    It is possible to have it working even without setting the 'Start in' property in some cases.

提交回复
热议问题