Results 1 to 11 of 11

Thread: finding directories and subdirectories

  1. #1

    Thread Starter
    Addicted Member reznor's Avatar
    Join Date
    May 2001
    Location
    Netherlands
    Posts
    151

    finding directories and subdirectories

    I need to write a programm that finds all the directories on a my c-drive with a certain name. For example I need to find all directory or subdirectorynames which end with \temp. On my drive I have like c:\temp, c:\games\temp and c:\internet\temp. I would like to write a programm that lists these 3 directories in say a listbox.

    I need some hints.
    Anybody????
    Last edited by reznor; Jul 2nd, 2001 at 04:56 AM.
    "Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination." - Albert Einstein

  2. #2
    Hyperactive Member FATBOYPEE's Avatar
    Join Date
    May 2001
    Location
    Charleville (Ireland) Still. ANYONE GOT A JOB I CAN HAVE ? GIZA JOB. I CAN DO THAT....
    Posts
    463

    Thumbs up

    I have been slagged off before in this forum for mentioning this, but here I go again !!

    scrrun.DLL is the Microsoft Scripting Runtime library which neatly packages alot of Disk/DIR & file utilities. Once you investigate some of the functions it provides, you may well find it invaluable.

    Also, as it's shipped with Windows, you only need reference it, & not package it with your app.

    There are lots of different methods & functions, the one you may find useful is FolderExists

    FATBOY

    Best Bar.....

  3. #3

    Thread Starter
    Addicted Member reznor's Avatar
    Join Date
    May 2001
    Location
    Netherlands
    Posts
    151

    Thumbs up Thanx!

    Thanx,
    I can loop through the folders collection to solve my problem!!
    "Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination." - Albert Einstein

  4. #4
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    I have done this in C++, but I will try and translate some of it into vb for u

  5. #5

    Thread Starter
    Addicted Member reznor's Avatar
    Join Date
    May 2001
    Location
    Netherlands
    Posts
    151
    Damn, still can't get it to work. That scripting stuff is better used for VBScript in html.
    What I would like is the functionality every windows PC has. I mean under start->find->Files or Folders... This little programm gives me almost what I want. In it you select a drive and in the advanced tab you select 'of type: folder' and it displays all directories on that drive.
    This programm uses API calls I think, but I can't get FindFirstFile and FindNextFile to work to mimick it.
    If I could get a listbox with all directory names of a drive then I could work from that.

    Help needed!
    "Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination." - Albert Einstein

  6. #6
    Hyperactive Member FATBOYPEE's Avatar
    Join Date
    May 2001
    Location
    Charleville (Ireland) Still. ANYONE GOT A JOB I CAN HAVE ? GIZA JOB. I CAN DO THAT....
    Posts
    463
    reznor,

    At the risk of sounding 'churlish', would the commondialog not do it for you ?

    the Scripting runtime does use the API's but perhaps what you want to do is a tad deeper. Try:

    www.vbapi.com/ref/f/findfirstfile.html

    unless you have already.

    or for NextFile:


    www.vbapi.com/ref/f/Findnextfile.html

    Each of which are full vb snippets. I've used both before to great success though I cannot find the VBP at the mo, otherwise I'd sent it on.

    FATBOY.

    Best Bar.....

  7. #7
    Hyperactive Member FATBOYPEE's Avatar
    Join Date
    May 2001
    Location
    Charleville (Ireland) Still. ANYONE GOT A JOB I CAN HAVE ? GIZA JOB. I CAN DO THAT....
    Posts
    463

    Best Bar.....

  8. #8
    Hyperactive Member FATBOYPEE's Avatar
    Join Date
    May 2001
    Location
    Charleville (Ireland) Still. ANYONE GOT A JOB I CAN HAVE ? GIZA JOB. I CAN DO THAT....
    Posts
    463
    http://www.mvps.org/vbnet/code/filea...renumbasic.htm

    more like the link !!!!!!! Apologies....

    Best Bar.....

  9. #9

    Thread Starter
    Addicted Member reznor's Avatar
    Join Date
    May 2001
    Location
    Netherlands
    Posts
    151
    thanks,
    I use www.vbapi.com a lot, but I had some problems with the WIN32_FIND_DATA type. It contains another type called FILETIME. I got a compile error. Does it matter which type is declared first? Haven't tried that yet.

    The commondialog will not do.

    My problem is this. On a network drive, somewhere deep down in the directory structure there are a few subdir's with identical names. What I need to build is a little programm that does a filecount on those subdir's and gives a total. I want to make it generic, so they can give a diectory name, and the programm searches the drive and counts all the files that are in a directory with that name. They just don't want to do a manual count.

    Hope it's a little more clear.
    "Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination." - Albert Einstein

  10. #10

    Thread Starter
    Addicted Member reznor's Avatar
    Join Date
    May 2001
    Location
    Netherlands
    Posts
    151

    Thumbs up

    FATBOYPEE,
    thanx for the links, looks promising!
    "Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination." - Albert Einstein

  11. #11
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Try this.

    In a module:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const MAX_PATH = 260
    4. Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
    5.  
    6. Public Type FILETIME
    7.         dwLowDateTime As Long
    8.         dwHighDateTime As Long
    9. End Type
    10.  
    11. Public Type WIN32_FIND_DATA
    12.         dwFileAttributes As Long
    13.         ftCreationTime As FILETIME
    14.         ftLastAccessTime As FILETIME
    15.         ftLastWriteTime As FILETIME
    16.         nFileSizeHigh As Long
    17.         nFileSizeLow As Long
    18.         dwReserved0 As Long
    19.         dwReserved1 As Long
    20.         cFileName As String * MAX_PATH
    21.         cAlternate As String * 14
    22. End Type
    23.  
    24. Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    25. Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    26. Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    27.  
    28. Private Dirs()          As String
    29.  
    30. Public Function FindDirectories(ByVal Path As String, DirName As String) As Variant
    31.  'Nucleus
    32.  ReDim Dirs(1 To 1)
    33.  Call EnumDirs(Path, DirName)
    34.  If Len(Dirs(1)) Then FindDirectories = Dirs Else FindDirectories = Null
    35.  Erase Dirs
    36. End Function
    37.  
    38. Private Sub EnumDirs(ByVal Path As String, DirName As String)
    39.  Dim hFirstFound         As Long
    40.  Dim hFound              As Long
    41.  Dim WFD                 As WIN32_FIND_DATA
    42.  Dim fname               As String
    43.  
    44.  If Right$(Path, 1) <> "\" Then Path = Path & "\"
    45.  hFirstFound = FindFirstFile(Path & "*.*", WFD)
    46.  hFound = (hFirstFound > 0)
    47.  Do While hFound
    48.     fname = Left(WFD.cFileName, InStr(1, WFD.cFileName, Chr(0)) - 1)
    49.     If WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY Then
    50.         If fname <> "." And fname <> ".." Then
    51.             Call EnumDirs(Path & fname, DirName)
    52.             If fname = DirName Then
    53.                 If Len(Dirs(1)) Then ReDim Preserve Dirs(1 To UBound(Dirs) + 1)
    54.                 Dirs(UBound(Dirs)) = Path & fname
    55.             End If
    56.         End If
    57.     End If
    58.     hFound = FindNextFile(hFirstFound, WFD)
    59.  Loop
    60.  
    61. End Sub
    62.  
    63.  
    64. Public Function EnumerateFiles(ByVal Path As String) As Variant
    65.  Dim hFirstFound         As Long
    66.  Dim hFound              As Long
    67.  Dim WFD                 As WIN32_FIND_DATA
    68.  Dim fname               As String
    69.  Dim Files()             As String: ReDim Files(1 To 1)
    70.  
    71.  If Right$(Path, 1) <> "\" Then Path = Path & "\"
    72.  hFirstFound = FindFirstFile(Path & "*.*", WFD)
    73.  hFound = (hFirstFound > 0)
    74.  Do While hFound
    75.     fname = Left(WFD.cFileName, InStr(1, WFD.cFileName, Chr(0)) - 1)
    76.     If fname <> "." And fname <> ".." Then
    77.         If Len(Files(1)) Then ReDim Preserve Files(1 To UBound(Files) + 1)
    78.         Files(UBound(Files)) = Path & fname
    79.     End If
    80.     hFound = FindNextFile(hFirstFound, WFD)
    81.  Loop
    82.  
    83.  If Len(Files(1)) Then EnumerateFiles = Files Else EnumerateFiles = Null
    84. End Function

    Example usage:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim a
    3. Dim i&
    4.  
    5. a = FindDirectories("c:\tmp", "Custom Titlebar")
    6. If Not IsNull(a) Then
    7.     For i = 1 To UBound(a)
    8.         Debug.Print a(i)
    9.     Next
    10. Else
    11.     Debug.Print "No directories"
    12. End If
    13. End Sub

    What this does is find and list directories with the same name.

    Once you have this you can pass each directory to the EnumerateFiles function I included which enumerates all the files in a directory.

    Now you have all the code you need.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width