Number of files in a folder

If I want to determine how many files there’s in a folder, how could this be done?
Let’s say I’ve got 10 swf’s in a folder. Then I want to send the number 10 to flash, where I later on will use this number. Is it possible to do this?

It is possible with PHP.

Are you just checking for .swf files or all files? (or will there just be .swf files in there)

There will only be one kind of files in there, so I’m only checking for swf’s. Do you know how to do it with PHP?

<?php
$directory = "files";
$fileType = "swf";
$i = 0;
if ($handle = opendir($directory)) {
    while (false !== ($file = readdir($handle))) { 
		if (stristr($file, $fileType)) $i++;
	}
	echo "numberOfFiles=" . $i;
	closedir($handle);
}
?>

That should get the amount of .swf files in the directory.

Communicating that to Flash is not my area so I can’t really help much there.

NOTES:

This should be saved in a .php file

The directory you want to read should be put as the value for the $directory variable.

The file type of the files you are looking for should be put as the value for the $fileType variable. (without the “.”)

Ok thanks! =D I’ll see what I can do about this.
If I for some silly reason would like to place this phpfile into the same folder as the swf’s, what would $directory be then?

The directory would be “.” So it would be

$directory = ".";

And note: I made a few changes to the above code if you haven’t seen it. I made it easier to change file type to search for and I forgot to add a closedir() :x

I know for a fact you can get a count of every file in a directory as well as the filename with PHP. I would assume you could pass that info off to flash, but I’ve never done anything with flash and PHP. m_andrews808 was right, SSS forum. well, thinking about it… with proper naming you MIGHT be able to do something like this…


x=0;
do {
x++;
loadMovie("buddy"+x+".swf",x);
} while(there is no error);

I don’t know how to do the error checking, but it will let you do the loadMovie(“buddy”+x+".swf",x); if you have a file named “buddy0.swf” so you could loop it and have “buddy1.swf” and “buddy2.swf.” I started trying to figure something out and I learned that much anyway… but alas I must go to class now… but it’s a point in the right direction I suppose.

edit: well, i started typing this before lostininbeta answered your question more or less, then I toyed with it for about half an hour… oh well.

APD: That would require you name all your .swf files the same thing just with an incremented number. Not to mention it would require you load in all the movies at once.

:x

yeah I know, but you could expand on it if you wanted to… you wouldn’t have to make it load all the movies at once, you could just make it count the number until there is an error and then bulid some buttons based on that number. Just a suggestion :frowning:


<?
function parse_dir($dir,$level){
	$dp=opendir($dir);
	while (false!=($file=readdir($dp))){
		if ($file!="." && $file!=".."){
			$countervar=$countervar+1;
			if (is_dir($dir."/".$file)) parse_dir($dir."/".$file,$level+1);
			else print "&path".$countervar."=".$dir."/".$file;
print "&numOfFiles=".$countervar;
		}
	}
}

$start_dir="dirNameGoesHere";  
$level=1;

parse_dir($start_dir,$level);
?>


That would give you a list of variables that are sequentialy numbered containing the file names, and it would give you a count of the files in the directory.
then in flash do something like:
on the first frame of the movie
[AS]
loadVariablesNum(“TheNameOfThe.php”,0);
[/AS]
the following can be anywhere in the flash
[AS]
for(i=1;i<numOfFiles;i++){
//load files into holders
loadMovie(_root[“path”+i], _root[“holder”+i]);
}
[/AS]

You will probably have to adapt most of the code and fix several little bugs in the AS part, but you should get the idea. The php give flash the list of files in this format
&path1=fileName.ext&path2=somethingElse.swf

Well I hope that will help a little, or maybe just get you on the right track.

Good Luck,
Shawn Harte

Thanks for your reply paradox. Really nice! :smiley:

Ok, I’ve walked into the wall again. Maybe you could help me. I’ve got this script in the first frame:[AS]for (i=1; i<_root.numberOfFiles; i++) {
duplicateMovieClip(“knapp”, “knapp”+i, i);
this[“knapp”+i]._x = knapp._x + knapp._widthi2;
this[“knapp”+i].onRelease = function() {
_root.laddabild.loadMovie(“bilder/”+i+".swf");
};
}
[/AS]

This will result in that ALL buttons (knapp1, knapp2, knapp3 and so on) will have the same onRelease code on them. Let’s say numberOfFiles = 5, then all the buttons will load 5.swf when clicked.
I’m on the right way, but right now I’m stuck. Could you help me to make knapp1 be linked to 1.swf, knapp2 to 2.swf and so on?

Thanks

Common mistake, simple fix…

Within the dynamic event handler you cannot target “i”, it will always get the last number “i” produces. So what you ahve to do is create a variable inside the object and target that. Example… [AS]for (i=1; i<_root.numberOfFiles; i++) {
duplicateMovieClip(“knapp”, “knapp”+i, i);
this[“knapp”+i]._x = knapp._x + knapp._widthi2;
this[“knapp”+i].targNum = i;
this[“knapp”+i].onRelease = function() {
_root.laddabild.loadMovie(“bilder/”+this.targNum+".swf");
};
}[/AS]

And because i’m such a nice guy I will let you in on a tip (lol, j/k about the conceitedness). You can assign an ID to your dynamically created clip to make it easier to target. Example [AS]for (i=1; i<_root.numberOfFiles; i++) {
mc = duplicateMovieClip(“knapp”, “knapp”+i, i);
mc._x = knapp._x + knapp._widthi2;
mc.targNum = i;
mc.onRelease = function() {
_root.laddabild.loadMovie(“bilder/”+this.targNum+".swf");
};
}[/AS]

Thanks Lost! :slight_smile: Works fine now. Really nice! :smiley:
Couldn’t get the second part of code to work though.
Won’t that create multiple instances of the "knapp"mc, name them all to mc which will make only one visible. The other will be replaced. If not so, please explain why :slight_smile:
You’re the man, as always! :smiley:

I still have one problem. I guess it’s not the right thread to talk about it, but it’s the same fla, so I’ll continue here. Maybe it’s related to the code above.

Take a look at this: test.swf
Why isn’t the rolloverstate work on the lower squares?
Take a look at the fla: test.fla

mc is nothing but an ID, it basically IS this[“knapp”+i] since the value of it is the the value your duplicated clip returns hence… [AS]mc = duplicateMovieClip(“knapp”, “knapp”+i, i);[/AS]

If you want to see, just run [AS]trace(mc);[/AS] right after the duplicateMovieClip code, it will return the newly created clip.

Cool… But I can only get it to work if I change the script to:
[AS]duplicateMovieClip(“knapp”, “knapp”+i, i);
mc = this[“knapp”+i];[/AS]
Should your code work? Hmm… Well anyway… It works as it should…
And… If you didn’t notice I added another question up there…

Really? That’s odd. Try writing it like this [AS]mc = knapp.duplicateMovieClip(“knapp”+i, i);[/AS] Notice the target clip is outside of the parenthesis, this is how I always write it and have never had a problem, I wonder if theirs a difference.

I’ve been playing around with this and can’t seem to get it to work. The text box displays the correct number of files in the directory, but the duplicateMovieClip() doesn’t work. If I hard code the variable, it does. Can someone tell me what I’m doing wrong? Thanks in advance.

<? 
function parse_dir($dir,$level){ 
$dp=opendir($dir); 
while (false!=($file=readdir($dp))){ 
if ($file!="." && $file!=".."){ 
$countervar=$countervar+1;
if (is_dir($dir."/".$file)) parse_dir($dir."/".$file,$level+1);
else print "&path".$countervar."=".$dir."/".$file;
print "&numberOFfiles=".$countervar;
}
}
}
$start_dir="thumbs";
$level=1;
parse_dir($start_dir,$level);
?> 

loadVariablesNum("thumbcounter.php", 0);
for (i=1; i<_root.numberOfFiles; i++) {
duplicateMovieClip("box", "box"+i, i);
this["box"+i]._x = box._x+box._width*i*2;
}

You should probably try using loadVars() so you can detect when the data is fully loaded in (since you can’t access the data until it is fully loaded).

myLV = new LoadVars();
myLV.onLoad = function(success){
    if (success){
        for (i=1; i<_root.numberOfFiles; i++) {
            duplicateMovieClip("box", "box"+i, i);
            this["box"+i]._x = box._x+box._width*i*2;
        }
    } else {
        trace("file failed to load");
    }
}
myLV.load(thumbcounter.php);

Or something along those lines (a bit tired, just didn’t want to forget about this, so I figured i’d throw something out there).
}

Sorry about not replying, luckily lost was here to help…
as far as I know the variable names are case sensitive, and I have a typo in my code, numberOFfiles in the php and numberOfFiles in flash…the Ff vs fF could be making all the difference and causing the duplicateMovie hassel. If not I will try to help, I should have access to a computer for the next couple of days so hopefully we can get this worked out before I leave again.
Also, I would go with the loadVars method, I basically just discovered this method and it is quickly becoming my favorite as you have way more information about variables being loaded into flash.

paradox244, It wasn’t the case sensitivity. I made sure that they were identical and it still didn’t work.

I tried your code there lostinbeta, but didn’t have any “success”. I’m at work right now, but I’ll keep trying when I get home this evening. If you guys have any other ideas in the meantime, please share. Thanks guys.