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

K

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
 
R

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.
-----------------------------------
 
K

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.
:)
Keith
 

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