Get Disk Info. and create a treeview

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 tricky.

Hello all,
I am a beginner and have two questions.
1. How to know how many drives are there in the system and their names?(e.g. c:\ d:\ e:\)
2. How can I create a treeview just like the one in windows explorer?
+Desktop
+My Documents
+My Computer
.
.
.
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 fweil.

Hello tricky,

I answer you just on listing a directory using PureBasic Filesystem functions now. Sorry I do not have time enough to look at other part of your question now. If something remain I will continue tomorrow.

Just check the code here after and tell me if you found interesting matter with. I did not comment the code. I suppose you will read PB documentation, but feel free to come back with more questions.

The following code works using a Directory variable you can set at any needed value. It is possible to catch events to surf over your file system and eventually to have a drives root setted by hardcoding or OS detection.

Enjoy PureBasic

;============================================================
Quit.l
NextEntry.l
NextEntrySize.l
Result.l
WEvent.l
EventGadget.l
FileAttributes.l
Directory.s
FileName.s
FileType.s
SNextEntrySize.s
sFileAttributes.s

Quit = #FALSE

If OpenWindow(0, 200, 200, 480, 360, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_TitleBar, "")

If CreateGadgetList(WindowID())

ListIconGadget(10, 10, 10, 460, 300, "Name", 120)
AddGadgetColumn(10, 1, "Type", 80)
AddGadgetColumn(10, 2, "Size", 80)
AddGadgetColumn(10, 3, "Attributes", 80)
EndIf

Directory = "C:\"
NextEntry = ExamineDirectory(0, Directory, "*.*")

While NextEntry
NextEntry = NextDirectoryEntry()
FileName = DirectoryEntryName()
Select NextEntry
Case 1
FileType = "File"
Case 2
FileType = "Dir"
Default
EndSelect
NextEntrySize = FileSize(Directory + "\" + FileName)
Select NextEntrySize
Case -1
sNextEntrySize = "Not foud"
Case -2
sNextEntrySize = "."
Default
sNextEntrySize = Str(NextEntrySize)
EndSelect
FileAttributes = DirectoryEntryAttributes()
sFileAttributes = " "
If FileAttributes & #PB_FileSystem_Hidden
sFileAttributes = "H" + Mid(sFileAttributes, 2, 5)
EndIf
If FileAttributes & #PB_FileSystem_Archive
sFileAttributes = Mid(sFileAttributes, 1, 1) + "A" + Mid(sFileAttributes, 3, 4)
EndIf
If FileAttributes & #PB_FileSystem_Compressed
sFileAttributes = Mid(sFileAttributes, 1, 2) + "C" + Mid(sFileAttributes, 4, 3)
EndIf
If FileAttributes & #PB_FileSystem_Normal
sFileAttributes = Mid(sFileAttributes, 1, 3) + "N" + Mid(sFileAttributes, 5, 2)
EndIf
If FileAttributes & #PB_FileSystem_ReadOnly
sFileAttributes = Mid(sFileAttributes, 1, 4) + "R" + Mid(sFileAttributes, 6, 1)
EndIf
If FileAttributes & #PB_FileSystem_System
sFileAttributes = Mid(sFileAttributes, 1, 5) + "S"
EndIf
AddGadgetItem(10, -1, FileName + Chr(10) + FileType + Chr(10) + sNextEntrySize + Chr(10) + sFileAttributes)
Wend

Repeat

WEvent = WaitWindowEvent()

Select WEvent
Case #PB_EventMenu
Case #PB_EventGadget
Case #PB_EventCloseWindow
Quit = #TRUE
Case #PB_EventRepaint
Case #PB_EventMoveWindow
Default
EndSelect

Until Quit

EndIf

End
;============================================================

Francois Weil
14, rue Douer
F64100 Bayonne
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 freak.

Hi tricky, and welcome to the PB community.

About the drive letters: here's some code that does what you want.
I've tried to comment it, so it's easier to understand.

It returns a string containing the drive letters.

Code: Select all

; **** Get Drives ****
  
bitmask.l = GetLogicalDrives_()       ; not a PB Function, its Windows API (becourse of the '_' )
                                      ; each bit represents one drive.
drives.s = ""
one.l = 1
  
For i = 0 To 31                       ; go from bit 0 to 31.
  If bitmask  & one<<i                ; the 'one<<i' thingy is a bit tricky, what it does is something like 2^i
                                      ; '&' is bitwise AND operator, so if the bit i is one, the whole thing returns true.
    drives = drives + Chr(65+i)       ; add the driveletter to the string.
  EndIf
Next i
  
MessageRequester("DRIVES",drives,0)   ; displays the result.
End
  
 
Note that GetLogicalDrives_() is not a native PB command, so you wont find it in the docs. It's a function provided by the Windows OS.
If you are not familiar with that OS stuff, just use it as i posted it, and don't be afraid to ask.

About the treeview, there's a treeviewgadget (under the Gadget library in the docs) but i haven't used that yet, so maybe some other user can help you there.

That's it, hope it helped a bit...

Timo

--

A debugged program is one for which you have not yet found the conditions that make it fail.
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 tricky.

Thanks, fweil and freak. The code you provided helps me a lot.
Post Reply