Counting files in a Dir

KummaZ
10-18-2003, 04:50 AM
How would you go about counting how many files were in a directory?
Thankyou for your time.

wilbert
10-18-2003, 05:07 AM
You might have a look at this thread:
http://www.visualbasicforum.com/t5050.html

Greets,
Wilbert

KummaZ
10-18-2003, 06:31 AM
I am confused after reading that. I just want to search a directory that I specify for files and add them to a list one by one in vb. That way I can count how many files are in the list and I will have the number I am looking for.

Robse
10-18-2003, 06:44 AM
Yes the tutorial is slightly advanced. An easier way to accomplish this
would be to use the Dir statement in a loop, and an array if
you want to add all the files. There is an exmple in your MSDN I think,
and you can also search this site.

KummaZ
10-18-2003, 07:04 AM
I'm not sure on how to use the Dir statement. This is the first time in a long while where I have used anything to do with FileSystems in vb. An example would be great. Thanks for your help so far.

Robse
10-18-2003, 07:34 AM
http://www.visualbasicforum.com/showpost.php?postid=474216&postcount=3

Use vbNormal instead of vbDirectory to scan for files.

KummaZ
10-18-2003, 08:11 AM
Dim MyPath As String
Dim MyName As String
MyPath = "E:\Documents and Settings\Michael\My Documents\Visual Basic Programs\Information"
MyName = dir(MyPath, vbDirectory)
Do While MyName <> "" ' Start the loop.
If MyName <> "." And MyName <> ".." Then
If (GetAttr(MyPath & MyName) And vbNormal) = vbNormal Then
MsgBox MyName ' Display entry only if it
End If
End If
MyName = dir
Loop


That is my code. Why doesn't it show me the text files that are in that directory? I am stuck. Thanks for your help so far.

Robse
10-18-2003, 08:22 AM
OK hmm that was just an example... a somewhat simpler way to do it:

Dim sFileName As String

'Get all .txt files in the directory
sFileName = Dir$("E:\Documents and Settings\Michael\My Documents\Visual Basic Programs\Information\*.txt", vbNormal)

Do While Len(sFileName) > 0
Debug.Print sFileName
sFileName = Dir$
Loop


Edit: It didn't work because of line 4:

MyName = dir(MyPath, vbDirectory)
'Change to: MyName = dir(MyPath, vbNormal)

KummaZ
10-18-2003, 08:29 AM
I had it as normal and it didn't work so I just left it the way it originally was. Thanks for the help. I wonder what was wrong then?

Robse
10-18-2003, 08:38 AM
Not sure. I copy & pasted your code and modified to vbNormal,
changed the path to "c:\", and it worked for me... :confused:

KummaZ
10-18-2003, 08:53 AM
oh well. I got it working now. Its just that I am having problems with line input. How do I just input the first line? Thanks you have been a great help.

Robse
10-18-2003, 08:59 AM
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vastmlineinputx.asp

KummaZ
10-18-2003, 09:03 AM
So line input should only input the first line? Well if thats so why does my program keep reading through all the lines? I thought thats what i was supposed to do. I just want to read line 1 into a variable and check it and then close that file.

Robse
10-18-2003, 09:09 AM
No, Line Input reads a file's contents line by line. If you only
want the first line, use a counter as criteria to stop loop execution
where you read the file in (Do Until EOF(1)...Loop or similar)

KummaZ
10-18-2003, 09:44 AM
Ok I figured out the problem. Now I have forgotten it cos I been doing all this other stuff. Anyway it was real easy and thanks for all the help you gave me.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum