[RESOLVED] get all filenames in a drive
hi, can anyone help me in getting all the list of filenames in a drive(ex flash drive, drive e:\ for example) i used the file list box but is there and alternative where i can filter the files like getting all the list of filenames which files are .doc for example.,? and can i list all the filenames and put it in the listbox?
Re: get all filenames in a drive
Look up the Dir() command.... on the initial call, pass it the filter you want.... then start a loop as long as you have a file name returned.... add it to the lsit box... then keep calling Dir() without any paramters, until nothing is returned, adding to the list box as you go.
-tg
Re: get all filenames in a drive
sir thx for the reply., sir i heard about dir but i new here in vb so im not very familiar about the coding in dir() sir can you guide me about the dir? and do you have sample code or syntax?
Re: get all filenames in a drive
Quote:
Look up the Dir() Command
The Dir() Entry on MSDN
the code sample is .NET, but it's in the Vb6 syntax/format, so with a couple of minor tweaks, it'll run jsut fine.
-tg
Re: get all filenames in a drive
do a search on this forum for recursive dir, there will be many examples, alternatives are FSO and findfirstfile /findnextfile APIs, there will be many examples of these too
recursive is when you search all subdirectories as well
Re: get all filenames in a drive
try:
Code:
'Command Button And Listbox
Private Sub Command1_Click()
Dim sFiles As String
sFiles = Dir("C:\*.doc")
Do While Len(sFiles) > 0
List1.AddItem sFiles
sFiles = Dir
Loop
End Sub
Re: get all filenames in a drive
Also try this with API:
Code:
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 lngF As Long
lngF = SendMessageStr(List1.hwnd, &H18D, &H20, "C:\*.*")
End Sub
Re: get all filenames in a drive
thx guys.., cheers to all!:D
Re: [RESOLVED] get all filenames in a drive
Add a list box and a command button to a form. Then apply this code:
Code:
Private Sub Command1_Click()
Dim NameFile As String, SubDir As String
SubDir = CurDir$ & "\*.*" ' Change CurDir to whatever directory is being searched
NameFile = Dir$(SubDir)
Do While NameFile <> vbNullString
List1.AddItem NameFile
NameFile = Dir$
Loop
End Sub
Re: [RESOLVED] get all filenames in a drive
sir thx for the code it works
just one thing., if i change the curdir to D$ it doesnt change the directory. its still on the drive C
Re: [RESOLVED] get all filenames in a drive
Quote:
Originally Posted by
huxley
sir thx for the code it works
just one thing., if i change the curdir to D$ it doesnt change the directory. its still on the drive C
CurDir is a VB Function.
Syntax: CurDir[(drive)]
The optional drive argument is a string expression that specifies an existing drive. If no drive is specified or if drive is a zero-length string (""), CurDir returns the path for the current drive.
If you want to hard code in the drive and path then just change the SubDir string to what you want. i.e. the root of drive D:, SubDir = "D:\*.*"
Re: [RESOLVED] get all filenames in a drive
Quote:
Originally Posted by
Edgemeal
CurDir is a VB Function.
Syntax: CurDir[(drive)]
The optional drive argument is a string expression that specifies an existing drive. If no drive is specified or if drive is a zero-length string (""), CurDir returns the path for the current drive.
If you want to hard code in the drive and path then just change the SubDir string to what you want. i.e. the root of drive D:, SubDir = "D:\*.*"
Thanks, Edge. You got there faster than I did. (Longer lunch hour than expected.) ;)
Re: [RESOLVED] get all filenames in a drive
thx guys.,:) it works., cheers
is it possible to identify the folders using the DIR function?
Re: [RESOLVED] get all filenames in a drive
Code:
If Dir("C:\WINDOWS", vbDirectory) <> vbNullString Then
MsgBox "Folder Exist"
End If
Re: [RESOLVED] get all filenames in a drive
im mean sir is it possible also for the folders to be listed just like the files listed in the listbox.
Re: [RESOLVED] get all filenames in a drive
Re: [RESOLVED] get all filenames in a drive
thank you sir., this post is resolved., thank you for all the reply., :D cheers!:D:D