Page 1 of 1

recursive scanning of files in a folder

Posted: Tue Nov 24, 2009 7:22 pm
by shadowslash
hi all,

i'm in deep loss as to how i can make a simple system where it parses each files inside a folder one by one. i need recursion in this such as the example below:

Code: Select all

,-My Folder to Scan
|-- File 1
|-- File 2
|-- File 3
|-- Another Folder
    |-- File 4
    |-- File 5
    '-- File 6
|-- Folder No. 2
    |-- File 7
    '-- File 8
... and so on.
see, when i set "My Folder to Scan" as the folder to be scanned, there should be a handler like the one below that should be called for each file inside the set folder as well as all the other folders under it, so called recursive scanning.

Code: Select all

on doSomething pFile
   // My Code here
end doSomething

pFile is supposed to contain the full path to one of the files inside the chosen folder. this continues on until all the files in the said folder have been parsed.
let's just say this system is to check each and every file inside a set folder.
i tried the example located at the Resource Center of Revolution. it works but it uses repeat loops and repeat loops are often times uninterruptable by, e.g.: clicking a cancel button to stop the scanning. so i thought doing it via dispatch or send command would be better but Lord knows i've tried my best to attempt converting the sample found in the Resource Center about recursion in Files and Folders and still haven't been able to make it work. i seem to get overwhelmed by so much functions and commands! Image can anybody help me with this one? if anybody has already made a library stack for this sort, would you please share it with me? i'm at my wit's end here to some extent. Image

BTW: i absolutely adore the new and improved board, phpBB3 am i right? and also the "File Attachment" feature is absolutely great! Image

Re: recursive scanning of files in a folder

Posted: Wed Nov 25, 2009 8:05 am
by Janschenkel
It's not too hard to adapt a repeat loop to allow a 'cancel' button. Let's make a new stack to demonstrate:
- drop in a field and name it "Counter"
- drop in a button, name it "LongLoop" and set its script to:

Code: Select all

on mouseUp
  repeat with i = 1 to 100000
    put "foobar" after snafu
    put i into field "Counter"
  end repeat
end mouseUp
That's obviously going to take a while, if you click on it, and there is indeed no clean escape route. So we'll add that now:
- drop in another button, name it "Cancel" and set its script to:

Code: Select all

on mouseUp
  set the uCancel of button "LongLoop" to true
end mouseUp
- next we update the script of button "LongLoop"

Code: Select all

on mouseUp
  set the uCancel of me to false
  repeat with i = 1 to 100000
    put "foobar" after snafu
    put i into field "Counter"
    wait 0 milliseconds with messages
    if the uCancel of me is true then
      put "Cancel" into field "Counter"
      exit repeat
    end if
  end repeat
end mouseUp
Now if we click the button again, our "Cancel" button will actually interrupt the loop. The wait with messages allows the rev engine to temporarily suspend a running script to handle other events, which allows the click on the "Cancel" button to take effect. Of course, if you use this approach, it's your job to make sure the user cannot get your application into an ambiguous state (such as starting two conflicting long-running tasks, or in case of an editor application, saving changes the user is making after starting a slow process) - so disable other controls appropriately, and enable them again at the end of the long process.

When we're talking about interrupting running scripts, we should also look at the cantAbort stack property, the allowInterrupts global property and the interrupted function. By default, any script can be halted by hitting Command-period on your keyboard (on Mac anyway, I think it's Control-period on Windows and Linux) - unless the stack has its cantAbort property set to true. But if you only want to protect a particular section of code, you can set the allowInterrupts property to true, check the return value of the [/i]interrupted[/i] function in appropriate places to see if you should stop cleanly, and then after the critical section of code, reset the allowInterrupts to false.

HTH,

Jan Schenkel.

Re: recursive scanning of files in a folder

Posted: Thu Apr 08, 2010 6:50 am
by WinstonJenks
Janschenkel wrote:But if you only want to protect a particular section of code, you can set the allowInterrupts property to true
It seems to me that to protect a section of code, you would set allowInterrupts to "false" not "true". That is the way I understand the 4.0 documentation.