Krug.FileUtil (Krug v0.1.0) View Source

Utilitary module to handle files and directories.

Link to this section Summary

Functions

Change the file/directory permissions.

Copy the content of a directory to another directory.

Creates a new directory with name specified.

Delete a directory whit specified name.

Read a content of a file.

Remove the content of a file if that is located between insertionPoints[0] and insertionPoints[1]. The insertionPoints are removed too.

Replaces all ocurrences of a searchBy parameter value by replaceTo parameter value in the file.

Write a content in a file, if the file exists and is a file (not directory).

Link to this section Functions

Change the file/directory permissions.

Return true if the file/directory exists and the permissions are correctly changed.

Permission should be initiate whit 0o chars (for example 0o777, 0o755, 0o744, 0o600).

Example

iex > Krug.FileUtil.chmod(path,0o777)
true (or false if fail)

Copy the content of a directory to another directory.

Finally change permissions of the destination directory to 777 (chmod), in success case.

Example

iex > Krug.FileUtil.copyDir(fromDir,toDir)
true (or false if fail)
Link to this function

copyFile(fromFile, toFile, ignoreIfExists \\ true)

View Source

Copy a file to another file.

Return false if a destination file already exists and the parameter ignoreIfExists don't was received as true.

Finally change permissions of the destination file to 777 (chmod), in success case.

Examples

iex > Krug.FileUtil.copyFile(fromFile,toFile)
true (or false if fail)
iex > Krug.FileUtil.copyFile(fromFile,toFile,true)
true (or false if fail)

Creates a new directory with name specified.

If already exists a directory whit this name, only preserve these directory.

If exists a file whit this name try delete the file before create a new directory. Return false if this file cannot be deleted.

Finally change permissions of these dir to 777 (chmod), in success case.

Example

iex > Krug.FileUtil.createDir(path)
true (or false if fail)
Link to this function

dropDir(path, ignoreIfDontExists \\ false)

View Source

Delete a directory whit specified name.

Return false if the directory don't exists and parameter ignoreIfDontExists is false/don't received.

If the name is a file and not a diectory, return false.

Examples

iex > Krug.FileUtil.dropDir(path)
true (or false if fail)
iex > Krug.FileUtil.dropDir(path,true)
true (or false if fail)
Link to this function

dropFile(path, ignoreIfDontExists \\ false)

View Source

Delete a file.

Return false if the file don't exists and the parameter ignoreIfDontExists don't was received as true.

If file is a directory, return false.

Examples

iex > Krug.FileUtil.dropFile(path)
true (or false if fail)
iex > Krug.FileUtil.dropFile(path,true)
true (or false if fail)

Read a content of a file.

Return nil file is a directory, or don't exists, or the operation failed on open/read the file.

Example

iex > Krug.FileUtil.readFile(path)
file content (or nil)
Link to this function

remove(path, insertionPoints)

View Source

Remove the content of a file if that is located between insertionPoints[0] and insertionPoints[1]. The insertionPoints are removed too.

Fail if the file don't exists, is a directory, operation fail, or the insertionPoints[0] or insertionPoints[1] don't exists in file.

Accept only file extensions: ["sql","txt","html","ts","js","ex","exs","sh","json"] will fail for other file extensions.

Example

iex > Krug.FileUtil.remove(path,["markerBegin","markerEnd"])
true (or false if fail)
Link to this function

replaceInFile(path, searchBy, replaceTo)

View Source

Replaces all ocurrences of a searchBy parameter value by replaceTo parameter value in the file.

Fail if the file is directory or don't exists.

Accept only file extensions: ["sql","txt","html","ts","js","ex","exs","sh","json"] will fail for other file extensions.

Example

iex > Krug.FileUtil.replaceInFile(path,"AA","BB")
true (or false if fail)
Link to this function

write(path, content, insertionPoints \\ [], insertionPointTag \\ nil)

View Source

Write a content in a file, if the file exists and is a file (not directory).

If the parameter insertionPoints and parameter insertionPointTag are received, the content original is preserved and content received is inserted before of insertionPointTag and beetwen insertionPoints[0] and insertionPoints[1]. (insertionPoints are write in this moment, and could be used to remove this content later).

If insertionPointTag is received and don't exists in file, return false.

Accept only file extensions: ["sql","txt","html","ts","js","ex","exs","sh","json"] will fail for other file extensions.

Examples

iex > Krug.FileUtil.write(path,content)
true (or false if fail)
iex > Krug.FileUtil.write(path,content,["markerBegin","markerEnd"],"markerInsertion")
true (or false if fail)