directory file functions examples (blitz3d)

Started by RemiD, December 12, 2020, 10:01:44

Previous topic - Next topic

RemiD


;directory / file functions examples (blitz3d)

;you can download the necessary directories / files structure here :
;http://rd-stuff.fr/blitz3d/directory-file-examples-20201212-1052.zip

;constants, globals, lists
;------------------------------------------------------------------

;sub kinds
Const CDirectory% = 1
Const CFile% = 2

;sub list
Global TSubsCount% = 0
Dim TSub_Kind%(100) ;directory or file
Dim TSub_Name$(100)
Dim TSub_Size%(100)

;------------------------------------------------------------------

Graphics(640,480,32,2)

DebugLog("")
;create a directory
Path$ = "testdir"
CreateDir(Path)
If( IsDir(Path) <> 0 )
dirH% = OpenDir(Path)
DebugLog(dirH)
CloseDir(dirH) : dirH = 0
EndIf

DebugLog("")
;delete a directory
Path$ = "testdir"
If( IsDir(Path) <> 0 )
DeleteDir(Path)
DebugLog(IsDir(Path))
EndIf

DebugLog("")
;open a directory, then close a directory
Path$ = "config"
If( IsDir(Path) <> 0 )
dirH% = OpenDir(Path)
DebugLog(dirH)
CloseDir(dirH) : dirH = 0
EndIf

DebugLog("")
;scan a directory, display subs properties (kind, name, size)
Path$ = "C:\Datas LTPE13\Programmation\Blitz3d creations\directory file 20201204"
If( IsDir(Path) <> 0 )
ScanDir(Path, True)
For I% = 1 To TSubsCount Step 1
  DebugLog( SubKindStr(TSub_Kind(I))+" "+TSub_Name(I)+" "+TSub_Size(I)+"bytes" )
Next
EndIf

DebugLog("")
;create a file
Path$ = "saves"
FileName$ = "testfile.xxx"
If( IsDir(Path) = True )
CreateFile(Path+"\"+FileName)
DebugLog( IsFile(Path+"\"+FileName) )
EndIf

DebugLog("")
;delete a file
Path$ = "saves"
FileName$ = "testfile.xxx"
If( IsDir(Path) = True )
DeleteFile(Path+"\"+FileName)
DebugLog( IsFile(Path+"\"+FileName) )
EndIf

DebugLog("")
;open a file, then close a file
Path$ = "saves"
FileName$ = "test-0000-00-00-00-00.sav"
If( IsDir(Path) = True )
If( IsFile(Path+"\"+FileName) = True )
  fileH% = OpenFile(Path+"\"+FileName)
  DebugLog(fileH)
  If( fileH <> 0 )
   CloseFile(fileH) : fileH = 0
  EndIf
EndIf
EndIf

DebugLog("")
;open/create a text file, then write lines, then close the file
Path$ = "saves"
FileName$ = "player-2020-12-11-10-17.txt"
If( IsDir(Path) = True )
If( IsFile(Path+"\"+FileName) = True )
  fileH% = OpenFile(Path+"\"+FileName)
  DebugLog(fileH)
  If( fileH <> 0 )
   TName$ = "RemiD" : WriteLine(fileH,TName) : DebugLog(TName)
   TPhysicalState# = 80.0 : WriteLine(fileH,TPhysicalState) : DebugLog(TPhysicalState)
   TMentalState# = 90.0 : WriteLine(fileH,TMentalState) : DebugLog(TMentalState)
   TEnergy# = 100.0 : WriteLine(fileH,TEnergy) : DebugLog(TEnergy)
   TMoney% = 428571 : WriteLine(fileH,TMoney) : DebugLog(TMoney)
   CloseFile(fileH) : fileH = 0
  EndIf
EndIf
EndIf

DebugLog("")
;open a text file, then read lines, then close the file
Path$ = "saves"
FileName$ = "player-2020-12-11-10-17.txt"
If( IsDir(Path) = True )
If( IsFile(Path+"\"+FileName) = True )
  fileH% = OpenFile(Path+"\"+FileName)
  DebugLog(fileH)
  If( fileH <> 0 )
   TName$ = ReadLine(fileH) : DebugLog(TName)
   TPhysicalState# = ReadLine(fileH) : DebugLog(TPhysicalState)
   TMentalState# = ReadLine(fileH) : DebugLog(TMentalState)
   TEnergy# = ReadLine(fileH) : DebugLog(TEnergy)
   TMoney% = ReadLine(fileH) : DebugLog(TMoney)
   CloseFile(fileH) : fileH = 0
  EndIf
EndIf
EndIf

DebugLog("")
;open/create a binary file , then write datas, then close the file
Path$ = "saves"
FileName$ = "player-2020-12-11-10-17.bin"
If( IsDir(Path) = True )
If( IsFile(Path+"\"+FileName) = True )
  fileH% = OpenFile(Path+"\"+FileName)
  DebugLog(fileH)
  If( fileH <> 0 )
   TName$ = "RemiD" : WriteString(fileH,TName) : DebugLog(TName)
   TPhysicalState# = 80.0 : WriteFloat(fileH,TPhysicalState) : DebugLog(TPhysicalState)
   TMentalState# = 90.0 : WriteFloat(fileH,TMentalState) : DebugLog(TMentalState)
   TEnergy# = 100.0 : WriteFloat(fileH,TEnergy) : DebugLog(TEnergy)
   TMoney% = 428571 : WriteInt(fileH,TMoney) : DebugLog(TMoney)
   CloseFile(fileH) : fileH = 0
  EndIf
EndIf
EndIf

DebugLog("")
;open a binary file, then read the datas, then close the file
Path$ = "saves"
FileName$ = "player-2020-12-11-10-17.bin"
If( IsDir(Path) = True )
If( IsFile(Path+"\"+FileName) = True )
  fileH% = OpenFile(Path+"\"+FileName)
  DebugLog(fileH)
  If( fileH <> 0 )
   TName$ = ReadString(fileH) : DebugLog(TName)
   TPhysicalState# = ReadFloat(fileH) : DebugLog(TPhysicalState)
   TMentalState# = ReadFloat(fileH) : DebugLog(TMentalState)
   TEnergy# = ReadFloat(fileH) : DebugLog(TEnergy)
   TMoney% = ReadInt(fileH) : DebugLog(TMoney)
   CloseFile(fileH) : fileH = 0
  EndIf
EndIf
EndIf

WaitKey()

End()

;functions
;------------------------------------------------------------------

Function IsDir%( Path$ )
R = False
If( FileType(Path$) = 2 )
  R = True
EndIf
Return R
End Function

Function IsFile%( PathAndFileName$ )
R = False
If( FileType(PathAndFileName) = 1 )
  R = True
EndIf
Return R
End Function

Function OpenDir%( Path$ )
Return ReadDir(Path)
End Function

Function CreateFile( PathAndFileName$ )
File = WriteFile(PathAndFileName)
CloseFile(File)
End Function

Function ScanDir( Path$, Flag%=False )
;delete the old entries and count
For I% = 1 To TSubsCount
  TSub_Kind(I) = 0
  TSub_Name(I) = ""
  TSub_Size(I) = 0
Next
TSubsCount = 0
;scan the directory, store each sub with its properties (kind, name, size)
Directory = ReadDir(Path)
LoopState% = True
While(LoopState = True)
  TName$ = NextFile$(Directory)
  If( TName <> "" And TName <> "." And TName <> ".." )
   If( IsDir(Path+"\"+TName)=True )
    TKind% = CDirectory
    TSize% = 0
   ElseIf( IsFile(Path+"\"+TName)=True )
    TKind% = CFile
    If( Flag = True )
     TSize% = FileSize(Path+"\"+TName)
    Else If( Flag = False )
     TSize% = 0
    EndIf
   EndIf
   TSubsCount = TSubsCount + 1
   I% = TSubsCount
   TSub_Kind(I) = TKind
   TSub_Name(I) = TName
   TSub_Size(I) = TSize
  ElseIf(TName = "")
   LoopState = False
  EndIf
Wend
End Function

Function SubKindStr$(T_Kind%)
If( T_Kind = Cdirectory )
  Return "D"
Else If( T_Kind = Cfile )
  Return "F"
EndIf
End Function

;------------------------------------------------------------------