tags:

views:

62

answers:

2

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

A: 

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.

Doug Currie
Actually, I think just figured it out. I did os.execute('C:\\\Program Files\\\Movie Maker\\\moviemk.exe"') ... the extra '' around the "" seems to do the trick. Seems strange to me though.
Brian T Hannan
It may be the spaces in some of the file names. Windows struggles with those form time to time.
Judge Maygarden
+5  A: 

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.

RBerteig