Results 1 to 7 of 7

Thread: Accessing files and directories from a module...

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2000
    Posts
    13

    Question

    I know this is probably easy, but don't know quite how to do it. Previously every time that I needed to access files, I would put a directory and file control on a form and make it invisible so the user does not see it. Now I am trying to access files from a module and don't quite know the proper way to do it. I tried to declare a DirListBox in my code, but it didn't seem to like that. Could someone help me out?

    Thanks,

    Shawn

    VB5, WinNT

  2. #2
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655

    Thumbs up

    Well there are several functions out there that are built into VB that you can use:

    This function will open a file for appending (it will add to the end of the file:

    Open FileName For Append As #1
    'add something to the file...
    'use the print statement
    Close #1 'Close the file

    This function will open a file/create a file for putting information into it (it will overwrite the current file):

    Open FileName For Output As #1
    Print #1, SomeInformation
    Close #1

    This function will pull info from a file:

    Open FileName For Input As #1
    Do Until EOF(1)
    Line Input #1, LineOfText
    txt1.Text = txt1.Text & LineOfText & vbCrLf
    Loop
    Close #1

    Check your help file for other file functions. There are a ton of good ones in there.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2000
    Posts
    13

    more...

    Ok, I know how to do that, I guess I should have been more explicit. What I am trying to get at is the directory information, such as how many files and the names of the files in the directory.

    Shawn

  4. #4
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    Have a look at the Dir function.

  5. #5
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    I have a found a cool vb project that does this, also allows for recursion, so it will go done into subdirectories if wanted, it also as option to print to file or printer, and would not take much to convert it to print to an excel spreadsheet and really make it look nice, if you are interested in it, just post a reply.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Ok for single directory it's simple
    Code:
        dirname = Dir(path, 63) ' Get first directory name.
        Do While len(dirname)
         '... your code go here
         dirname=Dir
        Loop
    for subdirectories you have my usercontrol with raisevents, you don't actually have to make the usercontrol and the event's, just replace the raiseevent statements with your own.
    Code:
    Event Filecatch(File$, path$, level%)
    Event Direcatch(dire$, path$, level%)
    Public curlevel%
    Public filemax&
    Public diremax&
    Public tid#
    Sub explore(startdir$, Optional pauses = 100)
        tids = Timer
        filemax = 0: diremax = 0: Totalb = 0: curlevel = 0
        If Right(startdir, 1) <> "\" Then startdir = startdir & "\"
        ListSubDirs startdir, pauses
    
        tid = Timer - tids
    End Sub
    
    Sub ListSubDirs(path$, pauses)
    Dim i&, dmax&, dirname$, dire$()   ' Declare variables.
        dirname = Dir(path, 63) ' Get first directory name.
        Do While len(dirname)
            If dirname <> "." And dirname <> ".." Then
                If Int(GetAttr(path + dirname) / 16) Mod 2 = 1 Then
                        
                        If (dmax Mod 10) = 0 Then
                            ReDim Preserve dire(dmax + 10)    ' Resize the array.
                        End If
                    diremax = diremax + 1: dmax = dmax + 1
                    dire(dmax) = dirname
                    RaiseEvent Direcatch(dirname, path, curlevel)
                Else
                    filemax = filemax + 1
                    RaiseEvent Filecatch(dirname, path, curlevel)
                End If
            End If
                dirname = Dir   ' Get another directory name.
            waiter = waiter + 1: If waiter Mod (pauses) = 1 Then DoEvents
        Loop
        For i = 1 To dmax
            curlevel = curlevel + 1
            ListSubDirs path & dire(i) & "\", pauses
        Next i
        curlevel = curlevel - 1
    End Sub
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Guest
    I posted something similar just yesterday, have a few responses on it. Take a look, the topic is:

    Help with retrieving file names.

    It is not to far down yet, so easy to find. There might be some info in that thread that could help you.

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