[Resolved]Display directory contents in a listview
Im having problem using Listview control...in my application, i want to display all files in a particular directory...let say i only want to display a directory Dir1.Path = "C:\ISISArchive"...and then i want my listview to view all files in this directory...
can someone show me a code for this???
ur help would be much appreciated!!!
Last edited by nyah_RMC; Nov 12th, 2002 at 09:49 PM.
robee
There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Sub Command1_Click()
Dim r As Long
r = SendMessageStr(List1.hwnd, &H18D, &H20, "C:\*.*")
End Sub
But to have more control of the individual files (so you can also add sizes and dates etc to your ListView) you might want to take some of these examples and combine them:
To check if a file exists:
VB Code:
Function FileExist(ByVal strPath As String) As Boolean
FileExist = Len(Dir$(strPath)) <> 0
To check if a Directory exists:
VB Code:
Function DirExist(ByVal DirPath As String) As Boolean
On Error Resume Next
'The above line is only required on Win NT4, and 2K. Remove on Win9x
DirExist = Len(Dir$(DirPath, vbDirectory)) <> 0
End Function
To check to see if strFile is a directory or not:
VB Code:
If (GetAttr(strPath & “\” & strFile) AND vbDirectory) = vbDirectory then
‘ it’s a directory, not a file
To load an array with the names of all the files in a directory (not sub-directories):
If you are having problems with the ListView parts, then this might help:
The following code is used in order to show to the user the list of files in a folder.
Notes:
· The display of the list of files is done using the ListView control, rather than the standard and simple List control. The ListView allows for multiple columns, user re-sizing of the columns, sorting by columns etc.
· The ListView control is used by adding “Microsoft Windows Common Controls” to the project (Project Components).
· In order to use this control, the MSCOMCTL.OCX file is required in C:\Windows\System.
Add a ListView into the project. Modify it as follows:
· View = 3 - Report
· Border Style = 1
· Column Headers:
· Name – Width of 3800
· Extn – 700
· Date – 1800
· Size – 600 Right Align
· SizeFormatted – 0 Right Align
· DateFormatted – 0 Right Align
· Font = MS Sans Serif of 8.25
Loop through the files in a folder using this code. The variable f will hold just the file names found, not the full path to the file.
VB Code:
SearchName = gFolder & "\*.*"
f = Dir(SearchName, vbReadOnly)
While f <> ""
f = Dir()
Wend
Before adding items into the ListView, it is first cleared out of any old information:
ListView1.ListItems.Clear
For each file found (i.e. for each f) within the list of files loop above, the details are added into the ListView using:
Note: To allow the correct sorting fields, the last two items take the size and date and ensure these items can be sorted correctly. However, these sorting columns are described with a width of 0, so the user never sees them.
The ListView can be sorted with:
VB Code:
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As ColumnHeader)
thanks for your codes, but i only want to put several codes on my Form_load event.. i want my listview control to display upon form load my specified file directory files...i adopt ur code like this:
ey roy it works! though jordanchris' code gives a lot of ideas! but this is just what i need roy! THANKS!...but one more thing roy...it didnt display the file size and the date just the file name..maybe there's missing here..
robee
There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!
i tried to change the listview properties but it didnt work! well i also noticed that the word Format(FileLen and FileDateTime is written in blue...but when i try to copy this code it is written in black...are these some vb function???
robee
There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!
Oh, don't be silly - it's a forum, they have their own way of formatting key words. VB doesn't necessary have to look the same. But enough of lyrics ...
I'm not sure what are doing in your project, but you need to set your Listview1.View = 3 (Report view) in order to see columns and headers.
oh im sorry for finding my post 'silly'....i just thought that those WORDS are embedded VB functions...or just a user-defined VARIABLES!...anyways, ive already set the view property to 3 but still not working...THANKS FOR UR TIME! ...nyways, im adopting ur code here...maybe i just work it out myself!
Last edited by nyah_RMC; Nov 11th, 2002 at 09:54 PM.
robee
There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!
I tried to change some codes here and it displays the size and date of the LAST file...(see my uploaded image)...how can i display all file's sizes and date modifications????
VB Code:
Public Sub CheckFolder(strFolder As String, Optional strExt As String = "")
IT finally works! I just removed the sorting order on my listview property!! and thanks to those who reply on my post! u HELP A LOT GUYS!!! THANK YOU VERY MUCH!!!
robee
There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!
hey dude, it's been a while.... oops, i need to check my code and study it again ! (cause im shifted to doing web applications..)..nyways, i tried to eliminate that line of code in my apps and it still works..so you can also disregard it! (kinda dont remember why i put it anyway)
robee
There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!