Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 784
  • Last Modified:

exclude hidden files

I have this code in a loop.. but id like to exclude the hidden .doc files. Can someone imprve it?
 'add the doc files
                                For Each CurrentFileInfo As FileInfo In DirInfo.GetFiles()


                                    If CurrentFileInfo.Extension = ".doc" Then
                                        zip.AddFile(CurrentFileInfo.FullName)
                                    End If

                                Next


this code includes the hidden files with start of "~"
0
zachvaldez
Asked:
zachvaldez
  • 2
  • 2
  • 2
1 Solution
 
paulop1975Commented:
On the FileInfo class there's the Attributes property. Through it, yo may know if a certain file is hidden or not, among other things.
Hope it helps.
Cheers,
eLN
0
 
paulop1975Commented:
Hence, you would have somethong like this:
 
For Each CurrentFileInfo As FileInfo In DirInfo.GetFiles()
    If CurrentFileInfo.Extension = ".doc" And CurrentFileInfo.Attributes <> FileAttributes.Hidden Then
        zip.AddFile(CurrentFileInfo.FullName)
    End If
Next

Open in new window

0
 
Todd GerbertIT ConsultantCommented:
You need to change line 2 in above example to:

If CurrentFileInfo.Extension = ".doc" And Not CurrentFileInfo.Attributes.HasAttribute(FileAttributes.Hidden) Then


0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
zachvaldezAuthor Commented:
to use hasattribute a namespace should be added. which one?
0
 
Todd GerbertIT ConsultantCommented:
Sorry, it's HasFlag() not HasAttribute, and is only in .Net 4.  If you're at an earlier version of .Net try this:

If CurrentFileInfo.Extension = ".doc" And Not HasFileAttribute(CurrentFileInfo.Attributes, FileAttributes.Hidden) Then

Open in new window

Private Function HasFileAttribute(ByVal fileAttributes As System.IO.FileAttributes, ByVal attribute As System.IO.FileAttributes) As Boolean
    Return (fileAttributes And attribute) > 0
End Function

Open in new window

0
 
zachvaldezAuthor Commented:
perfect!. I already got the system.io namespace. this work now
0
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.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now