How to check if file exists in Eiffel

feature 

    open_file_sample
        local
            l_file: UNIX_FILE_INFO
            l_path: STRING
        do
            make
            l_path := "/var/log/syslog"
            l_file.update (l_path)
            if l_file.parent_directory.exists and then l_file.parent_directory.is_writtable then
                create l_file.make
            end
             -- AS the above statement doesn't exist!
            check
                syslog_file_exists_and_is_readable: l_file.exists and then l_file.is_readable
            end
        end

Is this the proper way to check for file existence in Eiffel?

I was wondering if there is a way not to create 2 objects. I'll complete my check with following statement:

  • define path `l_file_path := "/some/path/with_file.log"
  • check if parent directory exists and has rights to write into
  • create log file

You can use {FILE_UTILITIES}.file_exists (the_file_name)

or (create {RAW_FILE}.make_with_name (the_file_name)).exists

How to check if file exists in Eiffel, (Uses effective UID to check that parent is writable -- and file does not exist.) local external_name: ANY do external_name := name.to_c Result := file_creatable  Files, input, output. A few classes of the Kernel Library support file manipulation, input and output: STD_FILES, FILE, DIRECTORY and UNIX_FILE_INFO.For simple applications it suffices to use STD_FILES, but to understand the concepts better it is preferable to look first at the other two.

The problem when accessing the file system is that the property of a file or directory may have changed between the time you query it and the time you want to use it (even if it's only a small fraction of a second). Because of that, assertions in Eiffel of the form:

f (a_file: RAW_FILE)
    require
        a_file.is_writable
    do
        a_file.open_write

may be violated. In the Gobo Eiffel libraries, instead of checking whether a file can be opened in write mode before actually opening it, the revert approach was chosen: try to open the file, and check whether it was opened successfully.

f (a_pathname: STRING)
    local
        l_file: KL_TEXT_OUTPUT_FILE
    do
        create l_file.make (a_pathname)
        l_file.recursive_open_write
        if l_file.is_open_write then
            -- Write to the file.
            l_file.close
        else
            -- Report the problem.
        end

Note that it uses recursive_open_writeand not just open_write so that missing directories in the path get created as well.

PLAIN_TEXT_FILE Flat, Being a type system freak, I like to point out that some classes of errors could really could be avoided if nonnull (nonvoid in Eiffel lingo) types were introduced. However, even if we check for the file's existence upfront, we've only partially  This is unlikely, since the installation procedure correctly sets up the permissions, but if you feel the library is there you may want to check whether the permissions may have been changed inadvertently.) Fix. If you don't have a C compiler, any serious use of Eiffel requires a precompiled library.

do
  if not l_file.exists then
    print ("error: '" + l_path + "' does not exist%N")
  else
    ...

You can something similar to this

FILE Text, when enhancements to the type system are implemented. could be avoided if nonnull (nonvoid in Eiffel lingo) types were introduced. However, even if we check for the file's existence upfront, we've only partially mitigated the problem. Hi all, Let me explain the situation: I need to search for a file name in a folder where are many different files. For example a file like this: “All Data Export_1_spend_3_2018” Find that specific file and than move that specific file to a different folder. The problem is that the file changes name every month AND every year (when I download the file): For April will be: All Data Export_1

My final solution is following, and is subject to critics, I personnaly find it very complicated in comparison to more low level languages and libs (as bash for ex)

log_file_path: detachable PATH
        -- Attached if can be created
    local
        l_file: UNIX_FILE_INFO
        l_path, l_parent_dir: PATH
        l_fu: FILE_UTILITIES
    do
        create l_fu
        -- Parent directory check
        create l_path.make_from_string ({APP_CONFIGURATION}.application_log_file_path)
        l_parent_dir := l_path.parent
        if not l_fu.directory_exists (l_parent_dir.out) then
            l_fu.create_directory_path (l_parent_dir)
        end
        create l_file.make
        l_file.update (l_parent_dir.out)
        if not l_file.exists or
            l_file.is_access_writable
        then
            io.putstring ("Error: " + log_file_path_string + " parent directory is not writtable and cannot be created")
            check
                parent_dir_exists_and_is_writtable: False
            end
        else
            Result := l_path
        end
    ensure
        file_name_could_be_created: Result /= Void
    end

C# 5.0 Unleashed: C 5.0 Unleashed, These constructs test the equality of references—not the underlying object values Say that you got an Eiffel object from a file or from a separate Eiffel process. utilize or override ANSI prototypes that may exist for the external C function. Check if File Exists When checking if a file exists, the most commonly used FILE operators are -e and -f. The first one will check whether a file exists regardless of the type, while the second one will return true only if the FILE is a regular file (not a directory or a device).

C# 4.0 Unleashed, (Uses effective UID to check that parent is writable -- and file does not exist.) support_storable: BOOLEAN -- Can medium be used to store an Eiffel object? Iterating on a LIST. There are three Eiffel mechanisms to iterate on every element of a LIST. the across loop. The across can be used on every ITERABLE object (including LIST objects). print_elements (a_list: LIST[INTEGER]) -- Print every elements on `a_list` do across a_list as ic loop print (ic.item.out + "%N") end end

The Handbook of Software for Engineers and Scientists, If it does not exist, the user is asked to give the names of the root class and creation This file is monitored by the run time system to check which assertions are  C file exists function using fopen () function In the first approach, we will try to read the data from the file using the fopen () function. If we can read data from the file, it means the file exists otherwise it does not. The following is the C file exists function to check if a file exists using the fopen () function.

file Flat contracts, Hi, I am new to Eiffel Programming, i need to know a way to read some values from a text file. I herd of an Now, assuming the file exists on disk at that location​, open operations so that I check to see if the file is accessible) Eiffel programming language syntax. The syntax specification shown here is a less complete and less formal version of that which is in the Eiffel ISO/ECMA standard document. The format is BNF-E. The Language Specification section of the standard document includes an overview of BNF-E.

Comments
  • side comment, see difference between is_access_readable and is_readable .
  • Thx this gives me the way with {FILE_UTILITIES}
  • Actually could be nice to have something like dirname on linux which gives the parent directory of given file, so not having to define in 2 parts a file and its parent folder....
  • I would recommend to use the class PATH , it handles Unicode, parent folder, and so on.
  • where does your file object come from?
  • sry meant to do l_file.exist
  • replace l_fu.directory_exists (l_parent_dir.out) by l_fu.directory_path_exists (l_parent_dir) Why do you use UNIX_FILE_INFO ? You should use directly RAW_FILE