-- _file_.e
-- _file_.e
Following global procedures are defined:
replaces all occurences of a sub-string by another in a file
Params:text to be replaced
new text
result is written in a new file
old file is not affected
replace_text("test_file_.bak", "new_file_.bak", "this", "that")
writes a sequence of strings in a text file
Params:one line of text per string
write_lines("new_file.txt", {"first line","second line"})
puts(1, read_file("new_file.txt") & "\n")
first line
second line
read_lines(), append_lines(), write_file()
appends a sequence of strings to a text file
Params:one line of text per string
write_lines("file.txt", {"first line","second line"})
append_lines("file.txt", {"next line","second next line"})
puts(1, read_file("file.txt") & "\n")
first line
second line
next line
second next line
read_lines(), write_lines(), write_file()
writes a sequence of byte in a file
Params:file may be text or binary
Example:write_file("new_file.txt", {1,2,3})
? read_file("new_file.txt")
{1,2,3}
read_file(), read_lines()
writes a sequence of lines in a CSV file
Params:CSV separator is a semicolon
CSV strings are surrounded by quotes
write_csv("test2.csv", {{1,"Two",3,"Four"},{1,"Two",3,"Four"}})
analyze_object(read_csv("test2.csv", 1), "read_csv", {})
read_csv =
. [1]
. . [1] 1
. . [2] "Two"
. . [3] 3
. . [4] "Four"
appends a sequence of lines to a CSV file
Params:append_csv("test2.csv", {{1,"Two",3,"Four"},{1,"Two",3,"Four"}})
writes a sequence of lines in an INI file
Params:{ {section name, section content}, ... }
each section content is a list of parameters and values { "parameter=value", ... }
write_ini_file("test2.ini", {{"Section",{"name=value"}}})
[Section]
name=value
read_ini_file()
writes an element of an INI file
Params:INI file name
write_ini("test.ini", "Telnet", "port", "223")
[Telnet]
host=127.0.0.1
port=223
write_ini("test.ini", "HTTP", "host", "127.0.0.1")
[HTTP]
host=127.0.0.1
write_ini("test.ini", "HTTP", "port", "80")
[HTTP]
host=127.0.0.1
port=80
search_ini(), read_ini()
Clear (delete) a directory of all files, but retaining sub-directories.
Params:name of the directory you want to remove files from
This never removes a directory. It only ever removes files. It is used to
clear a directory structure of all existing files, leaving the structure
intact.
clear_directory("the_old_folder")
remove_directory(), delete_file()
Following global functions are defined:
remove end of line characters from given sequence
Params:puts(1, remove_eol("First Line\nSecond Line\r\nThird Line\rFourth Line") & "\n")
First Line Second Line Third Line Fourth Line
splits a complete filename in drive, path, name and extension
Params:s = split_filename("C:\path\filename.txt")
s is:
. [1] "C"
. [2] "\path"
. [3] "filename"
. [4] "txt"
returns file drive
Params:s = file_drive("C:\path\filename.txt") --> "C"
file_path(), file_base(), file_ext(), file_name()
returns file path
Params:s = file_path("C:\path\filename.txt") --> "\path"
file_drive(), file_base(), file_ext(), file_name()
returns filename without extension or path
Params:s = file_base("C:\path\filename.txt") --> "filename"
file_drive(), file_path(), file_ext(), file_name()
returns file extension
Params:s = file_ext("C:\path\filename.txt") --> "txt"
file_drive(), file_path(), file_base(), file_name()
returns filename with extension, without path
Params:s = file_name("C:\path\filename.txt") --> "filename.txt"
file_drive(), file_path(), file_base(), file_ext()
Get the type of a file
Params:name of the file to query. It must not have wildcards.
integer:
* -1 if file could be multiply defined
* 0 if filename does not exist
* 1 if filename is a file
* 2 if filename is a directory
dir()
check if a file exists
Params:file to check for
? file_exists("test_file_.bak")
1
? file_exists("test_file_.old")
0
Returns the size of a file
Params:name of the file to search size of
atom: size of file if existing or -1 else
a = file_length("C:\\path\\filename")
returns file version
Params:name of the bound executable program
string: file version
Example:cmd = command_line()
s = get_file_version(cmd[1])
Determine if the supplied string is an absolute path or a relative path
Params:name of the file path
A relative path is one which is relative to the current directory and
an absolute path is one that doesn't need to know the current directory
to find the file.
integer: 0 if filename is a relative path or 1 otherwise
? is_absolute_path("") -- returns 0
? is_absolute_path("/usr/bin/abc") -- returns 1
? is_absolute_path("\\temp\\somefile.doc") -- returns 1
? is_absolute_path("../abc") -- returns 0
? is_absolute_path("local/abc.txt") -- returns 0
? is_absolute_path("abc.txt") -- returns 0
? is_absolute_path("c:..\\abc") -- returns 0
? is_absolute_path("c:\\windows\\system32\\abc") -- returns 1
? is_absolute_path("c:/windows/system32/abc") -- returns 1
Returns sum of all file sizes from rootdir and all subdirs
Params:directory to search size of
atom
a = dir_size("C:\\path\\subpath")
returns parent dir of given path
Params:remove last sub directory from path if there is one and return it.
if root return empty sequence
path has to point to a folder and not to a file
parent_dir(".") --> ".."
parent_dir("C:\path\subpath") --> "\path"
reads content of a text file as a sequence of strings
Params:one line of text per string
s = read_lines("new_file_.bak")
s may be (example):
. [1] "-- _file_.e"
. [2] ""
. [3] "include lib/_file_.e"
. [4] "include lib/_debug_.e"
. [5] "include get.e"
...
write_lines(), read_file()
reads content of a text file as a sequence of bytes
Params:file may be text or binary
Example:write_file("new_file.txt", {1,2,3})
? read_file("new_file.txt")
{1,2,3}
write_file(), read_lines()
reads a CSV file in a sequence
Params:CSV file to read
if set skip first line in CSV file
CSV separator is a semicolon
CSV strings may be surrounded by quotes
analyze_object(read_csv("test.csv", 0), "read_csv", {})
read_csv =
. [1]
. . [1] "Name"
. . [2] "Bytes"
. . [3] "Size"
. . [4] "#Files"
. . [5] "Most recent"
. [2]
. . [1] "Z:\"
. . [2] 56144659864
. . [3] "52,29 GB"
. . [4] 23855
. . [5] 2016
. [3]
. . [1] "Z:" & SLASH & "BlackBox"
. . [2] 81699302
. . [3] "77,91 MB"
. . [4] 4
. . [5] 2012
. [4]
. . [1] "Z:" & SLASH & "Dying_Gasp"
. . [2] 7757074
. . [3] "7,40 MB"
. . [4] 66
. . [5] 2015
. [5]
. . [1] "Z:" & SLASH & "Temp"
. . [2] 812629032
. . [3] "774,98 MB"
. . [4] 641
. . [5] 2016
analyze_object(read_csv("test.csv", 1), "read_csv", {})
read_csv =
. [1]
. . [1] "Z:\"
. . [2] 56144659864
. . [3] "52,29 GB"
. . [4] 23855
. . [5] 2016
. [2]
. . [1] "Z:" & SLASH & "BlackBox"
. . [2] 81699302
. . [3] "77,91 MB"
. . [4] 4
. . [5] 2012
. [3]
. . [1] "Z:" & SLASH & "Dying_Gasp"
. . [2] 7757074
. . [3] "7,40 MB"
. . [4] 66
. . [5] 2015
. [4]
. . [1] "Z:" & SLASH & "Temp"
. . [2] 812629032
. . [3] "774,98 MB"
. . [4] 641
. . [5] 2016
converts a sequence to a CSV line
Params:CSV separator is a semicolon
CSV strings are surrounded by quotes
puts(1, sequence_to_csv({1,"Two",3,"Four"}) & "\n")
1;"Two";3;"Four"
find the index of a parameter in a list of strings "parameter=value"
Params:list of strings to search in
parameter name to search for
integer: requested index
sequence params
params = {"first=1","second=2","third=three"}
? find_param_name(params, "third")
3
set_param_value(), get_param_value()
find the value of a parameter in a list of strings "parameter=value"
Params:list of strings to search in
parameter name to search for
default value if not found. defaults to ""
sequence: requested value if found, default value if not
puts(1,get_param_value(params, "second") & "\n")
2
find_param_name(), set_param_value()
sets the value of a parameter in a list of strings "parameter=value"
Params:parameter name
parameter value
optional "add" indicates to add value if not already existing else nothing is added
if sequence optional is empty, a parameter value can only be modified
if optional "add" is set, a non-existing parameter will be added
sequence: modified list
sequence params
params = {"first=1","second=2","third=three"}
analyze_object(set_param_value(params, "third", "3", {}), "set_param_value", {})
set_param_value =
. [1] "first=1"
. [2] "second=2"
. [3] "third=3"
analyze_object(set_param_value(params, "fourth", "4", {}), "set_param_value", {})
set_param_value =
. [1] "first=1"
. [2] "second=2"
. [3] "third=three"
("add" is not set so parameter "fourth" is not added)
analyze_object(set_param_value(params, "fourth", "4", {{"add", 1}}), "set_param_value", {})
set_param_value =
. [1] "first=1"
. [2] "second=2"
. [3] "third=three"
. [4] "fourth=4"
find_param_name(), get_param_value()
reads a file structured as an INI file
Params:result is a sequence { {section name, section content}, ... }
each section content is a list of parameters and values { "parameter=value", ... }
file test.ini contains following lines:
[Telnet]
host=127.0.0.1
port=23
[SSH]
host=127.0.0.1
port=22
[FTP]
host=127.0.0.1
port=21
analyze_object(read_ini_file("test.ini"), "read_ini", {})
read_ini =
. [1]
. . [1] "Telnet"
. . [2]
. . . [1] "host=127.0.0.1"
. . . [2] "port=23"
. [2]
. . [1] "SSH"
. . [2]
. . . [1] "host=127.0.0.1"
. . . [2] "port=22"
. [3]
. . [1] "FTP"
. . [2]
. . . [1] "host=127.0.0.1"
. . . [2] "port=21"
write_ini_file()
extracts a section from an INI file content
Params:INI file content
section name
file content has to be read previously with read_ini_file()
sequence: selected section content
ini = read_ini_file("test.ini")
s = get_ini_section(ini, "SSH")
s may be (example):
. [1] "host=127.0.0.1"
. [2] "port=22"
reads an element of an INI file
Params:INI file content
section of ini file to search in
key to search for
default value returned if search fails
file content has to be read previously with read_ini_file()
sequence: searched parameter value or default value if not found
puts(1, search_ini(ini, "FTP", "port") & "\n")
21
read_ini(), write_ini()
reads an element of an INI file
Params:INI file name
default value to return if read failed
puts(1, read_ini("test.ini", "Telnet", "port", -1) & "\n")
23
search_ini(), write_ini()
Delete a file.
Params:name of the file to delete.
integer: 0 on failure, 1 on success.
Copy a file.
Params:name of the file or directory to copy
new name or location of the file
optional "overwrite" set to 0 (the default) will prevent an existing
destination file from being overwritten.
Non-zero will overwrite the destination file.
integer: 0 on failure, 1 on success.
copy_file("test_file_.exw", "test_file_.bak", {})
1 file(s) copied.
move_file(), rename_file()
Move a file to another location
Params:name of the file or directory to move
new location for the file
optional "overwrite" set to 0 (default) prevents overwriting an existing
destination file,
If set to 1, existing destination file is overwritten
If overwrite was requested but the move fails, any existing
destination file is preserved.
integer: 0 on failure, 1 on success.
rename_file(), copy_file()
Rename a file.
Params:name of the file or directory to rename
new name for the renamed file
optional "overwrite" set to 0 (default) prevents renaming if destination file
already exists,
If set to 1, existing destination file is deleted first
If new_name contains a path specification, this is equivalent to
moving the file, as well as possibly changing its name. However, the path
must be on the same drive for this to work.
If overwrite was requested but the rename fails, any existing
destination file is preserved.
integer: 0 on failure, 1 on success.
move_file(), copy_file()
Create a new directory.
Params:name of the new directory to create
* optional "mode" : on Unix systems, permissions for the new directory.
Defaults to 448 (all rights for owner, none for others).
mode is ignored on non-Unix platforms.
* optional "mkparent" : If true (default) the parent directories are also
created if needed.
This is OpenEuphoria 4.1 create_directory()
Return:integer: 0 on failure, 1 on success.
if not create_directory("the_new_folder", {}) then
error_message("Filesystem problem - could not create the new folder", 1)
end if
-- This example will also create "myapp/" and "myapp/interface/"
-- if they don't exist.
if not create_directory("myapp/interface/letters", {}) then
error_message("Filesystem problem - could not create the new folder", 1)
end if
-- This example will NOT create "myapp/" and "myapp/interface/"
-- if they don't exist.
if not create_directory("myapp/interface/letters", {{"mkparent",0}}) then
error_message("Filesystem problem - could not create the new folder", 1)
end if
remove_directory(), chdir()
Remove a directory.
Params:name of the directory to remove
optional "force" set to 1 will also remove files and sub-directories in the
directory. The default is 0, which means that it will only remove the
directory if it is already empty.
integer: on failure, 1 on success.
if not remove_directory("the_old_folder", {}) then
error_message("Filesystem problem - could not remove the old folder", 1)
end if
create_directory(), chdir(), clear_directory()