Search code examples
iolualnk

Create lnk shortcut from lua (without lfs)


I would like to write a function to create a windows .lnk file from my lua script. I found a function in the LuaFileSystem library . Is there a way to do this without the library? (The reason: I am writing the script for multiple users, would be nice if we don't have to install the library on every machine.)

I appreciate the help!


Solution

  • To make a shortcut (an .lnk file)

    -- your .lnk file
    local your_shortcut_name = "your_shortcut.lnk"      
    
    -- target (file or folder) with full path
    local your_target_filespec = [[C:\Windows\notepad.exe]]
    
    local ps = io.popen("powershell -command -", "w")
    ps:write("$ws = New-Object -ComObject WScript.Shell;$s = $ws.CreateShortcut('"..your_shortcut_name.."');$s.TargetPath = '"..your_target_filespec.."';$s.Save()")
    ps:close()
    

    To make a symlink simply use os.execute"mklink ..."