Krug.FileUtil (Krug v2.0.27) 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 with specified name.

Read a content of a file.

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

Replaces all ocurrences of a search_by parameter value by replace_to parameter value in the file.

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

Creates a zip from a directory with same name directory name plus ".zip" extension. This function uses File.ls! to obtain directory files and because that dont is recursive.

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 with 0o chars (for example 0o777, 0o755, 0o744, 0o600).

Example

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

copy_dir(from_dir, to_dir)

View Source

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.copy_dir(from_dir,to_dir)
true (or false if fail)
Link to this function

copy_file(from_file, to_file, ignore_if_exists \\ true)

View Source

Copy a file to another file.

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

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

Examples

iex > Krug.FileUtil.copy_file(from_file,to_file)
true (or false if fail)
iex > Krug.FileUtil.copy_file(from_file,to_file,true)
true (or false if fail)

Creates a new directory with name specified.

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

If exists a file with 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.create_dir(path)
true (or false if fail)
Link to this function

drop_dir(path, ignore_if_dont_exists \\ false)

View Source

Delete a directory with specified name.

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

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

Examples

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

drop_file(path, ignore_if_dont_exists \\ false)

View Source

Delete a file.

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

If file is a directory, return false.

Examples

iex > Krug.FileUtil.drop_file(path)
true (or false if fail)
iex > Krug.FileUtil.drop_file(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.read_file(path)
file content (or nil)
Link to this function

remove(path, insertion_points)

View Source

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

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

Accept only file extensions:

["sql","txt","html","xml","webmanifest","ts","js","ex","exs","sh","json","ret",
   "pdf","ppt","pptx","doc","docx","xls","xlsx","php","erl","gif","jpeg","jpg","png","bmp"]

will fail for other file extensions.

Example

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

replace_in_file(path, search_by, replace_to)

View Source

Replaces all ocurrences of a search_by parameter value by replace_to parameter value in the file.

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

Accept only file extensions:

["sql","txt","html","xml","webmanifest","ts","js","ex","exs","sh","json","ret",
   "pdf","ppt","pptx","doc","docx","xls","xlsx","php","erl","gif","jpeg","jpg","png","bmp"]

will fail for other file extensions.

Example

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

write(path, content, insertion_points \\ [], insertion_point_tag \\ nil)

View Source

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

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

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

Accept only file extensions:

["sql","txt","html","xml","webmanifest","ts","js","ex","exs","sh","json","ret","csv",
   "pdf","ppt","pptx","doc","docx","xls","xlsx","php","erl","gif","jpeg","jpg","png","bmp"]

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)
Link to this function

zip_dir(path, drop_if_exists \\ false)

View Source (since 0.4.24)

Creates a zip from a directory with same name directory name plus ".zip" extension. This function uses File.ls! to obtain directory files and because that dont is recursive.

If already exists a zip file with this name, or the path directory don't exists or isn't a directory, then return false. Otherwise tries create a zip and return true if succeed.

If drop_if_exists parameter received and equals "true" boolean, then tries delete the equivalent zipped dir if these already exists before try zip.

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

Example

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