Verzeichnisbaum ExamineDirectory vs Datenbank

Anfängerfragen zum Programmieren mit PureBasic.
Benutzeravatar
marcelx
Beiträge: 424
Registriert: 19.02.2010 20:19
Wohnort: Darmstadt

Verzeichnisbaum ExamineDirectory vs Datenbank

Beitrag von marcelx »

Hallo zusammen,

ich erzeuge ein Verzeichnisbaum und wollte nachträgliche diese untersuchen.
Ich kann mit ExamineDirectory den Baum durchforsten oder beim anlegen der Baum die Baum-Struktur in eine Datenbank vermerken um direkt in dem Baum zu ändern.

Der Baum hat nur 2 Ebene:

Dir-A
SubDir-A1
...
SubDir-An

Dir-B
SubDir-B1
...
SubDir-Bm

Dir-C
...

Was ist schneller?
EDIT:
Ich möchte z.B. alle Verzeichnisse z.B. in Dir-A listen
Ich denke, dass mit ExamineDirectory besser ist als parallel in eine Datenbank die Information zu halten
Win-10, PB 5.31 (Windows - x86)
GPI
Beiträge: 1511
Registriert: 29.08.2004 13:18
Kontaktdaten:

Re: Verzeichnisbaum ExamineDirectory vs Datenbank

Beitrag von GPI »

Kommt natürlich immer drauf an, was du genau machen willst. Eine Datenbank ist aber etwas "overkill". Ich würde dafür Listen und Structuren nehmen, so bspw:

Code: Alles auswählen

EnableExplicit

Structure sFile
  name.s
  size.q
EndStructure

Structure sDirectory
  name.s
  *parent.sDirectory
  List files.sFile()
  List *subDir.sDirectory()
EndStructure

Global NewList _DirectoryCache.sDirectory()

Procedure Dir(path.s, pattern.s="*.*", *parent = #Null)
  Protected d
  Protected name.s
  Protected *this.sDirectory
  
  AddElement(_DirectoryCache())
  *this = _DirectoryCache()
  
  With *this
    \name = GetFilePart(path)
    \parent = *parent
    
    d = ExamineDirectory(#PB_Any, path, pattern)
    If d
      While NextDirectoryEntry(d)
        name = DirectoryEntryName(d)
        If DirectoryEntryType(d) = #PB_DirectoryEntry_File
          AddElement( \files() )
          \files()\name = name
          \files()\size = DirectoryEntrySize(d)
        ElseIf name <> "." And name <> ".."
          AddElement( \subDir() )
          \subDir() =  dir(path + #PS$ + name, pattern, *this)
        EndIf
      Wend
      FinishDirectory(d)
    EndIf
  EndWith
  ProcedureReturn *this
EndProcedure

Procedure.s GetFullPath(*dir.sDirectory)
  If *dir = #Null
    ProcedureReturn ""
  EndIf
  Protected ret.s = *dir\name
  While *dir\parent
    *dir = *dir\parent
    ret = *dir\name + #PS$ + ret
  Wend
  ProcedureReturn ret
EndProcedure
    

Procedure OutPutDir(*dir.sDirectory)
  If *dir = #Null
    Debug "INVALID"
    ProcedureReturn 
  EndIf
  
  Debug "Content of " + GetFullPath(*dir)
  ForEach *dir\subDir()
    Debug "Dir " + *dir\subDir()\name
  Next
  ForEach *dir\files()
    Debug "File " + *dir\files()\name +" "+ *dir\files()\size+" bytes"
  Next
  Debug "---"
EndProcedure

; return a pointer to the subdirectory or #null
Procedure _CD(*dir.sDirectory, subdir.s)
  If subdir = ".."
    ProcedureReturn *dir\parent
  EndIf
  ForEach *dir\subDir()
    If *dir\subDir()\name = subdir
      ProcedureReturn *dir\subDir()
    EndIf
  Next
  ProcedureReturn #Null
EndProcedure

; parse a complete pfad!
Procedure CD(*dir.sDirectory, path.s)
  CompilerIf "/" <> #PS$ ;windows-fix for linux-paths
    ReplaceString(path,"/",#PS$,#PB_String_InPlace)
  CompilerEndIf
  
  If Left(path,1) = #PS$
    While *dir\parent
      *dir = *dir\parent
    Wend
    path = Mid(path, 2)
  EndIf
  
  Protected i = 1
  Protected subDir.s
  While *dir
    subDir=StringField(path,i,#PS$) : i+1
    If subDir = "" : Break : EndIf
    *dir = _cd(*dir, subDir)
    Debug subDir+" "+*dir
  Wend
  ProcedureReturn *dir
EndProcedure

Procedure.q GetFileSize(*dir.sDirectory, file.s)
  ForEach *dir\files()
    If *dir\files()\name = file
      ProcedureReturn *dir\files()\size
    EndIf
  Next
  ProcedureReturn -1 ; not found
EndProcedure
    

Define *root.sDirectory = dir (#PB_Compiler_Home+"Examples")

OutPutDir(*root)

Define *sub.sDirectory 
*sub = cd(*root,"3D\Data")

OutPutDir(*sub)

*sub = cd(*sub,"..\Demos")
OutPutDir(*sub)

*sub = cd(*sub,"\Sources\Data")
OutPutDir(*sub)


Debug "Sizetest: " + GetFileSize(*sub, "world.png")
Wenn du sehr oft das Verzeichnis einliest, kann es vorteilhaft sein, das zwischenzuspeichern. Ansonsten würde ich es direkt auslesen.
CodeArchiv Rebirth: Deutsches Forum Github Hilfe ist immer gern gesehen!
Benutzeravatar
marcelx
Beiträge: 424
Registriert: 19.02.2010 20:19
Wohnort: Darmstadt

Re: Verzeichnisbaum ExamineDirectory vs Datenbank

Beitrag von marcelx »

Danke GPI
Das direkt auslesen ist bei mein Fall gütiger als eine Datenbank
Win-10, PB 5.31 (Windows - x86)
Antworten