Check if a result is a file or directory

Hi,

I have a loop that populates a listview control with directory and file names.  If the loop is on a file then I also want to display the file size and the file last modified date.

The code I am using is:

LocalDirectory = Directory.GetFileSystemEntries(CurrentDirectory)
       
Length = LocalDirectory.Length

For Count = 0 To Length - 1
 
  listviewLocalFile.Items.Add(LocalDirectory(Count))

  If (FILE) Then

    listviewLocalFile.Items(Row).SubItems.Add(FileLen(LocalFile(Count)))

    listviewLocalFile.Items(Row).SubItems.Add(FileDateTime(LocalFile(Count)))

  End If
Next Count


I can't figure out what to put in the If (FILE) Then line.

Thanks!
Ben Cunningham
bcunningAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DabasCommented:
Hi bcunning,
> Directory.GetFileSystemEntries(CurrentDirectory)
I suggest that instead of using GetFileSystemEntries, you use
    GetDirectories and GetFiles separately.
You will still list all of them together in your listview, and be able to handle each one individually.

Dabas
0
bcunningAuthor Commented:
Actually, I originally used GetDirectories and GetFiles separately, but it seemed like it would be more inefficient if I could do it in 1 loop instead of 2.

Thanks!
Ben Cunningham
0
armoghanCommented:
One way is to use something like this

Dim f As New FileInfo(LocalDirectory(Count))

If (f.Attributes.Directory) Then
        Console.WriteLine("Is a dir")
End If
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

GlomCommented:
Hi,

>> If (FILE) Then

You can replace your line by the following to do this :

If New IO.FileInfo(LocalDirectory(Count)).Attributes = IO.FileAttributes.Directory Then

Glom
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
armoghanCommented:
but easiest way is :)

If (Directory.Exists(LocalDirectory(Count))) Then
                Console.WriteLine("Is a dir")
else
                Console.WriteLine("Is a file")
0
armoghanCommented:
or

If (File.Exists(LocalDirectory(Count))) Then
                Console.WriteLine("Is a File")
Else
                Console.WriteLine("Is a dir ")

End If
0
iboutchkineCommented:
Private Sub TestFileOrDirectory(byVal sFileOrDir as String)
        Dim oFileInfo As FileInfo = New FileInfo(sFileOrDir)
        If Not oFileInfo Is Nothing Then
            If (oFileInfo.Attributes And FileAttributes.Directory) = 0 Then
                MsgBox("Its a file!")
            Else
                MsgBox("Its a directory!")
            End If
        End If
    End Sub
0
bcunningAuthor Commented:
Thanks all!

- Ben
0
armoghanCommented:
bcunning,
I wrote the same code before Glom, why didnt you accept that ?
0
bcunningAuthor Commented:
Not sure, either Glom's post made more sense to me at the time or I missed your's.

Sorry,
Ben
0
armoghanCommented:
ok, NP
What is done is done :)
0
GlomCommented:
Hi armoghan,

In fact, your first posted code sample seems wrong :

If (f.Attributes.Directory) Then
        Console.WriteLine("Is a dir")
End If

Should be :

If (f.Attributes = IO.FileAttributes.Directory ) Then
        Console.WriteLine("Is a dir")
End If

That may be why...
0
armoghanCommented:
thanx Glom for the correction..
Yes I checked it , actually  overlooked it :)
the following line will always return true
if (f.Attributes.Directory)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.