PDA

View Full Version : Detects if a file exists AND if it's empty


Jayme65
09-23-2006, 11:15 AM
I have to load a text file.
First I want to detect if the file exists or not, so I use "if (rawTxt == undefined)".
Now, if the file exists and contains data, the condition n°2 is met...it's OK!

But now, if the file exists BUT is empty it doesn't meet the condition n°2...why?
It's not "undefined" neither "not undefined" since any trace action accours.

In other words, how can I check that a text file exists...AND that it's empty or not
(so that I can add a branch if that condition is met?)
Try by yourself with an empty text file named "Favorites.ini".

Thanks very much!




var loadFavorite = new LoadVars();
loadFavorite.onData = function(rawTxt:String) {
if (rawTxt == undefined) {
trace("It's undefined");
} else {
trace("It's OK");
}
};
loadFavorite.load("Favorites.ini");

same behavior with an onLoad function

var loadFavorite = new LoadVars();
loadFavorite.onLoad = function(success:Boolean) {
if (success) {
trace("onLoad success")
} else {
trace("onLoad success")
}
};
loadFavorite.load("Favorites.ini");

mooska
09-23-2006, 11:38 AM
var lv = new LoadVars();
lv.onHTTPStatus= function(s){
if(s==404){
trace("the file does not exists")
}
}
lv.onData = function(d){
if(data==undefined){
trace("the file is empty");
}
}
lv.load("http://www.actionscript.org/file.txt");

Jayme65
09-23-2006, 11:55 AM
mooska,

Thnaks very much for this quick and nice reply !!

But...... ;-)

it seems taht I've forgotten one important think...this is a projector file..so it works locally, with a local text file.

Any idea in that case?

Thanks a lot in advance,

Jayme65
09-23-2006, 05:12 PM
Actually, I've understand that if the text is empty, the function won't be called!

For example, this:

var loadFavorite = new LoadVars();
loadFavorite.onData = function(rawTxt:String) {
trace("Hello");
};
loadFavorite.load("Favorites.ini");

...won't trace nothing, the function hasn't been called.

I suppose that this is because there is NO data...so no onData function!

So, how to proceed to include a detection on file is empty in LoadVars function?

Thanks very much by advance,