A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: Check if file exists

  1. #1
    Ximensions.com Sul's Avatar
    Join Date
    Oct 2000
    Location
    UK
    Posts
    423

    Check if file exists

    Hi

    How do you use actionscript to check if a file in the same folder exists? I want to check if a textfile exists. Thank you.

  2. #2
    Senior Member pellepiano's Avatar
    Join Date
    Feb 2000
    Location
    Stockholm, Sweden
    Posts
    15,151
    Flash can not do that unfortunately. Only thing you can do is to have a special variable in the textfile, and if its not loaded within a certain amount of time, you know the textfile is not there.

    If its for local use you can use the projector software from www.northcode.com

    -Pelle Piano
    // Image Gallery
    www.studiobild.com
    // Photo Blog
    http://talesofthepixel.blogspot.com

  3. #3
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    Originally posted by pellepiano
    Only thing you can do is to have a special variable in the textfile
    Why would you need to do that these days? With the LoadVars class you get a way of handling errors so there is no need to check for a variable within the text file.

    code:

    txtLoader = new LoadVars();
    txtLoader.onData = function(_data_){
    if (_data_) {
    trace("file loaded!\n\n"+_data_);
    } else {
    trace("file not loaded!");
    }
    }
    txtLoader.load("myText.txt");



    For web use, if you wanted to check for files before attempting to load them then you would need to use some server side script (PHP, ASP etc) to test for the files for you.
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  4. #4
    Flashkit Veteran joejoe2288's Avatar
    Join Date
    Apr 2004
    Location
    Hickville, Oregon
    Posts
    2,554
    With PHP you can check to see what files are in the same directory and then write a link to them, it is pretty cool. Only problem is I can't think of the script off the top of my head
    So tired of all the fighting....

    http://joejoe2288.kawanda.net

  5. #5
    Ximensions.com Sul's Avatar
    Join Date
    Oct 2000
    Location
    UK
    Posts
    423
    Thanks

    I know how to do it in PHP, but I'm going to try to see if I can contain it all in flash with the LoadVars class. I'll try it tomorrow, if I have problems I'll report here! Thanks again.

  6. #6
    Senior Member pellepiano's Avatar
    Join Date
    Feb 2000
    Location
    Stockholm, Sweden
    Posts
    15,151
    Thats good to know Lexicon.

    However the code you supplied does not seem to work. It gives a "File not loaded" wether there is a textfile present or not. (which loads fine with old loadVariables though ).

    -Pelle Piano
    // Image Gallery
    www.studiobild.com
    // Photo Blog
    http://talesofthepixel.blogspot.com

  7. #7
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    Thanks for pointing that out Pel.

    It works fine in MX2004 exported as flash7 AS2... but not in earlier versions.

    However you can use this similar code in earlier versions....

    code:

    txtLoader = new LoadVars();
    txtLoader.onLoad = function(_data_){
    if (_data_) {
    trace("file loaded!\n\n");
    } else {
    trace("file not loaded!");
    }
    }
    txtLoader.load("myText.txt");



    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  8. #8
    Senior Member hanszorg2's Avatar
    Join Date
    Sep 2004
    Posts
    308
    lexicon can you post a sample fla in flash mx, it would be great help for me in understanding

  9. #9
    Senior Member pellepiano's Avatar
    Join Date
    Feb 2000
    Location
    Stockholm, Sweden
    Posts
    15,151
    Thanks for that. It works great. It's now saved in my snippet code folder, and Im one good trick wiser.

    -Pelle Piano
    // Image Gallery
    www.studiobild.com
    // Photo Blog
    http://talesofthepixel.blogspot.com

  10. #10
    Flashkit Veteran joejoe2288's Avatar
    Join Date
    Apr 2004
    Location
    Hickville, Oregon
    Posts
    2,554
    Well, you can contain it all to flash except one php page which you could load into your swf with loadMovieNum("yourphppage.php",0); and then the user will never know you loaded it
    So tired of all the fighting....

    http://joejoe2288.kawanda.net

  11. #11
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    Sul, there is no need to send you a sample fla as everything I would put in the fla is in the post. Just copy and paste the code into a frame and make a text file in the same directory (or leave the text file out) and the jobs a goodun.


    Originally posted by joejoe2288
    Well, you can contain it all to flash except one php page which you could load into your swf with loadMovieNum("yourphppage.php",0); and then the user will never know you loaded it
    I assume you mean loadVariables() or loadVariablesNum() joejoe, and rather than loadMovieNum()

    In any case it is now recommended to use the loadVars class to load the php data, but whatever floats your boat, loadVariables() does still work.
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  12. #12
    Ximensions.com Sul's Avatar
    Join Date
    Oct 2000
    Location
    UK
    Posts
    423
    I didn't ask for the sample .fla it was hanszorg2 Thanks for your help.

    I tried the LoadVars method but it's just not working. Here is what I have:

    code:

    var arrTypingText = new Array();
    mLV = new LoadVars();
    myLV.load("typingtext.txt");
    myLV.onLoad = function(success:Boolean)
    {
    if (success)
    {
    arrTypingText = myLV.extTypingText.split("-split-");
    gotoAndPlay(2);
    }
    else
    {
    arrTypingText[0] = "HELLO";
    gotoAndPlay(2);
    }
    }



    Then on frame 5 or so I do:

    trace(myLV.extTypingText);

    This returns undefined, which means its not reading the extTypingText variable from the textfile!

  13. #13
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    code:

    var arrTypingText = new Array();
    // check spelling of myLV !!!
    myLV = new LoadVars();
    // declare the onLoad function before loading!
    myLV.onLoad = function(success:Boolean) {
    if (success) {
    arrTypingText = myLV.extTypingText.split("-split-");
    gotoAndPlay(2);
    } else {
    arrTypingText[0] = "HELLO";
    gotoAndPlay(2);
    }
    trace("!! " + arrTypingText);
    };
    myLV.load("yak.txt");

    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  14. #14
    Ximensions.com Sul's Avatar
    Join Date
    Oct 2000
    Location
    UK
    Posts
    423
    *Kick myself*

    Thanks! It's much easier for others to pick up spelling errors, and something often overlooked!

  15. #15
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    Aye, fresh eyes always help. Enjoy
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center