How to create a desktop shortcut from a batch file

9,783

The soultion was the triple quotes:

For some reason the

echo oLink.arguments = Chr(34) & "c:\Data\File 1.ext" & Chr(34)

Makes the File 1.ext open.

Share:
9,783

Related videos on Youtube

Morten Kahr
Author by

Morten Kahr

Updated on September 18, 2022

Comments

  • Morten Kahr
    Morten Kahr over 1 year

    I know there are many posts on this issue but there is a minor twist to what I need to do.

    I can figure out how to create a shortcut with an argument like this:
    "C:\Program Files\My App\App.exe" /s

    But I need to apply the path of a file wrapped in "" as argument like this:
    "C:\Program Files\My App\App.exe" "c:\Data\File.ext"
    Note the double quotes.

    I can't find any VBscript examples illustrating this behaviour.
    This is what I've based my current script on.

    @echo off
    
    set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
    
    echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
    echo sLinkFile = "%USERPROFILE%\Desktop\myshortcut.lnk" >> %SCRIPT%
    echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
    echo oLink.TargetPath = "D:\myfile.extension" >> %SCRIPT%
    echo oLink.Save >> %SCRIPT%
    
    cscript /nologo %SCRIPT%
    del %SCRIPT%
    
  • nixda
    nixda over 10 years
    @MortenKahr Usually I use Chr(34) instead of double quotes for the outer pair as shown on stackoverflow
  • Morten Kahr
    Morten Kahr over 10 years
    @nixda So echo oLink.arguments = Chr(34) c:\Data\File 1.ext Chr(34) ?
  • nixda
    nixda over 10 years
    @MortenKahr echo oLink.arguments = Chr(34) & "c:\Data\File 1.ext" & Chr(34)
  • Ƭᴇcʜιᴇ007
    Ƭᴇcʜιᴇ007 over 10 years
    Adjusted my answer (after testing).
  • Morten Kahr
    Morten Kahr over 10 years
    @nixda (techie007) I'll try both in the morning. Thanks a lot