2

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!

Tamaska Janos
  • 61
  • 1
  • 6

2 Answers2

1

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 ..."

Egor Skriptunoff
  • 906
  • 1
  • 8
  • 23
0

Use luacom is faster than powershell

local luacom=require'luacom'
local shortcut_file_path='test_create_shortcut.lnk'
local target_file_path=arg[0]
local shellObject=luacom.CreateObject("WScript.Shell")
local shortcut=shellObject:CreateShortcut(shortcut_file_path)
shortcut.TargetPath=target_file_path
shortcut:Save()
assert(io.open(shortcut_file_path)):close()--shortcut file exist now
os.remove(shortcut_file_path)

And use FileSystemObject object (another COM), or Windows shell link file format spec for Kaitai Struct (parse binary file struct to get info on various file format) to retrieve shortcut info. Which 'lfs' can't do now.

see: Create a desktop shortcut with Windows Script Host - Windows Client | Microsoft Docs LuaCOM User Manual (Version 1.3)

RobertL
  • 1
  • 1