Os.execute: Difference between revisions
From GiderosMobile
No edit summary |
No edit summary |
||
| (18 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
'''Available since:''' Gideros 2011.6<br/> | '''Available since:''' Gideros 2011.6<br/> | ||
'''Class:''' [[os]]<br/> | |||
=== Description === | === Description === | ||
This function is equivalent to the C function system. It passes command to be executed by an operating system shell. It returns a status code | This function is equivalent to the C function system. It passes command to be executed by an operating system shell. It returns a status code which is system-dependent. If command is absent then it returns nonzero if a shell is available and zero otherwise. | ||
< | <syntaxhighlight lang="lua"> | ||
(number) = os.execute(command) | (number) = os.execute(command) | ||
</ | </syntaxhighlight> | ||
=== Parameters === | === Parameters === | ||
'''command''': (string) command to execute <br/> | '''command''': (string) command to execute<br/> | ||
For Windows you can check: | |||
* '''https://learn.microsoft.com/en-gb/windows-server/administration/windows-commands/windows-commands''' | |||
* '''https://ss64.com/nt/explorer.html''' '''''HOT''''' | |||
=== Return values === | === Return values === | ||
'''Returns''' (number) status code<br/> | '''Returns''' (number) status code<br/> | ||
=== Examples === | |||
'''Opens a folder (Windows)''' | |||
<syntaxhighlight lang="lua"> | |||
if application:getDeviceInfo() == "Windows" then | |||
local lfs = require "lfs" | |||
-- go to user/pictures folder | |||
lfs.chdir(application:get("directory", "pictures")) | |||
-- check if "my_new_folder" directory exists in user/pictures folder | |||
local islfsdir = lfs.chdir(lfs.currentdir().."\\".."my_new_folder") | |||
if not islfsdir then | |||
-- if doesn't exist then create it and go to it | |||
lfs.mkdir(lfs.currentdir().."\\".."my_new_folder") | |||
lfs.chdir(lfs.currentdir().."\\".."my_new_folder") | |||
end | |||
end | |||
-- opens a window of the current lfs dir | |||
os.execute("start "..lfs.currentdir()) | |||
</syntaxhighlight> | |||
'''If your path contains spaces''' | |||
<syntaxhighlight lang="lua"> | |||
local path = "H:\\ShdowPLAY\\TMP\\Mafia Definitive Edition" | |||
--local path = "H:/ShdowPLAY/TMP/Mafia Definitive Edition" -- same | |||
os.execute("start \"\" \"" .. path .. "\"") | |||
</syntaxhighlight> | |||
'''Opens a folder (windows 11), in the form''' ''start "" folderpath''. '''The empty quotes set the window title correctly and prevent path-handling errors''' | |||
<syntaxhighlight lang="lua"> | |||
local folderpath = "C:\the\folder\path\handles spaces" | |||
os.execute("start ".."\"".."\"".." ".."\""..folderpath.."\"") -- start "" folderpath | |||
</syntaxhighlight> | |||
'''Opens a folder (windows 11) and select the file''' | |||
<syntaxhighlight lang="lua"> | |||
os.execute(explorer.exe /select,"C:\Folder\subfolder\file.txt") -- notice the \ | |||
</syntaxhighlight> | |||
'''Copies some files from folder to folder beyond a given date''' | |||
* see: '''https://learn.microsoft.com/en-gb/windows-server/administration/windows-commands/xcopy''' | |||
<syntaxhighlight lang="lua"> | |||
os.execute("xcopy \rawdata \reports /d:12-29-1993") | |||
</syntaxhighlight> | |||
{{Os}} | |||
Latest revision as of 22:47, 19 September 2026
Available since: Gideros 2011.6
Class: os
Description
This function is equivalent to the C function system. It passes command to be executed by an operating system shell. It returns a status code which is system-dependent. If command is absent then it returns nonzero if a shell is available and zero otherwise.
(number) = os.execute(command)
Parameters
command: (string) command to execute
For Windows you can check:
- https://learn.microsoft.com/en-gb/windows-server/administration/windows-commands/windows-commands
- https://ss64.com/nt/explorer.html HOT
Return values
Returns (number) status code
Examples
Opens a folder (Windows)
if application:getDeviceInfo() == "Windows" then
local lfs = require "lfs"
-- go to user/pictures folder
lfs.chdir(application:get("directory", "pictures"))
-- check if "my_new_folder" directory exists in user/pictures folder
local islfsdir = lfs.chdir(lfs.currentdir().."\\".."my_new_folder")
if not islfsdir then
-- if doesn't exist then create it and go to it
lfs.mkdir(lfs.currentdir().."\\".."my_new_folder")
lfs.chdir(lfs.currentdir().."\\".."my_new_folder")
end
end
-- opens a window of the current lfs dir
os.execute("start "..lfs.currentdir())
If your path contains spaces
local path = "H:\\ShdowPLAY\\TMP\\Mafia Definitive Edition"
--local path = "H:/ShdowPLAY/TMP/Mafia Definitive Edition" -- same
os.execute("start \"\" \"" .. path .. "\"")
Opens a folder (windows 11), in the form start "" folderpath. The empty quotes set the window title correctly and prevent path-handling errors
local folderpath = "C:\the\folder\path\handles spaces"
os.execute("start ".."\"".."\"".." ".."\""..folderpath.."\"") -- start "" folderpath
Opens a folder (windows 11) and select the file
os.execute(explorer.exe /select,"C:\Folder\subfolder\file.txt") -- notice the \
Copies some files from folder to folder beyond a given date
os.execute("xcopy \rawdata \reports /d:12-29-1993")