Home All Groups Group Topic Archive Search About

newbie: foundfile attributes? (filesize, saved date, etc.)

Author
25 Nov 2006 2:38 AM
Keith R
I'm a newbie using vb.net express 2005.

I've been working on the following code snippet, trying to figure out how to
return characteristics of the found files (I believe the code itself is just
returning the string path/filename). Is there a way to work backward to get
the file information? I want to populate it into a database (after filtering
it) and am hoping to use a combination of the filesize and saved date as the
unique key (these are AVIs that have been downloaded from a digital camera,
so they should all have non-identical saved dates, and I'd add the filesize
just in case as a backup). I'm using the listbox just for testing, then I'll
figure out how to add the contents to my database (while avoiding adding
clips that already exist)

Dim ApexPath As String

Dim folderDialog As FolderBrowserDialog

folderDialog = New FolderBrowserDialog()

folderDialog.ShowDialog()

ApexPath = folderDialog.SelectedPath

For Each foundFile As String In My.Computer.FileSystem.GetFiles (ApexPath ,
True, "*.avi")

KeyPart1 = foundFile.Length   'isn't an actual property of foundfile!

ListBox1.Items.Add(foundFile)

Next


Thank you,
Keith

Author
25 Nov 2006 5:01 AM
RobinS
You can get attributes such as read-only, hidden, system
file, etc., from either the File class or the FileInfo class.

You can get the CreationDate, LastAccessTime, LastWriteTime,
and stuff like that from both the File class and the FileInfo
class.

As far as I can tell, you can only get the file size
from the FileInfo class.

I have System.IO as an imports at the top of my class.

Here's some code:

    For Each FileFound As String In Directory.GetFiles("E:\")

        '***FILE CLASS***
        'Check attributes on the file using the File class.
        Dim attr_fc As FileAttributes = File.GetAttributes(FileFound)
        If CBool(attr_fc And FileAttributes.ReadOnly) Then
            Console.WriteLine(FileFound & " is read-only using File class.")
        End If
        Dim dt_fc As Date = File.GetCreationTime(FileFound)

        '***FILEINFO CLASS***
        Dim fi As FileInfo = New FileInfo(FileFound)
        Dim dt_fi As Date = fi.CreationTime
        Dim sz_fi As Long = fi.Length
        Console.WriteLine(String.Format("FileClass CreationTime = {0}, " & _
          " FileInfo CreationTime = {1}, " & _
          " FileInfo Size = {2}", dt_fc.ToString, dt_fi.ToString,
sz_fi.ToString))
        'Check attributes on the file using the FileInfo class.
        Dim attr_fi As FileAttributes = fi.Attributes
        If CBool(attr_fi And FileAttributes.ReadOnly) Then
            Console.WriteLine(FileFound & " is read-only using FileInfo
class.")
        End If
    Next

By the way, these can be useful, too.

    'return path for file
    Path.GetDirectoryName(filename)

    'return name w/o any directory or path info
    Path.GetFileName(filename)

    'return only the file extension
    Path.GetFileExtension(filename)

    'return name w/o path and w/o extension
    Path.GetFileNameWithoutExtension(filename)

I learned this from Francesco Balena's book
"VB2005: The Language".

Robin S.
-----------------------------------

Show quoteHide quote
"Keith R" <ASpamfilterAddress@NoMail.org> wrote in message
news:Of1AMrDEHHA.3600@TK2MSFTNGP06.phx.gbl...
> I'm a newbie using vb.net express 2005.
>
> I've been working on the following code snippet, trying to figure out how
> to return characteristics of the found files (I believe the code itself is
> just returning the string path/filename). Is there a way to work backward
> to get the file information? I want to populate it into a database (after
> filtering it) and am hoping to use a combination of the filesize and saved
> date as the unique key (these are AVIs that have been downloaded from a
> digital camera, so they should all have non-identical saved dates, and I'd
> add the filesize just in case as a backup). I'm using the listbox just for
> testing, then I'll figure out how to add the contents to my database
> (while avoiding adding clips that already exist)
>
> Dim ApexPath As String
>
> Dim folderDialog As FolderBrowserDialog
>
> folderDialog = New FolderBrowserDialog()
>
> folderDialog.ShowDialog()
>
> ApexPath = folderDialog.SelectedPath
>
> For Each foundFile As String In My.Computer.FileSystem.GetFiles (ApexPath
> , True, "*.avi")
>
> KeyPart1 = foundFile.Length   'isn't an actual property of foundfile!
>
> ListBox1.Items.Add(foundFile)
>
> Next
>
>
> Thank you,
> Keith
>
Author
25 Nov 2006 12:59 PM
Keith R
Thank you Robin! This looks much simpler than what I was trying. I'll check
with our local library to see if the have (or can order) the book you
referenced.
Show quoteHide quote
:)
Keith

"RobinS" <RobinS@NoSpam.yah.none> wrote in message
news:MsidnUYaR_aAU_rYnZ2dnUVZ_qCdnZ2d@comcast.com...
> You can get attributes such as read-only, hidden, system
> file, etc., from either the File class or the FileInfo class.
>
> You can get the CreationDate, LastAccessTime, LastWriteTime,
> and stuff like that from both the File class and the FileInfo
> class.
>
> As far as I can tell, you can only get the file size
> from the FileInfo class.
>
> I have System.IO as an imports at the top of my class.
>
> Here's some code:
>
>    For Each FileFound As String In Directory.GetFiles("E:\")
>
>        '***FILE CLASS***
>        'Check attributes on the file using the File class.
>        Dim attr_fc As FileAttributes = File.GetAttributes(FileFound)
>        If CBool(attr_fc And FileAttributes.ReadOnly) Then
>            Console.WriteLine(FileFound & " is read-only using File
> class.")
>        End If
>        Dim dt_fc As Date = File.GetCreationTime(FileFound)
>
>        '***FILEINFO CLASS***
>        Dim fi As FileInfo = New FileInfo(FileFound)
>        Dim dt_fi As Date = fi.CreationTime
>        Dim sz_fi As Long = fi.Length
>        Console.WriteLine(String.Format("FileClass CreationTime = {0}, " &
> _
>          " FileInfo CreationTime = {1}, " & _
>          " FileInfo Size = {2}", dt_fc.ToString, dt_fi.ToString,
> sz_fi.ToString))
>        'Check attributes on the file using the FileInfo class.
>        Dim attr_fi As FileAttributes = fi.Attributes
>        If CBool(attr_fi And FileAttributes.ReadOnly) Then
>            Console.WriteLine(FileFound & " is read-only using FileInfo
> class.")
>        End If
>    Next
>
> By the way, these can be useful, too.
>
>    'return path for file
>    Path.GetDirectoryName(filename)
>
>    'return name w/o any directory or path info
>    Path.GetFileName(filename)
>
>    'return only the file extension
>    Path.GetFileExtension(filename)
>
>    'return name w/o path and w/o extension
>    Path.GetFileNameWithoutExtension(filename)
>
> I learned this from Francesco Balena's book
> "VB2005: The Language".
>
> Robin S.
> -----------------------------------
>
> "Keith R" <ASpamfilterAddress@NoMail.org> wrote in message
> news:Of1AMrDEHHA.3600@TK2MSFTNGP06.phx.gbl...
>> I'm a newbie using vb.net express 2005.
>>
>> I've been working on the following code snippet, trying to figure out how
>> to return characteristics of the found files (I believe the code itself
>> is just returning the string path/filename). Is there a way to work
>> backward to get the file information? I want to populate it into a
>> database (after filtering it) and am hoping to use a combination of the
>> filesize and saved date as the unique key (these are AVIs that have been
>> downloaded from a digital camera, so they should all have non-identical
>> saved dates, and I'd add the filesize just in case as a backup). I'm
>> using the listbox just for testing, then I'll figure out how to add the
>> contents to my database (while avoiding adding clips that already exist)
>>
>> Dim ApexPath As String
>>
>> Dim folderDialog As FolderBrowserDialog
>>
>> folderDialog = New FolderBrowserDialog()
>>
>> folderDialog.ShowDialog()
>>
>> ApexPath = folderDialog.SelectedPath
>>
>> For Each foundFile As String In My.Computer.FileSystem.GetFiles (ApexPath
>> , True, "*.avi")
>>
>> KeyPart1 = foundFile.Length   'isn't an actual property of foundfile!
>>
>> ListBox1.Items.Add(foundFile)
>>
>> Next
>>
>>
>> Thank you,
>> Keith
>>
>
>