April 19, 2024, 02:01:22 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Recursive Directory Listing

Started by tbohon, February 01, 2007, 01:58:37 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

tbohon

As a mental exercise (believe me, I need it  ;) ) I'm trying to write a console app which will list all directories recursively, i.e., all directories (not files) at and beneath the starting directory.  I've searched this forum and, using what Paul did in an answer to an Aurora question along with information from the FINDOPEN and FINDNEXT help for EBasic, came up with the following:

OPENCONSOLE

printdir("c:\\*.*")

PRINT "Press Any Key:"
do : UNTIL INKEY$ <> ""

CLOSECONSOLE

END

sub printdir(string path)

  def dir, attrib   : int
  def filename[150] : string
  def fullname[150] : string

  dir = FINDOPEN(path)

  IF (dir)
    DO
      filename = FINDNEXT(dir,attrib)

      if attrib & @FILE_DIRECTORY then
          print path + "[" + filename + "]"
          fullname = path + filename + "\\"
          printdir(fullname)
      end if

    UNTIL filename = ""

    FINDCLOSE dir

  ENDIF

end sub


Compiles nicely, happy camper - but it doesn't work.  All I'm getting is a console flashing on the screen and, by running it eleventy seven times, I see that it's a blank console.  Turning on the debugger it does the OPENCONSOLE then starts complaining about the stack - the message didn't make much sense to me.

Would appreciate one of the gurus taking a quick look and pointing out the (probably very) obvious error(s) I'm making here ...

Also, how would I eliminate processing the '.' and '..' files at each level?  I tried to do that, it didn't compile, so took it back out again.

Thanks, as always, for your assistance in helping me learn this great language.

Best,

Tom
"If you lead your life the right way, the karma will take care of itself ... the dreams will come to you."  -- Randy Pausch, PhD (1961-2008)

Ionic Wind Support Team

I beleive that

  def filename[150] : string
  def fullname[150] : string

Should be:

  def filename[150] : istring
  def fullname[150] : istring

And there is a recursive directory example included with emergence basic BTW, aptly named recurse_dir.eba which is where the Aurora example was ported from.

Paul.
Ionic Wind Support Team

tbohon

Thanks, Paul ...

You mean it's legal to cheat and actually look at the examples???  ;)

Seriously, I did look at the examples but wasn't looking closely enough obviously.

Appreciate it.

Tom
"If you lead your life the right way, the karma will take care of itself ... the dreams will come to you."  -- Randy Pausch, PhD (1961-2008)

tbohon

OK - tried it with Paul's suggestion - it helps (prints out directories at the c:\ level) but it won't recurse.  I was thinking it might have something to do with the string I'm passing on the recursive calls to printdir but I've tried changing the argument to an istring ('no appropriate conversion exists' error) and adding a "*.*" to it (no difference).

Oh, and Paul - I don't find any recursive examples in my EBasic directory ...  ???

Here's the latest version of the code:

OPENCONSOLE

printdir("c:\\*.*")

PRINT "Press Any Key:"
do : UNTIL INKEY$ <> ""

CLOSECONSOLE

END

sub printdir(string path)

  def dir, attrib   : int
  def filename[150] : istring
  def fullname[150] : istring

  dir = FINDOPEN(path)

  IF (dir)
    DO
      filename = FINDNEXT(dir,attrib)

      if attrib & @FILE_DIRECTORY then
          fullname = left$(path,3) + filename + "\\"
  print fullname
          printdir(fullname)
      end if

    UNTIL filename = ""

    FINDCLOSE dir

  ENDIF

end sub


As always, thanks for the enlightenment ...  :)

Tom
"If you lead your life the right way, the karma will take care of itself ... the dreams will come to you."  -- Randy Pausch, PhD (1961-2008)

Ionic Wind Support Team

February 01, 2007, 03:03:09 PM #4 Last Edit: February 01, 2007, 03:05:04 PM by Paul Turley
here it is.  It should be in your 'projects' directory.


DEF count:INT


OPENCONSOLE
'recursively print all directories
count = printdir("d:\\")
PRINT count,"Files"
PRINT "Press Any Key"
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END


SUB printdir(path:STRING),INT
DEF count:INT
DEF dir,attrib:INT
DEF filename:STRING
DEF fullname:STRING
count = 0
dir = FINDOPEN(path + "*.*")
IF(dir)
DO
filename = FINDNEXT(dir,attrib)
IF len(filename)
IF attrib & @FILE_DIRECTORY
IF(filename <> ".") & (filename <> "..")
PRINT path + "[" + filename + "]"
fullname = path + filename + "\\"
count = count + printdir(fullname)
ENDIF
ELSE
                                count = count + 1
PRINT path + filename
ENDIF
ENDIF
'the exit case is when there are no more entries
'in the current directory
UNTIL filename = ""
FINDCLOSE dir
ENDIF
RETURN count
ENDSUB
Ionic Wind Support Team

LarryMc

Paul
Quotehere it is.  It should be in your 'projects' directory.
I didn't have it either.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Ionic Wind Support Team

Ionic Wind Support Team

LarryMc

well...you're surely right, Ollie. ;D
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

barry

This is interesting.  I remember that example file very well.  I played with it a little, reminding myself how to traverse a directory tree, shortly after I installed EBasic.  But now I don't have it either.

Could it have been left out of the update?  I'm sure it was there at one time.

Barry

Ionic Wind Support Team

Anything is possibe.  I probably forgot to select it when using ISTool.  No biggie as it will be included with the next update.
Ionic Wind Support Team

Raid

Quote from: barry on February 02, 2007, 03:42:05 PM
This is interesting.  I remember that example file very well.  I played with it a little, reminding myself how to traverse a directory tree, shortly after I installed EBasic.  But now I don't have it either.

Could it have been left out of the update?  I'm sure it was there at one time.

Barry


I've printed out that example code, and modified it so that it only shows me directory names; perfect. But I cannot for the life of me figure out how its working exactly. I've tried traversing directory trees for years, and always had to resort to temp files and such. Your sample code doesn't even have to create any temp files. Great, except I haven't the foggiest idea of how it's keeping track of the folder it previously showed?

I know this probably sounds very silly, But I really don't understand how it knows which folder it's previously shown, and how to keep track. IE: when it's reached the end of a folder tree, how does it know where it left off previously?

sub1/sub2/[sub3]
sub1/sub2/[sub3/4]

etc...
Obviously the one in [brackets] is as far down as the folder goes. I don't even see an array here keeping track of this. So whats the magic that I'm missing please?
Proudly licensed EBASIC owner since Feb 2009. Network library present!

Ionic Wind Support Team

It's called recursion and there have been entire books written on the subject.  Recursion is when a subroutine calls itself.

Local variables, those within a subroutine, are located on the stack and exist while the subroutine is running.  When a subroutine calls itself the new run of that subroutine gets its own space on the stack for the local variable.  So in essence it is your 'temp' variable by using the inherant nature of recursive calls.

Hard to explain simply.  Maybe this will help:

http://en.wikipedia.org/wiki/Recursion_%28computer_science%29

Paul.

Ionic Wind Support Team

Raid

Thanks greatly Paul! It makes sense now. :)

I have tried to port it to asic, but i've gotten the same results as the original poster. I'm not concerned tho, as I realize now whats going on with the code. I've been tinkering for the past few days on a cheat program for policy editing, and I must say, I'm actually enjoying this language.

Thanks again!
Proudly licensed EBASIC owner since Feb 2009. Network library present!