16 Replies Latest reply on Mar 27, 2007 7:25 PM by Rich Rainbolt

    Can I use file names from some folder to specisfly a value of a variable

    Flamebg
      so here is the thing I want to do:
      I've got like 3 variables in my swf file which value tells to the code how many picture there are to load.
      So I have like
      var totalPics:Number = 5
      in my folder images I have 5 pics, but I want to put 3 more so I want to make suck code that will check the total files in the folder and return a value of 8 or check the name of the last file found in the foulder like pic8.swf (I've made it into swf to load in a loader) and to take that letter 8 from it as a value to put in the variable totalPics so it is = 8.
      Can u tell me is this possible to make in flash.... where I should read helps or guides .. or just give me a simple example please.

      Thank u a lot. If u have any more questions, just ask me..
        • 1. Re: Can I use file names from some folder to specisfly a value of a variable
          Greg Dove Level 4
          Flash can't read the directory, so can't know how many files there are.

          I would approach this one of two ways:

          1. put the names of the swfs you want in an external text file as a variable that you load with loadVars (have a delimiter that you can easily use to split the string into separate files in an array and then load them in sequence). With this approach you have to maintain the external text file if you change the names or number of items you want to load, but the files don't have to following an ordered naming convention.

          2. Alternatively where you have a sequence of images/clips that have a standard name and numeric suffix (e.g. swf1.swf, swf2,swf etc), use MovieClipLoader and when each is loaded (when I did it recently I used the onLoadInit event) initiate another load of the next image/clip by incrementing the number in the filename. Have the MovieClipLoader onLoadError event handle the situation when you get to the end of the sequence (basically your loading is finished when an error occurs because the next requested file doesn't exist). Hint: I found it helpful when creating the listener object for the MovieClipLoader, to have a reference to the parent movie clip (e.g. myListener.parent = this) available so I could access the container clip to create the new empty movie clips there from within the event handler (as targets to load into) as each previous one loaded.
          Hope that makes sense - I've used it to load an unknown number of sequentially numbered jpegs previously.
          • 2. Re: Can I use file names from some folder to specisfly a value of a variable
            Greg Dove Level 4
            So ... I explained the two ways I would consider approaching it... now to specifically answer your question!

            In the first approach, the number of files is the array length created from the split string variable that you loaded (assuming they all subsequently load with no errors).

            In the second approach, the number is calculated as one less than the index of the failed load attempt (onLoadError).

            • 3. Re: Can I use file names from some folder to specisfly a value of a variable
              SymTsb Level 2
              This can be done using LoadVars and some php. PHP will return the exact directory listing of all jpgs and LoadVars sends and loads the request for the data. Once the data is into Flash, you'll have to store it in an array to be able to test length etc. This method works beautifully. Let me know if you need more direction or if php is even an option for you.
              • 4. Can I use file names from some folder to specisfly a value of a variable
                Flamebg Level 1
                Very helpful of you guys ;] Thanks a lot.. so lets continue on the matter..

                GWD the second option is good but can work in a simplier code.. mine is a bit complexed and by that I mean.. that the total number of pics or things to load should be known for the main swf file at his start. That is because before the turn for the pics to load comes, there is something else that must happen first, and it depends on that total number (actually 3 total numbers cuz there are 3 folders with different pics that must mix) So.. can't use the second option cuz it works good when the code is a bit more simple than that.
                If u didn't understand whant I tried to explain.. doesn't matter.. not so important atm.
                Now the thing with the LoadVars.. yes, I've been thinking the same thing but like this: At the 1st frame in the main timeline I have 3 variables that are used further in the code and that must be predefined by how many pics there actually are, so.. I've been thining to make 3 different var1.txt, var2.txt and var3.txt files for example and to put there as value just the number I need. So when I add more pics for example to my images folder, I edit the txt file containing their total number and just change it so the code can automatically add the new pics to the sequence.
                I've got some examples of this how LoadVars works with txt files and I think I can manage to do that, but anyway would be nice of you to give me some example ;] and of course correct me if my toughts above are not true.
                Now.. SumTsb.. what u mentioned, I am interested in, because I have a PHP guy, who can make me a code that automatically counts and gives back the totalPics number from a certain folder (I don't know for sure that it is possible but if it is he can do it). My point is.. When he makes this code.. that reterns value of totalPics, how can I redirect it so it changes the value of totalPics inside my main swf file. I see only this as option at the moment:
                1) when someone adds or deletes files from the folder the php code returns a different value, equal to the number of files in the folder, and automatically changes the content of the file var1.txt like this:
                First off var1.txt has: &content=10 // this is the total number of pics/swf files in the folder;
                when u add.. like 3 more files.. the php code changes the value inside of var1.txt to: &content=13 and when the main swf is started he takes the new value and my problem is solved. Is this what I say as example true ? If it is I guess the only thing left for me is the php programer to make his code and job is done.
                But answer me this question too: is it possible the php code to return the value of the totalPics directly into the main swf file. So if I have inside the main swf a variable at the first frame ot the main timeline called totalPics, can the php give it a value equal to the total numbers of files in a folder, and how ?

                I guess I've written too too much :D so I'll wait what u will answer me now.. I think you understood my idea.. and if more questions arise I will write u back. And if no.. I will just thank you a lot :D
                • 5. Re: Can I use file names from some folder to specisfly a value of a variable
                  Greg Dove Level 4
                  Thanks for your comments. I was thinking more in terms of what flash can do by itself.
                  If you can use PHP then same as what SymTsb said: PHP is definitely the way to go. Sounds like SymTsb can help you out with more details.
                  • 6. Re: Can I use file names from some folder to specisfly a value of a variable
                    Flamebg Level 1
                    thank u too GWD :]
                    I tought about 1 more thing... in the same topic..
                    Can a single swf file use its own name or just a part of it somehow in its own code..

                    for example if the name of the swf is helloThere2You.swf:
                    the code inside of this same swf say like this..
                    in the main timeline at first frame : trace(getTheNameOfThisSwfFile);
                    and it should look like this: //helloThere2You.swf
                    Of course this won't work good enough for me if possible.. but if it is.. maybe I can take just a part of that name.. for example the number 2 from the name of the swf.
                    What can u tell me about this too?
                    thanks in advance ;]
                    • 7. Re: Can I use file names from some folder to specisfly a value of a variable
                      Greg Dove Level 4
                      You need to know the name of the swf in order to load it, but the clip can determine its url (and therefore its filename) via the _url property.

                      See
                      http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/js/html/wwhelp.htm?href=Part4_ASLR2. html

                      BTW the PHP solution should enable you to do the same as what I explained in version one above, except that you obviously don't need to maintain the text file, because PHP is creating the data dynamically.
                      • 9. Re: Can I use file names from some folder to specisfly a value of a variable
                        Rich Rainbolt Level 1
                        I was looking at this thread and it sounds very much like something I would like to know. Would you take the time to explain further how this is done, or if you have a URL etc to a tutorial etc. I would appreciate it very much. Thanks!
                        • 10. Re: Can I use file names from some folder to specisfly a value of a variable
                          Flamebg Level 1
                          SymTsb still waiting for your comment ;-]
                          • 11. Can I use file names from some folder to specisfly a value of a variable
                            SymTsb Level 2
                            Sorry guys. Besides being the Web Admin at work, I'm also the net admin since our IT guy just up and walked. Anyways....

                            I know it can be done with php. I do it already. All the PHP guy will need to do is do a search in a given directory for the jpg extension and push each item returned to an array. Once you have the array of images, you'll need to convert the array to a comma delimited string (a simple foreach loop in php). This isn't necessary 100% but it makes getting the data back into Flash a heck of a lot easier. This is the part that always seems to get people stuck so listen closely and relay this information to your PHP guys. You are going to want to echo the string output. In php.....

                            echo("&images=".$images);

                            ....where $images is the comma delimited string created by the foreach loop.

                            This sets up the php file. Now you need Flash to do something with that data.

                            var sVars = new LoadVars();
                            var rVars = new LoadVars();

                            rVars.onLoad = doSomething;

                            function getImages(){
                            sVars.sendAndLoad("the_php_file.php", rVars, "POST");
                            }

                            function doSomething(){
                            // you need to test for possible errors etc with the data but regardless of all of that, you will need the next line
                            imgName = this.images.split(",");
                            }

                            this.onEnterFrame = function(){
                            getImages();
                            delete this.onEnterFrame;
                            }

                            If you need more help, let me know. I can even show you what I'm using this for so you get a better understanding.
                            • 12. Re: Can I use file names from some folder to specisfly a value of a variable
                              Flamebg Level 1
                              I kind of understand ur idea.. but it's still hard for me to see the big picture with no working example.. maybe you can make a very simple gallery of just a few pics and when we add more pics to a folder to see that they show up in the simple gallery.. (fla, swf and the php file)
                              I think this will be very helpful for all of us here. Just lets us know if u are going to do it for us cuz as u said u've got work to do.. so I don't really wanna waste ur free time.
                              Would be very grateful though :)
                              • 13. Can I use file names from some folder to specisfly a value of a variable
                                SymTsb Level 2
                                Here's the link to the project I'm working on at work right now....

                                EDIT: Link removed

                                Click the logo to enter the site and once inside, go into Seniors.... Click Previews... the test password is 123456789. This sounds similar to what you wanted to do.... All the images are pulled using PHP functions and returned to Flash using code similar to what I had in a previous post.

                                Send me an e-mail mike@sd-dezign.com with further questions. I've got my e-mail running all day. I don't check these forums but maybe once or twice a day. I've got some time (especially since I've already written all the code).
                                • 14. Can I use file names from some folder to specisfly a value of a variable
                                  SymTsb Level 2
                                  Due to the popularity of the topic (at least as far as my Inbox is concerned), I'll be writing up a full tutorial and posting it on my site tonight. I'll post the link later. All the attention is a bit suprising but apparently, I finally did something right in Flash. LOL
                                  • 15. Re: Can I use file names from some folder to specisfly a value of a variable
                                    SymTsb Level 2
                                    Here you go guys. If you have any problems with anything let me know. This should hopefully help those interested see the method I was talking about up above.

                                    http://www.sd-dezign.com/case_study/index.html