1. file exists function

is there a function in euphoria to determine if a file exists in a 
specified directory?

new topic     » topic index » view message » categorize

2. Re: file exists function

efapon4 wrote:

> is there a function in euphoria to determine if a file exists in a 
> specified directory?

function exists(sequence filename)
    -- filename can contain the whole path if required
    integer fh
    fh = open(filename, "rb")
    if fh != -1 then
        close(fh)
        return 1
    end if
    return 0
end function

HTH,
Carl

-- 
Carl R White - aka - Cyrek
eMail: carlw at legend.co.uk
       cyrek at bigfoot.com
URL:   nope none nada zip

new topic     » goto parent     » topic index » view message » categorize

3. Re: file exists function

efapon4 at juno.com wrote:
> 
> is there a function in euphoria to determine if a file exists in a
> specified directory?
> 

The dir() function will tell you: If the file doesn't exist it will
return the atom '-1' otherwise a sequence containing file information,
use: help, library, dir().

Have a nice day, Rolf

new topic     » goto parent     » topic index » view message » categorize

4. Re: file exists function

there are 2 ways to see if a file exists all depending on your intentions. 

Example 1: if you want to know if it exists for copying or moving or something
then use this. Note: this function works to check both files and also directories
themselves.

constant false = 0, true = not false

function exists(sequence filename)
-- return true or false if file or directory is found
	object temp_object
	temp_object = dir(filename)
	if atom (temp_object) then
		return false
	else
		return true
	end if
end function

usage:
if exists(filename) then
	-- good code goes here
else
	-- nasty message goes here
end if

also works for:
if exists("c:\\temp\\") then
	-- good code goes here
else
	-- nasty message goes here
end if

Example 2: if you are planning on opening the file if it does exist then use
something like this. this method looks more complex but is actually much more
efficient than Example 1. at least in my world a dir() takes MUCH more time to
execute than open():

constant false = 0, true = not false

function open_if_exists(sequence filename)
-- open file if it exists and pass back file number
-- if file doesn't exist then pass back false (0)
	atom temp_atom
temp_atom = open(filename,"r") -- change the open attributes according to your
needs
	if temp_atom < 1 then
		return false
	else
		return temp_atom
	end if
end function

usage: atom file_handle
file_handle = open_if_exists(filename)
if file_handle then
	-- good code goes here
else
	-- nasty message goes here
end if

there are probably more elegant ways to do it, but these 2 methods work for me.
hope it helps!

~OD~


Windows 95 Error 23042: Virus error - A virus has been activated in a DOS-box.
The virus,  however, requires Windows. All tasks will automatically be closed 
and the virus will be activated again.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu