Filesystem Functions

From SolarStrike wiki
Jump to: navigation, search

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 dofile() other scripts. Only when executing another script (via include()), 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


If you must check if one of the results is really a file or a directory, you can use isDirectory() like so:

local files = getDirectory(getExecutionPath());

for i,v in pairs(files) do
	if( isDirectory(getExecutionPath() .. "/" .. v) ) then
		-- It is a directory
		v = "[FOLDER]\t" .. v;
	else
		v = "[FILE]\t" .. v;
	end
	printf("  ==> %s\n", v);
end

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

   ==> [FILE]    script1.lua
   ==> [FILE]    script2.lua
   ==> [FILE]    script3.lua
   ==> [FOLDER]  subdirectory


isDirectory

bool isDirectory(path)

Returns true if the given path is really a directory, otherwise, it returns false.

Example:

local path = "C:/Windows";
if( isDirectory(path) ) then
  -- C:/Windows must exist, and is not a file.
end