View Full Version : while loop

01 January 2004, 09:48 PM
Hello, I've got some problems with a while loop; It loops for ever. The loop is suposed to print the content of some files. Since the script don't know how many files there are, it checks if the next file exists or not inside of the loop. If the next file does not exist, it is going to stop:


int $counter = 1;
string $fileName = "example" + $counter + ".txt";
$exampleFileName[$counter] = ( `internalVar -userScriptDir` + $fileName );
int $fileExist = `filetest -f $exampleFileName[$counter]`;

while(0 < $fileExist)
$fileId=`fopen $exampleFileName[$counter] "r"`;
string $s;
$s=`fread $fileId $s`;
print( $s + "\n" );
fclose $fileId;

$counter = $counter + 1;
$fileName = "example" + $counter + ".txt";
$exampleFileName[$counter] = ( `internalVar -userScriptDir` + $fileName );
int $fileExist = `filetest -f $exampleFileName[$counter]`;


01 January 2004, 10:28 PM
the loop is crashing because 0<$fileExists...

zero is NEVER less than $fileExists... the smallest $fileExists ever gets is zero, and zero is NOT less than zero. its equal to it.

i would personally just use:

while( $fileExists ) {

also, why don't you determine the file list before you go into the while loop, and then just use a for loop instead? at least this way, you can give the user a progress bar to indicate how the script is progresssing.

something like this:

string $files[] = `getFileList -folder (internalVar("-userScriptDir") -filespec "example*.mel"`;
for( $file in $files ) {
$fileId=`fopen $file "r"`;

this is much easier... you should also make a progress window for the script as well...

CGTalk Moderation
01 January 2006, 06:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.