-
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
-
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.
-
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
-
Have a look at the Dir function.
-
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.
-
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
-
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.