vb.net search network drive

C

Computer geek

Hello, I am new to VB.NET and programming in general. I have taught
myself a lot of the basics with vb.net but am still quite the novice.
I am working on a little application now and I need some help with one
part of the code. When a button is clicked I need to have it go out to
a network drive location and count how many files are present with a
certain file extension. Then store that number in a declared variable.
Is this possible? Can someone help? Also, would this be called a
method, function, or event procedure? Not exactly sure what the
difference is yet.
 
R

rowe_newsgroups

Hello, I am new to VB.NET and programming in general. I have taught
myself a lot of the basics with vb.net but am still quite the novice.
I am working on a little application now and I need some help with one
part of the code. When a button is clicked I need to have it go out to
a network drive location and count how many files are present with a
certain file extension. Then store that number in a declared variable.
Is this possible? Can someone help? Also, would this be called a
method, function, or event procedure? Not exactly sure what the
difference is yet.

Here's a quick module that I wrote a long time ago for another poster.
Beware I air-modified it just now to let you specify the extension
type so it might have some bugs. You should be able to modify the
procedure to count the files and return that value.

Let me know how it works out (or if you need any help modifying it)

Thanks,

Seth Rowe


Module Module1

Sub Main()
ListAllFiles("C:/", "exe")
Console.WriteLine("done")
Console.Read()
End Sub

' Pass just the extension --> i.e. just exe not .exe or *.exe
Private Sub ListAllFiles(ByVal path As String, ByVal extension As
String)
Try
Dim filenames() As String =
System.IO.Directory.GetFiles(path, String.Format("*.{0}", extension)
For i As Int16 = 0 To filenames.Length - 1
' Possibly increment your counter here
Console.WriteLine(filenames(i))
Next
Dim directories() As String =
System.IO.Directory.GetDirectories(path)
For i As Int16 = 0 To directories.Length - 1
ListAllFiles(directories(i), extension)
Next
Catch ex As Exception
' do nothing
End Try
End Sub

End Module
 
S

ShaneO

Computer said:
Hello, I am new to VB.NET and programming in general. I have taught
myself a lot of the basics with vb.net but am still quite the novice.
I am working on a little application now and I need some help with one
part of the code. When a button is clicked I need to have it go out to
a network drive location and count how many files are present with a
certain file extension. Then store that number in a declared variable.
Is this possible? Can someone help? Also, would this be called a
method, function, or event procedure? Not exactly sure what the
difference is yet.

If you are wanting to Count the files, the following code will do
exactly that (watch for line wrapping) -

Dim sFiles() As String = Directory.GetFiles("C:\Windows", "*.exe",
SearchOption.AllDirectories)
MsgBox(String.Format("Number of EXE Files Found = {0}", UBound(sFiles) - 1))


ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
S

ShaneO

ShaneO said:
If you are wanting to Count the files, the following code will do
exactly that (watch for line wrapping) -

Dim sFiles() As String = Directory.GetFiles("C:\Windows", "*.exe",
SearchOption.AllDirectories)
MsgBox(String.Format("Number of EXE Files Found = {0}", UBound(sFiles) -
1))


ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.

MISTAKE: Sorry, the second line is supposed to be "+ 1", not "- 1".

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
S

ShaneO

ShaneO said:
If you are wanting to Count the files, the following code will do
exactly that (watch for line wrapping) -

Dim sFiles() As String = Directory.GetFiles("C:\Windows", "*.exe",
SearchOption.AllDirectories)
MsgBox(String.Format("Number of EXE Files Found = {0}", UBound(sFiles) +
1))

I must apologise to all the new programmers out there. I should also
have pointed out that you'll need the "System.IO" NameSpace in order to
use my code example. (I received an email regarding this!)

NameSpaces are declared at the top of your Code, (above Module or Class
statements) and in this case, it should read -

Imports System.IO

You could also change to a fully qualified version on the first line of
my example like so -

Dim sFiles() As String = System.IO.Directory.GetFiles("C:\Windows",
"*.exe", System.IO.SearchOption.AllDirectories)


but that would create a lot of unnecessary typing.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
C

Computer geek

I must apologise to all the new programmers out there. I should also
have pointed out that you'll need the "System.IO" NameSpace in order to
use my code example. (I received an email regarding this!)

NameSpaces are declared at the top of your Code, (above Module or Class
statements) and in this case, it should read -

Imports System.IO

You could also change to a fully qualified version on the first line of
my example like so -

Dim sFiles() As String = System.IO.Directory.GetFiles("C:\Windows",
"*.exe", System.IO.SearchOption.AllDirectories)

but that would create a lot of unnecessary typing.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.

OK Let me try this again. I tried posting yesterday but for some
reason it didn't show up. First of all, I want to thank both Seth and
Shane for helping me. I got it to work both ways.. using NameSpace and
a fully qualified version. However, although it works I still dont
understand why it works (I'm the kind of geek that has to know why).
So can anyone help me understand the code:

Dim sFiles() As String = Directory.GetFiles("C:\Windows", "*.exe",
SearchOption.AllDirectories)

I know the first part is the variable declaration but what about the
second part... Directory.GetFiles and SearchOption.AllDirectories

Also the MsgBox statement from the previous post... what does the {0}
mean? and what does UBound(sFiles) mean?

Any help/advice would be greatly appreciated. I just want a firm
understanding of it while I try to learn how to program. Thanks.
 
R

rowe_newsgroups

So can anyone help me understand the code:
Dim sFiles() As String = Directory.GetFiles("C:\Windows", "*.exe",
SearchOption.AllDirectories)

I'll try :)

This statement accomplishes the same thing as my recursively called
ListAllFiles sub if that helps.

First we'll break down what the different parts do:

Directory.GetFiles("C:\Windows", "*.exe", SearchOption.AllDirectories)

This part performs a search the specified directory ("C:\Windows") and
all it's subdirectories (SearchOption.AllDirectories) for any file
that matches the search condition ("*.exe").

Dim sFiles() As String

This declares variable named sFiles that stores an Array (think of a
numbered list) of string values. If you look at the documentation for
Directory.GetFiles, you'll see that it is a function that returns an
array of strings - so what we are doing is matching up the sFiles()
variable with the output of GetFiles.

So now we have a "numbered list" of the names of the files that
GetFiles(...) found, but how do we how many we found? This is where
UBound comes into play. UBound will return number of the last item so
if the array contains 70 items it will return 69 (read on). It returns
69 because an array is zero-based meaning that an array with 10 items
will have items numbered 0,1,2,3,4,5,6,7,8,9 and a UBound of 9. So to
get the actual count of items you need to add 1 to the UBound (or just
use the array's length property)

And know for {0}:

String.Format is a pretty cool function that lets you combine strings.
It works by using placeholders ({n}) that refer to the parameters you
pass it:

For example, String.Format("{0} {1} {2} {1} {0}", "Seth", "says",
"hi") will return a string that says "Seth says hi says Seth". What
it does it just match up the placeholder with the parameter in the
specified position.

Let us know if you need further help!

Thanks,

Seth Rowe
 
C

Computer geek

I'll try :)

This statement accomplishes the same thing as my recursively called
ListAllFiles sub if that helps.

First we'll break down what the different parts do:

Directory.GetFiles("C:\Windows", "*.exe", SearchOption.AllDirectories)

This part performs a search the specified directory ("C:\Windows") and
all it's subdirectories (SearchOption.AllDirectories) for any file
that matches the search condition ("*.exe").

Dim sFiles() As String

This declares variable named sFiles that stores an Array (think of a
numbered list) of string values. If you look at the documentation for
Directory.GetFiles, you'll see that it is a function that returns an
array of strings - so what we are doing is matching up the sFiles()
variable with the output of GetFiles.

So now we have a "numbered list" of the names of the files that
GetFiles(...) found, but how do we how many we found? This is where
UBound comes into play. UBound will return number of the last item so
if the array contains 70 items it will return 69 (read on). It returns
69 because an array is zero-based meaning that an array with 10 items
will have items numbered 0,1,2,3,4,5,6,7,8,9 and a UBound of 9. So to
get the actual count of items you need to add 1 to the UBound (or just
use the array's length property)

And know for {0}:

String.Format is a pretty cool function that lets you combine strings.
It works by using placeholders ({n}) that refer to the parameters you
pass it:

For example, String.Format("{0} {1} {2} {1} {0}", "Seth", "says",
"hi") will return a string that says "Seth says hi says Seth". What
it does it just match up the placeholder with the parameter in the
specified position.

Let us know if you need further help!

Thanks,

Seth Rowe








- Show quoted text -

OK... So in the code:

MsgBox(String.Format("Number of EXE files found = {0}, UBound(sFiles)
+ 1))

Why does {0} return the number of files... seems like it would return
the name of the first file it found because UBound is useed after
calling {0}.
 
R

rowe_newsgroups

OK... So in the code:

MsgBox(String.Format("Number of EXE files found = {0}, UBound(sFiles)
+ 1))

Why does {0} return the number of files... seems like it would return
the name of the first file it found because UBound is useed after
calling {0}.


{0} is replaced by the value of (UBound(sFiles) + 1) as UBound returns
the number of the last string not the string itself. To get the string
you would need to do sFiles(UBound(sFiles)).

Thanks,

Seth Rowe
 
S

ShaneO

Well done Seth, I couldn't have explained it any better myself.


Yes, if I'd used "sFiles.Length" instead of "UBound(sFiles)" I would not
have made my original mistake of hitting the minus sign instead of the
plus sign. I guess old habits are sometimes hard to break and I need to
dispense with using UBound.

So now you can see what the {0} does, my second line should really have
been -

MsgBox(String.Format("Number of EXE Files Found = {0}", sFiles.Length))


ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
C

Computer geek

Well done Seth, I couldn't have explained it any better myself.


Yes, if I'd used "sFiles.Length" instead of "UBound(sFiles)" I would not
have made my original mistake of hitting the minus sign instead of the
plus sign. I guess old habits are sometimes hard to break and I need to
dispense with using UBound.


So now you can see what the {0} does, my second line should really have
been -

MsgBox(String.Format("Number of EXE Files Found = {0}", sFiles.Length))

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.- Hide quoted text -

- Show quoted text -

Thanks for the help guys. How about when I add things to a listbox. Is
there a way pause shortly between different lines you send to a
listbox?
 
R

rowe_newsgroups

Thanks for the help guys. How about when I add things to a listbox. Is
there a way pause shortly between different lines you send to a
listbox?

Could you post your code?

Two ways are to use either System.Threading.Thread.Sleep(# of
milliseconds) or to set up the routine on a timer.

Thanks,

Seth Rowe
 
C

Computer geek

Could you post your code?

Two ways are to use either System.Threading.Thread.Sleep(# of
milliseconds) or to set up the routine on a timer.

Thanks,

Seth Rowe- Hide quoted text -

- Show quoted text -

The purpose of this program is to check various system functions at
the beginning of the day. The first one I'm doing is to check for fax
files that did not get sent over night. This is what I have so far:

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnStart.Click

'Checks network drive for files of .fax type
Dim sFiles() As String = System.IO.Directory.GetFiles("K:
\AutoFax\", String.Format("*.{0}", "fax"))

'Displays the number of fax files in the listbox
lstResults.Items.Add(String.Format("There are " &
UBound(sFiles) + 1 & " fax files waiting to be sent"))

End Sub

Ideally, I'd like to use an IF statment to test if the are more than 0
files to display what I have above. And if not, display a differnt
message. But I was not able to get an IF statement to work. I think it
has something to do with how you test a String value... The reason I
asked about pausing was because after it does the check for faxes,
I'll want it to pause briefly before moving to the next step. Just so
the display doesn't fill up real fast.
 
R

rowe_newsgroups

Ideally, I'd like to use an IF statment to test if the are more than 0
files to display what I have above. And if not, display a differnt
message. But I was not able to get an IF statement to work. I think it
has something to do with how you test a String value...

If sFiles.Length = 0 Then
Msgbox("No files found")
Else
Msgbox(String.Format("{0} Files found", sFiles.Length)
Endif
I'll want it to pause briefly before moving to the next step. Just so
the display doesn't fill up real fast.

While you could use Thread.Sleep or a timer to do this, I wouldn't
recommend it. Instead why not have have different buttons to do the
different tasks? Like have one for finding unsent faxes, another for
checking network connectivity, etc. That way you don't have to sit
through all the tasks just to get to the one you want to see. And if
you want a "do all" button that button can call the PerformClick()
method on the others, however you would need to implement some sort of
timer here. If you need help wiring up a timer let us know - we'll be
happy to help.

Thanks,

Seth Rowe
 
M

Mr.JohnAllen

If sFiles.Length = 0 Then
Msgbox("No files found")
Else
Msgbox(String.Format("{0} Files found", sFiles.Length)
Endif


While you could use Thread.Sleep or a timer to do this, I wouldn't
recommend it. Instead why not have have different buttons to do the
different tasks? Like have one for finding unsent faxes, another for
checking network connectivity, etc. That way you don't have to sit
through all the tasks just to get to the one you want to see. And if
you want a "do all" button that button can call the PerformClick()
method on the others, however you would need to implement some sort of
timer here. If you need help wiring up a timer let us know - we'll be
happy to help.

Thanks,

Seth Rowe









- Show quoted text -

WOW... a timer. Would that be a little out of the bounds of a beginner
programmer like myself? I actually did want it to perform all the
functions with one click because the idea of it is for when I am on
vacation or out of the office, my boss can just click the button and
see what the status is on everything. That way if something is out of
sort she can call me and I can take care of it. I hate coming into
work after days off and things are all screwed up. Know what I
mean :) So this is giving me a good reason to start to learn how to
program. But before I go any further with that.... what about error
checking (my book is very vague about it). Seems like I remember
seeing some code somewhere like "On Err ...... Go to next" or
something like that. Is that something I should learn and put into
place? Like maybe it can't find the K:\ drive I specified. Let me know
what you think.
 
S

ShaneO

Computer said:
The purpose of this program is to check various system functions at
the beginning of the day. The first one I'm doing is to check for fax
files that did not get sent over night. This is what I have so far:

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnStart.Click

'Checks network drive for files of .fax type
Dim sFiles() As String = System.IO.Directory.GetFiles("K:
\AutoFax\", String.Format("*.{0}", "fax"))

'Displays the number of fax files in the listbox
lstResults.Items.Add(String.Format("There are " &
UBound(sFiles) + 1 & " fax files waiting to be sent"))

End Sub

Ideally, I'd like to use an IF statment to test if the are more than 0
files to display what I have above. And if not, display a differnt
message. But I was not able to get an IF statement to work. I think it
has something to do with how you test a String value... The reason I
asked about pausing was because after it does the check for faxes,
I'll want it to pause briefly before moving to the next step. Just so
the display doesn't fill up real fast.

Why not just place a Label above your ListBox, that way the Label won't
scroll-off the page if there's a lot of files being listed -

Dim sFiles() As String = System.IO.Directory.GetFiles("K:\AutoFax\",
"*.fax")
If sFiles.Length > 0 Then
Label1.Text = String.Format("There are {0} fax files waiting to be
sent", sFiles.Length)
Else
Label1.Text = "All faxes have been successfully sent."
End If

Note the following points:

1. I've removed your String.Format function from the first line. It was
illogical to use it with a known value like "fax". If you know the
value, then just use it in the string.
2. I've included the "If" that you mentioned you wanted.
3. Generally, watch out for the way you're using "String.Format". Take
another look at your second line.
4. This may be a little ahead of you at this time, but, if you decide to
use the Label option I've suggested and you're going to check for
additional files, do your next check and just add to the already
displayed Label Text. eg. - Label1.Text = Label1.Text & vbCrLf &
String.Format("There are also {0} whatever files waiting to be sent",
sWhatever.Length)

I hope this helps you.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
C

Computer geek

Why not just place a Label above your ListBox, that way the Label won't
scroll-off the page if there's a lot of files being listed -

Dim sFiles() As String = System.IO.Directory.GetFiles("K:\AutoFax\",
"*.fax")
If sFiles.Length > 0 Then
Label1.Text = String.Format("There are {0} fax files waiting to be
sent", sFiles.Length)
Else
Label1.Text = "All faxes have been successfully sent."
End If

Note the following points:

1. I've removed your String.Format function from the first line. It was
illogical to use it with a known value like "fax". If you know the
value, then just use it in the string.
2. I've included the "If" that you mentioned you wanted.
3. Generally, watch out for the way you're using "String.Format". Take
another look at your second line.
4. This may be a little ahead of you at this time, but, if you decide to
use the Label option I've suggested and you're going to check for
additional files, do your next check and just add to the already
displayed Label Text. eg. - Label1.Text = Label1.Text & vbCrLf &
String.Format("There are also {0} whatever files waiting to be sent",
sWhatever.Length)

I hope this helps you.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.- Hide quoted text -

- Show quoted text -

Do either of you know of any good online resources that clearly define
different functions/methods in vb.net? I've found a couple sites but
they are not very intuitive for a beginner like myself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top