🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Calling a batch file from lua!

Started by
6 comments, last by Woody FX 21 years, 1 month ago
Hi, I need to call a batch file from a lua script ! My c++ prog uses lua scripts and on of them needs to call a batch file that takes two argumets from dos it would look like:: batch c:\folder1 c:\folder2 but can i call this file from a lua script (lua 4)
Advertisement
I''m pretty sure Lua has the ability to execute console commands on the host machine, like C''s system(), but I can''t remember the function name.

Why you shouldn''t use iostream.h - ever! | A Good free online C++ book
I have gone through the documentation and i cannot find anything mentioned! Do not want t mail the mailing list yet!

Does anybody know?
Try:

execute("batch c:\\folder1 c:\\folder2")

Works in Lua 4.0.1, don't know if it's changed in 5.0.
EDIT: Just saw that you said Lua 4, so it should work.

[edited by - Dactylos on May 17, 2003 6:24:24 AM]
Thank you very much.... is that documented... will go and take a look now !!

If not will google it

Thanks again :-)


The folder names will be decided at run time by the lua script so they will have to be held in two variables.
E.g. Argument1 & Argumnet2

So for example Argument1 = c:\folder1
Argument2 = c:\folder2

So calling execute[[ batch Argument1 Argumnet2 ]]
would pass the actual string=''Argument1'' to the batch file rather that the path held by the varialbe Argument1

So you execute(batch Argument1 Argument2)
work?

or will it need the square brackets.... thanks lads

Brian

You can use the .. operator which concatenates strings:
Argument1 = "C:\\folder1" -- notice: \\ produces \Argument2 = "C:\\folder2"execute("batch " .. Argument1 .. " " .. Argument2) 
The above is treated as:
execute("batch C:\\folder1 C:\\folder2") 
Lua doesn''t do string interpolation. Here''s what you do, assuming you are
using Lua 4

function subst(str, table)
local v = gsub(str, ''$([_%a][_%w]*)'',
function(w) return %table[w] or '''' end)
return v
end

args = {
Argument1 = [[c:\folder1]],
Argument2 = [[c:\folder2]],
}

cmd = [[ batch $Argument1 $Argument2 ]]

print(subst(cmd, args))
execute(subst(cmd, args))

The above is a more complicated solution that should work..but your solution looks so much more tidy!

Regards
Brian

This topic is closed to new replies.

Advertisement