diskOP

29 July 2021 Link



Description

Module to Abstract and provide higher level disk operation functions by using the functionality provided by LuaFileSystem

Installation

There are 2 ways to install it:
  • The module consists of a single Lua file which can be placed in any path where Lua can find it through its package.path definition.
  • Or you can install it using Luarocks by running the command:
luarocks install diskOP

Usage

To use the module simply do:
dsko = require("diskOP")
Now tu will contain all the functions available for table manipulation

API

Functions

sanitizePath

Function that converts all path separators to '/' and adds one in the end for directories if not already there
Syntax
sanitizePath((path:string),(file:boolean)) -> (sanitizedPath:string)
Usage
  1. spath = sanitizePath([[C:\diskOP]]) -- spath = "C:/diskOP/"
  2. spath = sanitizePath([[C:\diskOP]],true) -- spath = "C:/diskOP"

Inputs
  • path string containing the path
  • file boolean to indicate whether the given path points to a file. In that case the ending '/' is not added

Returns
  • sanitizedPath string containing the sanitized path

verifyPath

Function to check whether the path exists
Syntax
verifyPath((path:string))->(boolean/nil),[(message:string)]
Usage
  1. verifyPath([[C:\diskOP]])

Inputs
  • path string containing the path

Returns
  • true is path exists or
  • false if path does not exist with message "Path does not exist"
  • nil if path is not a string with message "Path should be a string"

recurseIter

Function to return a iterator to traverse the directory and files from the given path
Syntax
recurseIter((path:String),(fd:integer),(onlyCurrent:boolean)) -> (iterator:table)
Usage
  1. iter = recurseIter([[C:\diskOP]],0) -- To list both files and directories in the diskOP hierarchy
  2. for item,path,typ in iter.next do
  3. print(item,path,typ)
  4. end

Inputs
  • path string containing the path
  • fd integer to specify what type of items to list:
    • 1 means files only
    • 2 means directories only
    • anything else means files and directories both
  • onlyCurrent boolean when true will only list contents of path directory otherwise it will recurse down

Returns
  • iterator table which on every call to iterator.next returns the next item name, its path and its type "file" or "directory"


listLocalHier

Function to list a hierarchy using recurseIter and return it as an array
Syntax
listLocalHier((path:String),(fd:integer),(onlyCurrent:boolean)) -> (list:Array)
Usage
  1. list = listLocalHier([[C:\diskOP]],0)
  2. for i = 1,#list do
  3. print(list[i][1],list[i][2],list[i][3])
  4. end

Inputs
  • path string containing the path
  • fd integer to specify what type of items to list:
    • 1 means files only
    • 2 means directories only
    • anything else means files and directories both
  • onlyCurrent boolean when true will only list contents of path directory otherwise it will recurse down

Returns
  • list array containing a 3 element array at each position. The 3 elements are:
    • Item name
    • Item path
    • Item type - "file" or "directory"

emptyDir

Function to empty a directory from all its contents
Syntax
emptyDir((path:string)) 
Usage
  1. emptyDir([[C:\diskOP]])

Inputs
  • path string containing the path

Returns
Nothing

getFileName

Function to extract the file name from the path
Syntax
getFileName((path:string))->(filename:string)
Usage
  1. filename = getFileName([[C:\diskOP\main.lua]]) -- filename = "main.lua"

Inputs
  • path string containing the path to the file

Returns
  • filename string containing just the file name

createPath

Function to make sure that the given path exists. If not then the full hierarchy is created where required to reach the given path.
Syntax
createPath((path:string)) -> (true/nil),[(message:string)]
Usage
  1. createPath([[C:\diskOP\folder1\folder1]])

Inputs
  • path string containing the path to create

Returns
  • true if successful
  • nil and error message if not successful


file_exists

Function to check whether the file exists.
Syntax
file_exists(file) -> (true/false),[(message:string)]
Usage
  1. if file_exists([[C:\diskOP\diskOP.lua]]) then
  2. f = io.open([[C:\diskOP\diskOP.lua]])
  3. end

Inputs
  • file string containing the path and file name to the file to check

Returns
  • true if file exists
  • false is it does not and the message system returned when trying to open the file.


copyFile

Function to copy a source file to a destination path.
Syntax
copyFile((source:string),(destPath:string),(fileName:string),[(chunkSize:integer)],[(overwrite:boolean)]) -> (true/false),[(message:string)]
Usage
  1. copyFile([[C:\diskOP\diskOP.lua]],[[C:\diskOP\folder1\folder1]],"newname.lua",nil,true) -- overwrite if it already exists

Inputs
  • source string containing path pointing to file
  • destPath string containing path where the file needs to be copied
  • fileName string containing name by which the file will be created in destPath with contents of source
  • chunkSize integer is the number of bytes read at a time during the copy process
  • overwrite boolean if true will overwrite a file with the same name at destPath if it exists
Returns
  • true if successful
  • nil/false with an error message if unsucessful

Repository

The repository can be found on github