Difference between revisions of "Filesystem Functions"

From SolarStrike wiki
Jump to: navigation, search
(New page: == getPath == '''string getPath()''' Returns the full path to MicroMacro's directory. '''Example''' <source lang="lua"> path = getPath(); </source>)
 
(getPath)
Line 8: Line 8:
 
path = getPath();
 
path = getPath();
 
</source>
 
</source>
 +
 +
== getExecutionPath ==
 +
'''path getExecutionPath()'''
 +
 +
Returns the 'working directory' or 'execution path' of the main(executed) script. That is, if you execute a script in C:/micromacro/scripts/, this function will return the string "C:/micromacro/scripts". The final "/" is not included.
 +
 +
Your working directory will not change when you include() or dofile() other scripts. Only when executing another script, or using [[Misc_Functions#setExecutionPath|setExecutionPath]]().
 +
 +
'''Example'''
 +
<source lang="lua">
 +
local basepath = getExecutionPath();
 +
printf("Base: %s\n", basepath);
 +
</source>
 +
 +
== setExectionPath ==
 +
'''setExectionPath(path)'''
 +
 +
Changes the 'working directory' or 'execution path'. You may change your working path with this function to make your job easier when using the include() function.
 +
 +
'''Example'''
 +
<source lang="lua">
 +
-- this will include the file C:/myscript.lua
 +
setExecutionPath("C:/");
 +
include("myscript.lua");
 +
</source>
 +
 +
 +
== getFilePath ==
 +
'''path getFilePath(fullpath)'''
 +
 +
Returns just the path segment (not including final slash and file name with extension) of a fully qualified path.
 +
 +
'''Example'''
 +
<source lang="lua">
 +
local file = "C:/testing/myscript.lua";
 +
local path = getFilePath(file);
 +
 +
printf("The path of \'file\' is \'%s\'\n", path);
 +
-- path should now contain "C:/testing"
 +
</source>
 +
 +
 +
== getFileName ==
 +
'''path getFileName(fullpath)'''
 +
 +
Returns just the file name segment (including extension) of a fully qualified path.
 +
 +
'''Example'''
 +
<source lang="lua">
 +
local fullpath = "C:/testing/myscript.lua";
 +
local filename = getFileName(fullpath);
 +
local path = getFilePath(fullpath);
 +
 +
-- filename should now contain "myscript.lua"
 +
-- path should now contain "C:/testing"
 +
</source>
 +
 +
 +
== getDirectory ==
 +
'''dir getDirectory'''
 +
 +
Returns a table containing the file names (without path) of all files and/or directories in the specified directory.
 +
 +
'''Example'''
 +
<source lang="lua">
 +
  dir = getDirectory(getExecutionPath());
 +
 +
  printf("Files and directories in %s:\n", getExecutionPath());
 +
  for i,v in pairs(dir) do
 +
    printf("  ==> %i:\t%s\n", i, v);
 +
  end
 +
</source>
 +
 +
The above example will produce the following (example) output:
 +
  Files and directories in C:/MicroMacro/scripts:
 +
    ==> 1:    script1.lua
 +
    ==> 2:    script2.lua
 +
    ==> 3:    script3.lua
 +
    ==> 4:    subdirectory

Revision as of 03:31, 15 November 2008

getPath

string getPath()

Returns the full path to MicroMacro's directory.

Example

path = getPath();

getExecutionPath

path getExecutionPath()

Returns the 'working directory' or 'execution path' of the main(executed) script. That is, if you execute a script in C:/micromacro/scripts/, this function will return the string "C:/micromacro/scripts". The final "/" is not included.

Your working directory will not change when you include() or dofile() other scripts. Only when executing another script, or using setExecutionPath().

Example

local basepath = getExecutionPath();
printf("Base: %s\n", basepath);

setExectionPath

setExectionPath(path)

Changes the 'working directory' or 'execution path'. You may change your working path with this function to make your job easier when using the include() function.

Example

-- this will include the file C:/myscript.lua
setExecutionPath("C:/");
include("myscript.lua");


getFilePath

path getFilePath(fullpath)

Returns just the path segment (not including final slash and file name with extension) of a fully qualified path.

Example

local file = "C:/testing/myscript.lua";
local path = getFilePath(file);

printf("The path of \'file\' is \'%s\'\n", path);
-- path should now contain "C:/testing"


getFileName

path getFileName(fullpath)

Returns just the file name segment (including extension) of a fully qualified path.

Example

local fullpath = "C:/testing/myscript.lua";
local filename = getFileName(fullpath);
local path = getFilePath(fullpath);

-- filename should now contain "myscript.lua"
-- path should now contain "C:/testing"


getDirectory

dir getDirectory

Returns a table containing the file names (without path) of all files and/or directories in the specified directory.

Example

  dir = getDirectory(getExecutionPath());

  printf("Files and directories in %s:\n", getExecutionPath());
  for i,v in pairs(dir) do
    printf("  ==> %i:\t%s\n", i, v);
  end

The above example will produce the following (example) output:

 Files and directories in C:/MicroMacro/scripts:
   ==> 1:     script1.lua
   ==> 2:     script2.lua
   ==> 3:     script3.lua
   ==> 4:     subdirectory