Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
0 registered members (), 1,103 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
file_exists return value and subfolders #461598
08/12/16 23:12
08/12/16 23:12
Joined: Jul 2002
Posts: 3,208
Germany
Error014 Offline OP
Expert
Error014  Offline OP
Expert

Joined: Jul 2002
Posts: 3,208
Germany
Hey,

this might as well be a "Blame the manual", but it had me confused for a while.

When passing a filename including a relative folder, file_exists reports the file as existing even if it does not actually exist there, but in the main folder.

For example, say you have the file "onlyhere.txt" in your mainfolder, and a folder called "folder" that does not have a file "onlyhere.txt".
To summarize, a "test.dat" no exists in both folders, "doesnotexist.dat" exists in no folders, and "onlyinsub.dat" exists only in the subfolder, (but no the main folder) whereas "onlyhere.dat" exists only in the main folder (but not the subfolder).

Then

Code:
if(file_exists("onlyhere.txt"))



fires as expected, but so does

Code:
if(file_exists("folder\\onlyhere.txt"))



and that is not what I expected.


What exactly does file_exists return when a file, well, exists? Is there a dependance on subfolders, even if that folder is given in the filename?

Because the following condition does not fire:

Code:
if(file_exists("onlyhere.txt")==1)



which is contrary to what I'd have expected from the manual.

-----

CODE FOR TESTING

You'll need to setup and create a few dummyfiles.
Put "test.dat" and "onlyhere.dat" in your main folder, and create a new folder there called "folder". In "folder", also place a "test.dat", and a "onlyinsub.dat".
Then, run this code:

Code:
void main() {
	var fe;

/*
	A ton of tests for file_exists
*/
	diag("\n - NORMAL - \n");	
	fe = file_exists("test.dat");
	diag(str_for_num(NULL,fe));
	if(file_exists("test.dat")) {
		diag(" - exists\n");
	} else {
		diag(" - does not exist.\n");	
	}

	fe = file_exists("doesnotexist.dat");
	diag(str_for_num(NULL,fe));
	if(file_exists("doesnotexist.dat")) {
		diag(" - exists\n");
	} else {
		diag(" - does not exist.\n");	
	}
	
	fe = file_exists("onlyinsub.dat");
	diag(str_for_num(NULL,fe));
	if(file_exists("onlyinsub.dat")) {
		diag(" - exists\n");
	} else {
		diag(" - does not exist.\n");	
	}
	fe = file_exists("onlyhere.dat");
	diag(str_for_num(NULL,fe));
	if(file_exists("onlyhere.dat")) {
		diag(" - exists\n");
	} else {
		diag(" - does not exist.\n");	
	}
	
	
	
	
	diag("\n - SUBFOLDER - \n");

	fe = file_exists("folder\\test.dat");
	diag(str_for_num(NULL,fe));
	if(file_exists("folder\\test.dat")) {
		diag(" - exists\n");
	} else {
		diag(" - does not exist.\n");	
	}

	fe = file_exists("folder\\doesnotexist.dat");
	diag(str_for_num(NULL,fe));
	if(file_exists("folder\\doesnotexist.dat")) {
		diag(" - exists\n");
	} else {
		diag(" - does not exist.\n");	
	}

	
	fe = file_exists("folder\\onlyinsub.dat");
	diag(str_for_num(NULL,fe));
	if(file_exists("folder\\onlyinsub.dat")) {
		diag(" - exists\n");
	} else {
		diag(" - does not exist.\n");	
	}
	fe = file_exists("folder\\onlyhere.dat");
	diag(str_for_num(NULL,fe));
	if(file_exists("folder\\onlyhere.dat")) {
		diag(" - exists\n");
	} else {
		diag(" - does not exist.\n");	
	}
	
	diag("\n - COMPARED TO VALUE -\n");

	fe = file_exists("test.dat");
	diag(str_for_num(NULL,fe));
	if(file_exists("test.dat")==1) {
		diag(" - exists\n");
	} else {
		diag(" - does not exist.\n");	
	}

	fe = file_exists("doesnotexist.dat");
	diag(str_for_num(NULL,fe));
	if(file_exists("doesnotexist.dat")==1) {
		diag(" - exists\n");
	} else {
		diag(" - does not exist.\n");	
	}	

	sys_exit("");
	
}



This is the relevant part of acklog.txt after I run it. Values that seem strange I have commented.

Quote:

- NORMAL -
1 - exists
0 - does not exist.
0 - does not exist.
1 - exists

- SUBFOLDER -
1 - exists
0 - does not exist.
1 - exists
1 - exists //should be 0 - does not exist!!

- COMPARED TO VALUE -
1 - does not exist. //should be 1 - exists
0 - does not exist.





Perhaps this post will get me points for originality at least.

Check out Dungeon Deities! It's amazing and will make you happy, successful and almost certainly more attractive! It might be true!
Re: file_exists return value and subfolders [Re: Error014] #461635
08/15/16 10:24
08/15/16 10:24
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
Yes. The file search order by the engine is: file buffer - given folder - Path list - WRS archive.

When you want to only check a file in a particular folder, use standard Windows functions like _stat32(). I'll make this more clear in the manual.


Moderated by  jcl, Nems, Spirit, Tobias 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1