Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't applications in Program Files run using os.execute in lua?

Tags:

lua

I'm trying to run an executable using Lua's os.execute() function. If I do something like the following it does not work:

os.execute("C:\\\Program Files\\\Movie Maker\\\moviemk.exe")

However, if I put my lua file in the same path that moviemk.exe is in then it can call it.

Any ideas why this may be?

P.S. I'm using Windows XP SP3

like image 781
Brian T Hannan Avatar asked Dec 04 '22 11:12

Brian T Hannan


2 Answers

This is a classic problem with the command shell. It isn't really a Windows specific problem, except that on *nix, people never really got into the habit of putting spaces in file names, and Windows puts spaces in several default system places such as C:\Program Files.

What is happening is that os.execute(str) is implemented in terms of the ANSI C function system(str), which on Windows tries to duplicate the effect of typing "cmd /C "..str to the command prompt. (On *nix, it uses /bin/sh -c instead of cmd /C.)

The classic problem is that this has to split the complete command string at whitespace to decide what program to run, and what its arguments are.

Your original example: os.execute("C:\\Program Files\\Movie Maker\\moviemk.exe") effectively became cmd /c c:\program files\movie maker\moviemk.exe, which after splitting it up on whitespace, CMD tried to find a program named c:\program to execute with arguments named files\movie and maker\moviemk.exe. This isn't what you intended.

The solution is to be much more defensive about quoting.

I would write this as:

os.execute [["C:\Program Files\Movie Maker\Moviemk.exe"]]

If there were additional command line arguments to be supplied, I would use double-quotes around each, and a single space between arguments. Using the long string syntax [[...]] has the advantage that backslash isn't a special character, so you don't need extra leaning toothpicks making it harder to read the string literal.

Using double quotes around each argument should work on both Windows and *nix, although it is harder to find commands that are the same on both platforms, of course.

One other detail to be aware of is that \Programs Files might not be on C:. There might not even be a disk named C:. (My work PC boots from E: and I find more buggy programs that way.) The easiest way to learn the correct pathname is to just use the environment variable ProgramFiles. There are many other ways.

like image 167
RBerteig Avatar answered Dec 06 '22 01:12

RBerteig


Try:

 os.execute("C:\\Program Files\\Movie Maker\\moviemk.exe")

or:

 os.execute("C:/Program Files/Movie Maker/moviemk.exe")

The '\' character is used for escape characters in Lua.

like image 24
Doug Currie Avatar answered Dec 05 '22 23:12

Doug Currie