Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Check if a result is a file or directory

Posted on 2004-11-09
13
Medium Priority
?
208 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:bcunning
  • 6
  • 3
  • 2
  • +2
13 Comments
 
LVL 27

Expert Comment

by:Dabas
ID: 12541206
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
 

Author Comment

by:bcunning
ID: 12541422
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
 
LVL 18

Expert Comment

by:armoghan
ID: 12541903
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Accepted Solution

by:
Glom earned 500 total points
ID: 12541907
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
 
LVL 18

Expert Comment

by:armoghan
ID: 12541912
but easiest way is :)

If (Directory.Exists(LocalDirectory(Count))) Then
                Console.WriteLine("Is a dir")
else
                Console.WriteLine("Is a file")
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12541924
or

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

End If
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12543711
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
 

Author Comment

by:bcunning
ID: 12547078
Thanks all!

- Ben
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12551733
bcunning,
I wrote the same code before Glom, why didnt you accept that ?
0
 

Author Comment

by:bcunning
ID: 12552332
Not sure, either Glom's post made more sense to me at the time or I missed your's.

Sorry,
Ben
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12552463
ok, NP
What is done is done :)
0
 
LVL 4

Expert Comment

by:Glom
ID: 12581764
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
 
LVL 18

Expert Comment

by:armoghan
ID: 12612187
thanx Glom for the correction..
Yes I checked it , actually  overlooked it :)
the following line will always return true
if (f.Attributes.Directory)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question