webb@uhccux.uhcc.hawaii.edu (Thomas Webb) (12/08/89)
Hi folks, sorry to bother you with what I think must be a simple porblem, but I'm totally confused. I haven't done any programming since I got out of University, and that was years ago despite what it says on my organisation line, and it seems my mind has been busy obeying the second law of thermodynamics... Anyway, all I am trying to do is write a short program which will give me a list of a directory tree. I am sure that this problem can be easily solved using recursion, but I think that DOS may be getting confused somehow--perhaps the DTA isn't being properly pased to it. I have included the file that I thought would work when I considered the problem--I've tried a bunch of other ways since, but none are doing what I want. Simply stated, the problem with this routine is that it works fine on the way down the first branch of the tree, but fails to find any new branches on the way back up... Thanks in advance for your thoughts, -tom PS I'm using Turbo Pascal 5.5. -tsmw ----------- program treetmp; uses DOS; var dirInfo: SearchRec; nextDirectory:string; procedure tree(next:string); var dtmp:string; begin findFirst(next + '*.*',directory,dirInfo); while dosError=0 do begin if (dirinfo.attr=directory) AND (dirinfo.name > '..') then begin dtmp:=next+dirinfo.name+'\'; writeln(dtmp); tree(dtmp); end; findNext(dirInfo); end; end; begin {MAIN} nextDirectory:='d:\'; writeln(nextDirectory); tree(nextDirectory); end. -- =============================================================================== webb@uhccux.uhcc.Hawaii.edu ===============================================================================
bb16@prism.gatech.EDU (Scott Bostater) (12/08/89)
In article <5651@uhccux.uhcc.hawaii.edu> webb@uhccux.UUCP (Thomas Webb) writes: [ problem with recursive subroutine stated ] >program treetmp; > >uses DOS; > >var >dirInfo: SearchRec; >nextDirectory:string; > >procedure tree(next:string); >var > dtmp:string; >begin [ guts of procedure deleted to keep rn happy :-( ] >end; [ rest of program deleted ] Your problem is that the variable DirInfor is not local to your recursive routine. What's happening is that the second call over-writes the information in DirInfo that specifies what the filename and pathname are. By making Dirinfo local to the procedure tree, you keep making copies of Dirinfo on the heap so that when you return from one level of recursion, the previous data in the old version of DirInfo is still intact. This method works fine as long as you have plenty of heap/stack space! -- Scott Bostater GTRI/RAIL/RAD (Ga. Tech) "My soul finds rest in God alone; my salvation comes from Him" -Ps 62.1 uucp: ...!{allegra,amd,hplabs,ut-ngp}!gatech!prism!bb16 Internet: bb16@prism.gatech.edu
webb@uhccux.uhcc.hawaii.edu (Thomas Webb) (12/09/89)
Lots of people mailed me answers to my problem with the recursive directory search, and I have tried to mail responses to all of them, however, I was unable to get mail through to Bob Baggerman and Tom at the U of Tronto, and so would like to thank them here. At any rate, the sloution that Scott Bostater puts forth here is the one that I have used, and it does, indeed, work as it should. Thank you Scott. >Your problem is that the variable DirInfor is not local to your recursive >routine. What's happening is that the second call over-writes the information >in DirInfo that specifies what the filename and pathname are. By making Dirinfo >local to the procedure tree, you keep making copies of Dirinfo on the heap so >that when you return from one level of recursion, the previous data in the >old version of DirInfo is still intact. This method works fine as long as you >have plenty of heap/stack space! >-- >Scott Bostater GTRI/RAIL/RAD (Ga. Tech) >"My soul finds rest in God alone; my salvation comes from Him" -Ps 62.1 -- =============================================================================== webb@uhccux.uhcc.Hawaii.edu ===============================================================================