To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Article :: Building Dynamic Systems with Expressions in .NET
How Is XML Like An Interface?
Understanding Covariance and Contravariance
Print VS 2010 Keyboard Shortcut References in Letter (8.5x11in) and A4 (210×297mm) Sizes
Updated Productivity Power Tools



Go Back   VBForums > VBForums CodeBank > CodeBank - Visual Basic 6 and earlier

Reply Post New Thread
 
Thread Tools Display Modes
Old May 13th, 2003, 03:44 PM   #1
manavo11
Super Moderator
 
manavo11's Avatar
 
Join Date: Nov 02
Location: Other side of town from si_the_geek
Posts: 7,153
manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)
VB - List All The Files In A Directory

This shows the filenames and not the total number of files in a directory.
Attached Files
File Type: zip list all the files in a directory (names).zip (2.0 KB, 13663 views)
__________________


Has someone helped you? Then you can Rate their helpful post.

Last edited by manavo11; Sep 23rd, 2003 at 04:49 PM.
manavo11 is offline   Reply With Quote
Old Sep 23rd, 2003, 04:49 PM   #2
manavo11
Super Moderator
 
manavo11's Avatar
 
Join Date: Nov 02
Location: Other side of town from si_the_geek
Posts: 7,153
manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)
Another way (API) :

VB Code:
  1. 'Create a form with a command button (command1), a list box (list1)
  2. 'and four text boxes (text1, text2, text3 and text4).
  3. 'Type in the first textbox a startingpath like c:\
  4. 'and in the second textbox you put a pattern like *.* or *.txt
  5. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
  6. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
  7. Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
  8. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
  9. Const MAX_PATH = 260
  10. Const MAXDWORD = &HFFFF
  11. Const INVALID_HANDLE_VALUE = -1
  12. Const FILE_ATTRIBUTE_ARCHIVE = &H20
  13. Const FILE_ATTRIBUTE_DIRECTORY = &H10
  14. Const FILE_ATTRIBUTE_HIDDEN = &H2
  15. Const FILE_ATTRIBUTE_NORMAL = &H80
  16. Const FILE_ATTRIBUTE_READONLY = &H1
  17. Const FILE_ATTRIBUTE_SYSTEM = &H4
  18. Const FILE_ATTRIBUTE_TEMPORARY = &H100
  19. Private Type FILETIME
  20.     dwLowDateTime As Long
  21.     dwHighDateTime As Long
  22. End Type
  23. Private Type WIN32_FIND_DATA
  24.     dwFileAttributes As Long
  25.     ftCreationTime As FILETIME
  26.     ftLastAccessTime As FILETIME
  27.     ftLastWriteTime As FILETIME
  28.     nFileSizeHigh As Long
  29.     nFileSizeLow As Long
  30.     dwReserved0 As Long
  31.     dwReserved1 As Long
  32.     cFileName As String * MAX_PATH
  33.     cAlternate As String * 14
  34. End Type
  35. Function StripNulls(OriginalStr As String) As String
  36.     If (InStr(OriginalStr, Chr(0)) > 0) Then
  37.         OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
  38.     End If
  39.     StripNulls = OriginalStr
  40. End Function
  41. Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
  42.     'KPD-Team 1999
  43.     'E-Mail: [email]KPDTeam@Allapi.net[/email]
  44.     'URL: [url]http://www.allapi.net/[/url]
  45.     Dim FileName As String ' Walking filename variable...
  46.     Dim DirName As String ' SubDirectory Name
  47.     Dim dirNames() As String ' Buffer for directory name entries
  48.     Dim nDir As Integer ' Number of directories in this path
  49.     Dim i As Integer ' For-loop counter...
  50.     Dim hSearch As Long ' Search Handle
  51.     Dim WFD As WIN32_FIND_DATA
  52.     Dim Cont As Integer
  53.     If Right(path, 1) <> "\" Then path = path & "\"
  54.     ' Search for subdirectories.
  55.     nDir = 0
  56.     ReDim dirNames(nDir)
  57.     Cont = True
  58.     hSearch = FindFirstFile(path & "*", WFD)
  59.     If hSearch <> INVALID_HANDLE_VALUE Then
  60.         Do While Cont
  61.         DirName = StripNulls(WFD.cFileName)
  62.         ' Ignore the current and encompassing directories.
  63.         If (DirName <> ".") And (DirName <> "..") Then
  64.             ' Check for directory with bitwise comparison.
  65.             If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
  66.                 dirNames(nDir) = DirName
  67.                 DirCount = DirCount + 1
  68.                 nDir = nDir + 1
  69.                 ReDim Preserve dirNames(nDir)
  70.             End If
  71.         End If
  72.         Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
  73.         Loop
  74.         Cont = FindClose(hSearch)
  75.     End If
  76.     ' Walk through this directory and sum file sizes.
  77.     hSearch = FindFirstFile(path & SearchStr, WFD)
  78.     Cont = True
  79.     If hSearch <> INVALID_HANDLE_VALUE Then
  80.         While Cont
  81.             FileName = StripNulls(WFD.cFileName)
  82.             If (FileName <> ".") And (FileName <> "..") Then
  83.                 FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
  84.                 FileCount = FileCount + 1
  85.                 List1.AddItem path & FileName
  86.             End If
  87.             Cont = FindNextFile(hSearch, WFD) ' Get next file
  88.         Wend
  89.         Cont = FindClose(hSearch)
  90.     End If
  91.     ' If there are sub-directories...
  92.     If nDir > 0 Then
  93.         ' Recursively walk into them...
  94.         For i = 0 To nDir - 1
  95.             FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount, DirCount)
  96.         Next i
  97.     End If
  98. End Function
  99. Sub Command1_Click()
  100.     Dim SearchPath As String, FindStr As String
  101.     Dim FileSize As Long
  102.     Dim NumFiles As Integer, NumDirs As Integer
  103.     Screen.MousePointer = vbHourglass
  104.     List1.Clear
  105.     SearchPath = Text1.Text
  106.     FindStr = Text2.Text
  107.     FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
  108.     Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
  109.     Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
  110.     Screen.MousePointer = vbDefault
  111. End Sub
__________________


Has someone helped you? Then you can Rate their helpful post.
manavo11 is offline   Reply With Quote
Old Sep 23rd, 2003, 05:06 PM   #3
manavo11
Super Moderator
 
manavo11's Avatar
 
Join Date: Nov 02
Location: Other side of town from si_the_geek
Posts: 7,153
manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)
The Dir way :

VB Code:
  1. 'Just add a listbox (List1)
  2. Private Sub ListFiles(strPath As String, Optional Extention As String)
  3. 'Leave Extention blank for all files
  4.     Dim File As String
  5.    
  6.     If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
  7.    
  8.     If Trim$(Extention) = "" Then
  9.         Extention = "*.*"
  10.     ElseIf Left$(Extention, 2) <> "*." Then
  11.         Extention = "*." & Extention
  12.     End If
  13.    
  14.     File = Dir$(strPath & Extention)
  15.     Do While Len(File)
  16.         List1.AddItem File
  17.         File = Dir$
  18.     Loop
  19. End Sub
  20. Private Sub Form_Load()
  21. ListFiles "C:\", "txt"
  22. End Sub
__________________


Has someone helped you? Then you can Rate their helpful post.
manavo11 is offline   Reply With Quote
Old Jul 19th, 2004, 04:25 AM   #4
thebloke
Hyperactive Member
 
thebloke's Avatar
 
Join Date: May 03
Posts: 358
thebloke is an unknown quantity at this point (<10)
Re: VB - List All The Files In A Directory

Quote:
Originally posted by manavo11
This shows the filenames and not the total number of files in a directory.
I'll take the easy route! Works a treat. Thanks.

__________________
The Bloke
www.blokeinthekitchen.com
making cooking cool for blokes
thebloke is offline   Reply With Quote
Old Jul 20th, 2004, 09:05 AM   #5
manavo11
Super Moderator
 
manavo11's Avatar
 
Join Date: Nov 02
Location: Other side of town from si_the_geek
Posts: 7,153
manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)
Re: Re: VB - List All The Files In A Directory

Quote:
Originally posted by thebloke
I'll take the easy route! Works a treat. Thanks.

Anytime
__________________


Has someone helped you? Then you can Rate their helpful post.
manavo11 is offline   Reply With Quote
Old Mar 9th, 2005, 03:35 PM   #6
positive+
Junior Member
 
Join Date: Jan 05
Posts: 18
positive+ is an unknown quantity at this point (<10)
Re: VB - List All The Files In A Directory

Yup...this is nice, thank's
positive+ is offline   Reply With Quote
Old Mar 10th, 2005, 02:10 PM   #7
DarkX_Greece
Hyperactive Member
 
DarkX_Greece's Avatar
 
Join Date: Jan 04
Location: Athens (Greece)
Posts: 313
DarkX_Greece is on a distinguished road (20+)
Re: VB - List All The Files In A Directory

Nice code example manavo11!

Thanks, i need it!
__________________
Short CV:
  1. Visual Basic Programmer
  2. PHP Programmer

My personal web site: http://www.botonakis.com/
My services: http://www.zio.gr/
DarkX_Greece is offline   Reply With Quote
Old Mar 26th, 2005, 02:55 AM   #8
dos5731
Banned
 
Join Date: Mar 05
Posts: 26
dos5731 is an unknown quantity at this point (<10)
Re: VB - List All The Files In A Directory

it works here.
dos5731 is offline   Reply With Quote
Old Jun 16th, 2007, 09:16 AM   #9
RedJam
New Member
 
Join Date: Jun 07
Posts: 2
RedJam is an unknown quantity at this point (<10)
Thumbs up Re: VB - List All The Files In A Directory

Quote:
Originally Posted by manavo11
The Dir way :

VB Code:
  1. 'Just add a listbox (List1)
  2. Private Sub ListFiles(strPath As String, Optional Extention As String)
  3. 'Leave Extention blank for all files
  4.     Dim File As String
  5.    
  6.     If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
  7.    
  8.     If Trim$(Extention) = "" Then
  9.         Extention = "*.*"
  10.     ElseIf Left$(Extention, 2) <> "*." Then
  11.         Extention = "*." & Extention
  12.     End If
  13.    
  14.     File = Dir$(strPath & Extention)
  15.     Do While Len(File)
  16.         List1.AddItem File
  17.         File = Dir$
  18.     Loop
  19. End Sub
  20. Private Sub Form_Load()
  21. ListFiles "C:\", "txt"
  22. End Sub

Hello,

Thank for the above code, works a treat but I'm just wondering if anyone could help me out with a similarly easy way to do the same but to list the names of directories ("folders") within a directory?

So, the above code works great, it lists all the files of whatever type in say "C:\folder1".

What I need is code to list all the directories within "C:\folder1", not including subdirectories.

If anyone could help I would be much obliged.

Thanks in advance.
RedJam is offline   Reply With Quote
Old Jun 18th, 2007, 01:26 PM   #10
Hack
Super Moderator
 
Hack's Avatar
 
Join Date: Aug 01
Location: Sterling Heights, Michigan
Posts: 54,096
Hack has a brilliant future (2000+)Hack has a brilliant future (2000+)Hack has a brilliant future (2000+)Hack has a brilliant future (2000+)Hack has a brilliant future (2000+)Hack has a brilliant future (2000+)Hack has a brilliant future (2000+)Hack has a brilliant future (2000+)Hack has a brilliant future (2000+)Hack has a brilliant future (2000+)Hack has a brilliant future (2000+)
Re: VB - List All The Files In A Directory

Quote:
Originally Posted by RedJam
What I need is code to list all the directories within "C:\folder1", not including subdirectories.
Code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const LB_DIR = &H18D

Private Const DDL_DIRECTORY = &H10
Private Const DDL_ARCHIVE = &H20
Private Const DDL_EXCLUSIVE = &H8000

Private Sub Command1_Click()
    List1.Clear
    SendMessage List1.hwnd, LB_DIR, DDL_EXCLUSIVE Or DDL_DIRECTORY, ByVal "C:\folder1\*.*"
End Sub
__________________
Please use [Code]your code goes in here[/Code] tags when posting code.
When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum.

Creating A Wizard In VB.NET
Modifications Required For VB6 Apps To Work On Vista
Paging A Recordset
What is wrong with using On Error Resume Next
Good Article: Language Enhancements In Visual Basic 2010
IT professionals freelancer site. Register today to find coders, or offer your services for available freelance projects!
Upgrading VB6 Code To VB.NET
Microsoft MVP 2005/2006/2007/2008/2009/2010
Hack is offline   Reply With Quote
Old Jun 18th, 2007, 04:15 PM   #11
RedJam
New Member
 
Join Date: Jun 07
Posts: 2
RedJam is an unknown quantity at this point (<10)
Re: VB - List All The Files In A Directory

Cool, cheers mate

Had to tweak it a bit of course but working now, thanks.

J
RedJam is offline   Reply With Quote
Old Aug 13th, 2007, 01:37 AM   #12
Paul M
Interweb adm/o/distrator
 
Paul M's Avatar
 
Join Date: Nov 06
Location: Australia, Melbourne
Posts: 2,306
Paul M has a spectacular aura about (150+)Paul M has a spectacular aura about (150+)
Re: VB - List All The Files In A Directory

Here is my shot it gets all files in sub directories to!

vb Code:
  1. Public Function ListFiles(FolderPath As String, Extension As String, LstObj As ListBox)
  2. Dim i As Long
  3. Dim FolderName As String
  4. Dim DirNames() As String
  5. Dim SubDirectories As Long
  6. 'List files in the main/first folder
  7. FolderName = Dir(FolderPath & "\" & Extension, vbNormal)
  8. Do While FolderName <> vbNullString
  9.     LstObj.AddItem FolderPath & "\" & FolderName
  10.     FolderName = Dir()
  11. Loop
  12. 'Get the sub directories
  13. FolderName = Dir(FolderPath & "\*.*", vbDirectory)
  14. Do While FolderName <> vbNullString
  15.     If FolderName <> "." And FolderName <> ".." Then
  16.         SubDirectories = SubDirectories + 1
  17.         ReDim Preserve DirNames(1 To SubDirectories)
  18.         DirNames(SubDirectories) = FolderName
  19.     End If
  20.         FolderName = Dir()
  21. Loop
  22. For i = 1 To SubDirectories
  23.         ListFiles FolderPath & "\" & DirNames(i), Extension, LstObj
  24. Next i
  25. End Function

vb Code:
  1. Private Sub Command1_Click()
  2. ListFiles "C:\SomeFolder", "*.img", List1
  3. End Sub

Sorry for the bump.
__________________
If this post was helpful please rate it
If it wasn't for C, we would be using BASI, PASAL and OBOL.

VB6: [System Uptime] [Extracting WMP Song] [Basic Flood Protection] [RTF Editor]
[Hex/String Conversions] [Reading/Writing INI] [List Files in Directories]


C++: [Mute Audio in Vista]

VB.NET: [Associating a Help File] C#: [One instance of your application]
Paul M is offline   Reply With Quote
Old Apr 7th, 2008, 06:15 PM   #13
goldenix
Lively Member
 
Join Date: Apr 08
Posts: 74
goldenix is an unknown quantity at this point (<10)
Re: VB - List All The Files In A Directory

just a note that none of the codes work in 2008.
__________________

M.V.B. 2008 Express Edition
goldenix is offline   Reply With Quote
Old Apr 8th, 2008, 04:48 PM   #14
manavo11
Super Moderator
 
manavo11's Avatar
 
Join Date: Nov 02
Location: Other side of town from si_the_geek
Posts: 7,153
manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)
Re: VB - List All The Files In A Directory

The Visual Studio 2008 you mean? It won't work, it's VB6 code. That's why it's in the VB6 and earlier codebank
__________________


Has someone helped you? Then you can Rate their helpful post.
manavo11 is offline   Reply With Quote
Old Aug 30th, 2009, 10:50 PM   #15
indigofish
New Member
 
Join Date: Aug 09
Posts: 1
indigofish is an unknown quantity at this point (<10)
Re: VB - List All The Files In A Directory

[quote=Paul M;2973969]Here is my shot it gets all files in sub directories to!

[quote]

Hi

Apologies in advance for needing to ask a dumb question, but I'm pretty new to this and flying solo. I'm a tech writer, not a programmer, and have been making a few half-hearted attempts to learn VB so that I can have more control over my word docs. Problem is I am still at the starting blocks and need to catalogue all the template files in a directory, including all sub-dirs, so this post is very interesting to me, but I don't know what to do with the code!

I know enough to open the VB editor in word, and paste it into a code window, but I don't know how to name it, or how to call it. Also, there's the question of the sub tacked on at the end and how to deal with it (where does it go, etc).

Ok so banish me to the sandpit, but if someone would be kind enough to give me some steps on how to use this code first, I'll be extremely grateful. My other option is to hand type out several thousand file names.

(PS While registering for the forum, I noticed an office forum, so i will head over there and read up, but this post is the first one i found covering exactly my required task)
indigofish is offline   Reply With Quote
Old Sep 8th, 2009, 03:27 PM   #16
mustiback
Member
 
Join Date: Sep 08
Location: Turkey
Posts: 32
mustiback is an unknown quantity at this point (<10)
Re: VB - List All The Files In A Directory

Thanks For aLL!
mustiback is offline   Reply With Quote
Old Nov 3rd, 2009, 10:53 AM   #17
tamalero
New Member
 
tamalero's Avatar
 
Join Date: Oct 09
Posts: 3
tamalero is an unknown quantity at this point (<10)
Re: VB - List All The Files In A Directory

Hello there, Thanks alot for your code,
just a quick question...
is there a way to list files in a directory but get the contents arranged in some ways like "last modified" or "by name" ?
tamalero is offline   Reply With Quote
Old Nov 5th, 2009, 02:02 PM   #18
manavo11
Super Moderator
 
manavo11's Avatar
 
Join Date: Nov 02
Location: Other side of town from si_the_geek
Posts: 7,153
manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)
Re: VB - List All The Files In A Directory

I'm not sure if there is a way to get the list of files in a specific sorted order. However, since you can get all files, you can then read any information you want for each file (last modified, size, etc) and then do the sorting yourself after you get the whole list!
__________________


Has someone helped you? Then you can Rate their helpful post.
manavo11 is offline   Reply With Quote
Old Dec 18th, 2009, 04:06 AM   #19
jason8100
New Member
 
Join Date: Dec 09
Posts: 2
jason8100 is an unknown quantity at this point (<10)
Unhappy List All The Files In A Directory and it's subdirs

Hi I am making a search engine in VB6, your code works but I also need to list all files in subdirs as well. I already searched on google but I couldnt find anything. ( except the ones that cause your whole project to crash:S ) Do you know how to achieve this?

Thanks in advance,
Jason

Last edited by jason8100; Dec 19th, 2009 at 02:52 PM. Reason: wrong quote
jason8100 is offline   Reply With Quote
Old Dec 18th, 2009, 05:06 PM   #20
manavo11
Super Moderator
 
manavo11's Avatar
 
Join Date: Nov 02
Location: Other side of town from si_the_geek
Posts: 7,153
manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)
Re: VB - List All The Files In A Directory

If you look at this thread: http://www.vbforums.com/showthread.php?t=244880 it's code for listing all sub-directories. If you combine the two, then you have exactly what you want
__________________


Has someone helped you? Then you can Rate their helpful post.
manavo11 is offline   Reply With Quote
Old Dec 19th, 2009, 03:15 PM   #21
jason8100
New Member
 
Join Date: Dec 09
Posts: 2
jason8100 is an unknown quantity at this point (<10)
Unhappy Re: VB - List All The Files In A Directory

Quote:
Originally Posted by manavo11 View Post
If you look at this thread: http://www.vbforums.com/showthread.php?t=244880 it's code for listing all sub-directories. If you combine the two, then you have exactly what you want
I have tryed the code but don't get it working. It just gives a number of folders, don't know what to do with it. Could you please help me.

Thanks in advance,
Jason
jason8100 is offline   Reply With Quote
Old Dec 23rd, 2009, 12:24 AM   #22
Dennijr
New Member
 
Join Date: Oct 09
Posts: 2
Dennijr is an unknown quantity at this point (<10)
Re: VB - List All The Files In A Directory

Quote:
Originally Posted by manavo11 View Post
The Dir way :

VB Code:
  1. 'Just add a listbox (List1)
  2. Private Sub ListFiles(strPath As String, Optional Extention As String)
  3. 'Leave Extention blank for all files
  4.     Dim File As String
  5.    
  6.     If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
  7.    
  8.     If Trim$(Extention) = "" Then
  9.         Extention = "*.*"
  10.     ElseIf Left$(Extention, 2) <> "*." Then
  11.         Extention = "*." & Extention
  12.     End If
  13.    
  14.     File = Dir$(strPath & Extention)
  15.     Do While Len(File)
  16.         List1.AddItem File
  17.         File = Dir$
  18.     Loop
  19. End Sub
  20. Private Sub Form_Load()
  21. ListFiles "C:\", "txt"
  22. End Sub
Quote:
Originally Posted by goldenix View Post
just a note that none of the codes work in 2008.
this code is pretty close, the following is what i turned it into and used, in vb.net 2008:

Code:
Dim file As String
        file = Dir$("D:\Documents and Settings\Compaq_Owner\My Documents\My Pictures\cards\*.png")
        Do While Len(file)
            ListBox2.Items.Add(Mid(file, 1, InStr(file, ".") - 1))
            file = Dir$()
        Loop
Dennijr is offline   Reply With Quote
Old Jan 19th, 2010, 09:54 AM   #23
techker
Member
 
Join Date: Nov 09
Posts: 56
techker is an unknown quantity at this point (<10)
Re: VB - List All The Files In A Directory

cold we modify this for viewing the images?
techker is offline   Reply With Quote
Old Jan 19th, 2010, 05:29 PM   #24
manavo11
Super Moderator
 
manavo11's Avatar
 
Join Date: Nov 02
Location: Other side of town from si_the_geek
Posts: 7,153
manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)
Re: VB - List All The Files In A Directory

You could specify an extension. It is mentioned in the examples if you read through them carefully.
__________________


Has someone helped you? Then you can Rate their helpful post.
manavo11 is offline   Reply With Quote
Old Jan 19th, 2010, 05:37 PM   #25
techker
Member
 
Join Date: Nov 09
Posts: 56
techker is an unknown quantity at this point (<10)
Re: VB - List All The Files In A Directory

Quote:
Originally Posted by manavo11 View Post
You could specify an extension. It is mentioned in the examples if you read through them carefully.
well itryed just this but it shows nothing

Dim file As String
file = Dir$("D:\Documents and Settings\Compaq_Owner\My Documents\My Pictures\cards\*.png")
Do While Len(file)
ListBox2.Items.Add(Mid(file, 1, InStr(file, ".") - 1))
file = Dir$()
Loop
techker is offline   Reply With Quote
Old Jan 20th, 2010, 05:58 PM   #26
manavo11
Super Moderator
 
manavo11's Avatar
 
Join Date: Nov 02
Location: Other side of town from si_the_geek
Posts: 7,153
manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)manavo11 is a jewel in the rough (300+)
Re: VB - List All The Files In A Directory

Try starting with the code from the third post: http://www.vbforums.com/showpost.php...79&postcount=3

It seems like the most similar to the code you have. Compare it, find the differences and I'm sure you'll be able to work it out
__________________


Has someone helped you? Then you can Rate their helpful post.
manavo11 is offline   Reply With Quote
Old Jan 20th, 2010, 07:26 PM   #27
Nightwalker83
Web developer
 
Nightwalker83's Avatar
 
Join Date: Dec 01
Location: Adelaide, Australia
Posts: 4,539
Nightwalker83 has a spectacular aura about (150+)Nightwalker83 has a spectacular aura about (150+)
Re: VB - List All The Files In A Directory

Quote:
Originally Posted by manavo11 View Post
This shows the filenames and not the total number of files in a directory.
What is the difference between this and using the filelist box?
Nightwalker83 is offline   Reply With Quote
Old Jan 21st, 2010, 05:20 AM   #28
techker
Member
 
Join Date: Nov 09
Posts: 56
techker is an unknown quantity at this point (<10)
Re: VB - List All The Files In A Directory

Quote:
Originally Posted by manavo11 View Post
The Dir way :

VB Code:
  1. 'Just add a listbox (List1)
  2. Private Sub ListFiles(strPath As String, Optional Extention As String)
  3. 'Leave Extention blank for all files
  4.     Dim File As String
  5.    
  6.     If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
  7.    
  8.     If Trim$(Extention) = "" Then
  9.         Extention = "*.*"
  10.     ElseIf Left$(Extention, 2) <> "*." Then
  11.         Extention = "*." & Extention
  12.     End If
  13.    
  14.     File = Dir$(strPath & Extention)
  15.     Do While Len(File)
  16.         List1.AddItem File
  17.         File = Dir$
  18.     Loop
  19. End Sub
  20. Private Sub Form_Load()
  21. ListFiles "C:\", "txt"
  22. End Sub
why do i get errors?
i added a list box named it List1

Error 1 Optional parameters must specify a default value. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 3 74 WindowsApplication1

Error 2 'Public ReadOnly Property Right() As Integer' has no parameters and its return type cannot be indexed. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 7 12 WindowsApplication1

Error 4 'Public Property Left() As Integer' has no parameters and its return type cannot be indexed. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 11 16 WindowsApplication1

Error 5 Type character '$' does not match declared data type 'Integer'. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 11 16 WindowsApplication1

Error 6 'AddItem' is not a member of 'System.Windows.Forms.ListBox'. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 17 13 WindowsApplication1
techker is offline   Reply With Quote
Old Jan 21st, 2010, 10:09 AM   #29
si_the_geek
Super Moderator
 
si_the_geek's Avatar
 
Join Date: Jul 02
Location: Bristol, UK
Posts: 29,676
si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)
Re: VB - List All The Files In A Directory

You are getting errors because you are in the wrong place... this is the VB6 Codebank, but you are using VB.Net

Post #22 is the only thing from this thread that you should consider using - for anything else (including help fixing issues with it) you should post in the VB.Net forum.
si_the_geek is offline   Reply With Quote
Reply

Go Back   VBForums > VBForums CodeBank > CodeBank - Visual Basic 6 and earlier


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:04 PM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.