1. file check

Does anyone know a simple way to check if a file already exists in euphoria? I am working on a project where, when the user saves data, I want the program to at least alert the user that an existing file of the same name will be overwritten if they use a particular file name.

Thanks, Asa.

new topic     » topic index » view message » categorize

2. Re: file check

Try using the dir() function, which is often used to check if a file exists.

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

3. Re: file check

Thanks for the quick reply. It looked daunting at first but now I look at it again, I see it wouldn't be hard to iterate through the returned sequence and match a string. That's what makes Euphoria such a pleasure to use. I shudder at the thought of writing my current game project in C using the STL etc. :)

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

4. Re: file check

No need to scan through the returned data. Try this ...

include file.e 
function file_exists(sequence filename) 
    return sequence(dir(filename)) 
end function 
 
if file_exists("somefile.xyz") then 
  . . . 
end if 
new topic     » goto parent     » topic index » view message » categorize

5. Re: file check

I benchmarked three different methods of checking the existence of a file:

function file_exists_dir(sequence filename) 
	return sequence(dir(filename)) 
end function 
 
function file_exists_open(sequence filename) 
	integer a = open(filename, "r") 
	if a = -1 then 
		return 0 
	end if 
	close(a) 
	return 1 
end function 
 
constant lib = open_dll("kernel32.dll"), filea = "parser.e", fileb="no.txt" 
constant f_access = define_c_func(lib, "GetFileAttributesA", {C_POINTER}, C_INT) 
function file_exists_win(sequence filename) 
	atom pFilename = allocate_string(filename) 
	integer r = c_func(f_access, {pFilename}) 
	free(pFilename) 
	return r > 0 
end function 

file_exists_dir() and file_exists_open() were about the same. file_exists_win() was much faster.

With 2376 files (C:\Windows\System32):

Time dir : 0.000129 per 
Time open: 0.000126 per 
Time win : 0.000033 per 

With 1 file (mocked directory)

Time dir : 0.000044 per 
Time open: 0.000034 per 
Time win : 0.000028 per 

The test looked for one file that did exist and one file that did not. This is a common enough operation that I will make a function called file_exists and place it in std/filesys.e for 4.0. On Unix we will use the C function access(). On DOS, it will revert to the dir() or open() method.

Jeremy

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

6. Re: file check

canadiancoder said...

Does anyone know a simple way to check if a file already exists in euphoria? I am working on a project where, when the user saves data, I want the program to at least alert the user that an existing file of the same name will be overwritten if they use a particular file name.

Thanks, Asa.

integer fh, rc 
while 1 do 
fh = open(file_to_write,"r") 
if fh != -1 then 
   close(fh) 
   rc = prompt_number("file_to_write & " already exists. Overwrite?\n", {0,1}) 
   if rc=0 then 
      exit 
   end if 
end if 
fh = open(file_to_write,"w") 
print(fh, your_text) 
close(fh) 
exit 
end while 

There will be simpler ways in 4.0.

CChris

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

7. Re: file check

jeremy said...

I benchmarked three different methods of checking the existence of a file:

function file_exists_dir(sequence filename) 
	return sequence(dir(filename)) 
end function 
 
function file_exists_open(sequence filename) 
	integer a = open(filename, "r") 
	if a = -1 then 
		return 0 
	end if 
	close(a) 
	return 1 
end function 
 
constant lib = open_dll("kernel32.dll"), filea = "parser.e", fileb="no.txt" 
constant f_access = define_c_func(lib, "GetFileAttributesA", {C_POINTER}, C_INT) 
function file_exists_win(sequence filename) 
	atom pFilename = allocate_string(filename) 
	integer r = c_func(f_access, {pFilename}) 
	free(pFilename) 
	return r > 0 
end function 

file_exists_dir() and file_exists_open() were about the same. file_exists_win() was much faster.

With 2376 files (C:\Windows\System32):

Time dir : 0.000129 per 
Time open: 0.000126 per 
Time win : 0.000033 per 

With 1 file (mocked directory)

Time dir : 0.000044 per 
Time open: 0.000034 per 
Time win : 0.000028 per 

The test looked for one file that did exist and one file that did not. This is a common enough operation that I will make a function called file_exists and place it in std/filesys.e for 4.0. On Unix we will use the C function access(). On DOS, it will revert to the dir() or open() method.

Jeremy

DOS 5.0+ I believe has an int #21 call to see if a file exists, it may be jyst as fast. Look for high numbers (between #60 and #6F). I may be able to check later.

CChris

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

8. Re: file check

CChris said...
jeremy said...

I benchmarked three different methods of checking the existence of a file:

function file_exists_dir(sequence filename) 
	return sequence(dir(filename)) 
end function 
 
function file_exists_open(sequence filename) 
	integer a = open(filename, "r") 
	if a = -1 then 
		return 0 
	end if 
	close(a) 
	return 1 
end function 
 
constant lib = open_dll("kernel32.dll"), filea = "parser.e", fileb="no.txt" 
constant f_access = define_c_func(lib, "GetFileAttributesA", {C_POINTER}, C_INT) 
function file_exists_win(sequence filename) 
	atom pFilename = allocate_string(filename) 
	integer r = c_func(f_access, {pFilename}) 
	free(pFilename) 
	return r > 0 
end function 

file_exists_dir() and file_exists_open() were about the same. file_exists_win() was much faster.

With 2376 files (C:\Windows\System32):

Time dir : 0.000129 per 
Time open: 0.000126 per 
Time win : 0.000033 per 

With 1 file (mocked directory)

Time dir : 0.000044 per 
Time open: 0.000034 per 
Time win : 0.000028 per 

The test looked for one file that did exist and one file that did not. This is a common enough operation that I will make a function called file_exists and place it in std/filesys.e for 4.0. On Unix we will use the C function access(). On DOS, it will revert to the dir() or open() method.

Jeremy

DOS 5.0+ I believe has an int #21 call to see if a file exists, it may be jyst as fast. Look for high numbers (between #60 and #6F). I may be able to check later.

CChris

Not correct. Instead cal int #21 with AX=#4E00, CX=0, DS:DX-> ASCIZ file name (wildcards accepted). This will only attempt to find the file., so it should be fast.

On return, CF is set if not found, clear if found.

CChris

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

Search



Quick Links

User menu

Not signed in.

Misc Menu