|
-
May 29th, 2009, 12:15 PM
#1
Thread Starter
Member
[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?
-
May 29th, 2009, 12:18 PM
#2
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
-
May 29th, 2009, 12:24 PM
#3
Thread Starter
Member
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?
-
May 29th, 2009, 12:54 PM
#4
Re: get all filenames in a drive
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
-
May 29th, 2009, 10:29 PM
#5
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
May 30th, 2009, 12:01 AM
#6
Hyperactive Member
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
"More Heads are Better than One"
-
May 30th, 2009, 12:33 AM
#7
Lively Member
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
-
Jun 8th, 2009, 10:13 AM
#8
Thread Starter
Member
Re: get all filenames in a drive
thx guys.., cheers to all!
-
Jun 8th, 2009, 10:26 AM
#9
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
-
Jun 8th, 2009, 10:31 AM
#10
Thread Starter
Member
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
-
Jun 8th, 2009, 01:05 PM
#11
Re: [RESOLVED] get all filenames in a drive
 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:\*.*"
-
Jun 8th, 2009, 02:18 PM
#12
Re: [RESOLVED] get all filenames in a drive
 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.)
-
Jun 9th, 2009, 01:01 AM
#13
Thread Starter
Member
Re: [RESOLVED] get all filenames in a drive
thx guys., it works., cheers
is it possible to identify the folders using the DIR function?
-
Jun 9th, 2009, 04:56 AM
#14
Hyperactive Member
Re: [RESOLVED] get all filenames in a drive
Code:
If Dir("C:\WINDOWS", vbDirectory) <> vbNullString Then
MsgBox "Folder Exist"
End If
"More Heads are Better than One"
-
Jun 9th, 2009, 05:19 AM
#15
Thread Starter
Member
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.
-
Jun 9th, 2009, 07:00 AM
#16
-
Jun 9th, 2009, 07:46 AM
#17
Thread Starter
Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|