TreeWalk

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

I've just downloaded the new demo version and ordered a licensed copy of 2.5...While waiting for it to arrive, could anyone show a PureBasic newbie how to do a treewalk from a starting filespec I.e "C:\*.log" and load an array with the results??? Then I can finally convert my cataloguer (done in PowerBasic (shudder!)) and actually finish it!! PurseBasic seems fun after what I have gone through.

Fangles.

Fangles
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Mr.Skunk.

Sorry, i don't know what is exactly a 'treewalk'. Could you explain more clearly what you need, please?

Mr Skunk

Mr Skunk's PureBasic Web Page
http://www.skunknet.fr.st

Edited by - mr.skunk on 24 September 2001 16:19:28
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.
I've just downloaded the new demo version and ordered a licensed copy of 2.5...While waiting for it to arrive, could anyone show a PureBasic newbie how to do a treewalk from a starting filespec I.e "C:\*.log" and load an array with the results??? Then I can finally convert my cataloguer (done in PowerBasic (shudder!)) and actually finish it!! PurseBasic seems fun after what I have gone through.
Here is a recursive exeample to scan all files and copy files to a destination directory (including sub directories).. I hope this is what you want !

Procedure CopyDirectory(SourceDirectory$, DestinationDirectory$, Start)

If ExamineDirectory(Start, SourceDirectory$, "*.*")
CreateDirectory(DestinationDirectory$)

Repeat
Type = NextDirectoryEntry()
If Type = 2
If DirectoryEntryName() "." And DirectoryEntryName() ".."
a$ = SourceDirectory$+DirectoryEntryName()+"\"
b$ = DestinationDirectory$+DirectoryEntryName()+"\"
CopyDirectory(a$, b$, Start+1)
UseDirectory(Start)
EndIf
Else
If Type = 1
CopyFile(SourceDirectory$+DirectoryEntryName(), DestinationDirectory$+DirectoryEntryName())
EndIf
EndIf
Until Type = 0
EndIf

EndProcedure

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

Sorry I wasn't too clear before Mr Skunk. Fred read my turbid mind more or less (grin). I want to search a hard disk, starting from a specified Drive (i.e C:\) with an optional file spec (I.e "*.log") and return all matching entries with FULL PATHNAMES (I.e "C:\Done\Internet\DialUp fixes\HarryDUN.log") into an ARRAY. From there, I should be able to integrate that into my existing code after I convert it into PureBasic. (that's going to be a big job for me). I can handle the array once it is built, will be asking it for the matching files (if they exist, attributes etc) and processing the contents of each log file as I have done before. Just didn't know how to do a treewalk in PureBasic!!
Regards, Fangles

Fangles


Fangles
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Mr.Skunk.

Here is two ways to do that:
The code, is Fred's code modified to copy requested files in array instead of directory.

By using dim() array (you must use a big array because you don't know how many files you'll have, so you use a large amount of memory:

Code: Select all

maxfiles=10000

Dim file$(maxfiles)


Procedure ReadDirectory(SourceDirectory$, Start,Pattern$)
Global files

If ExamineDirectory(Start, SourceDirectory$, "*.*")


  Repeat
    Type = NextDirectoryEntry()
    If Type = 2
      If DirectoryEntryName()  "." And DirectoryEntryName()  ".."
        a$ = SourceDirectory$+DirectoryEntryName()+"\"
        ReadDirectory(a$,Start+1,Pattern$)
        UseDirectory(Start)
      EndIf
    Else
      If Type = 1 And Right(Ucase(DirectoryEntryName()),Len(Pattern$))=Ucase(Pattern$)
        files+1
        file$(files)=SourceDirectory$+DirectoryEntryName()
      EndIf
    EndIf
  Until Type = 0
EndIf

EndProcedure

files=0

OpenWindow(0,100,100,600,500,#PB_Window_SystemMenu,"Treewalk")
ReadDirectory("c:\",0,".log")

DrawingOutput(WindowID())
Locate(20,20):Drawtext(str(files)+" files")

For i=1 To files
  Locate(20,20+(i*20)):Drawtext(file$(i))
Next
Stopdrawing()

Repeat
  ev.l=WaitWindowEvent()
Until ev=#PB_EventCloseWindow 
Another way using NewList().
It's a bit more complicated if you don't know structures and lists, but with this method you don't have to definy an array. So, you use only the memory amount you really need.

Code: Select all

 Structure file
  Name.s
EndStructure

NewList files.file()


Procedure ReadDirectory(SourceDirectory$, Start,Pattern$)

If ExamineDirectory(Start, SourceDirectory$, "*.*")


  Repeat
    Type = NextDirectoryEntry()
    If Type = 2
      If DirectoryEntryName()  "." And DirectoryEntryName()  ".."
        a$ = SourceDirectory$+DirectoryEntryName()+"\"
        ReadDirectory(a$,Start+1,Pattern$)
        UseDirectory(Start)
      EndIf
    Else
      If Type = 1 And Right(Ucase(DirectoryEntryName()),Len(Pattern$))=Ucase(Pattern$)
        AddElement(files())
        files()\Name=SourceDirectory$+DirectoryEntryName()
      EndIf
    EndIf
  Until Type = 0
EndIf

EndProcedure

files=0

OpenWindow(0,100,100,600,500,#PB_Window_SystemMenu,"Treewalk")

ReadDirectory("c:\",0,".log") ; don't use "*" in pattern here.

DrawingOutput(WindowID())
Locate(20,20):Drawtext(str(CountList(files()))+" files")

ResetList(files())
For i=1 To CountList(files())
  NextElement(files())
  Locate(20,20+(i*20)):Drawtext(files()\Name)
Next
Stopdrawing()

Repeat
  ev.l=WaitWindowEvent()
Until ev=#PB_EventCloseWindow
Hope it helps...


Mr Skunk

Mr Skunk's PureBasic Web Page
http://www.skunknet.fr.st
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

Thanks Mr Skunk (and Fred too for the original reply), Can't wait to try this as Fred has just sent me my registered compiler (I am almost drooling). As soon as I have played with this and got it right, I can then work on the rest of my cataloguing code.

I might be begging for more help soon (grin). Hope I can return the favour someday :)

Fangles

Fangles
Post Reply