Link to home
Start Free TrialLog in
Avatar of carla1
carla1

asked on

How do I read a file's Properties

What I need to do is look at all of the files (Excell, PowerPoint...) in a certain location, compare their properties (name, size, date created) with the files I already have, and, if I don't have that file already, copy the file to a new location.

I need to know how to read the files properties, and also how to copy the file to another location.
Avatar of carla1
carla1

ASKER

Edited text of question
You can use API's.
You can retrieve the Files properties using "GetFileAttributes" function.
If the function succeeds, the return value contains the attributes of the specified file or directory.
If the function fails, the return value is 0xFFFFFFFF. To get extended error information, call GetLastError.
The attributes can be one or more of the following values:
FILE_ATTRIBUTE_ARCHIVE      The file or directory is an archive file or directory. Applications use this flag to mark files for backup or removal.
FILE_ATTRIBUTE_COMPRESSED      The file or directory is compressed. For a file, this means that all of the data in the file is compressed. For a directory, this means that compression is the default for newly created files and subdirectories.
FILE_ATTRIBUTE_DIRECTORY      The "file or directory" is a directory.
FILE_ATTRIBUTE_HIDDEN      The file or directory is hidden. It is not included in an ordinary directory listing.
FILE_ATTRIBUTE_NORMAL      The file or directory has no other attributes set. This attribute is valid only if used alone.
FILE_ATTRIBUTE_OFFLINE      The data of the file is not immediately available. Indicates that the file data has been physically moved to offline storage.
FILE_ATTRIBUTE_READONLY      The file or directory is read-only. Applications can read the file but cannot write to it or delete it. In the case of a directory, applications cannot delete it.
FILE_ATTRIBUTE_SYSTEM      The file or directory is part of, or is used exclusively by, the operating system.
FILE_ATTRIBUTE_TEMPORARY      The file is being used for temporary storage. File systems attempt to keep all of the data in memory for quicker access rather than flushing the data back to mass storage. A temporary file should be deleted by the application as soon as it is no longer needed.

You can retrieve the file Date using "GetFileTime", the size with "GetFileSize", an you can retrieve a lot of information on a file using "GetFileInformationByHandle" which needs a "BY_HANDLE_FILE_INFORMATION" structure... Although these functions need a handle to an open file, which you can obtain with "CreateFile"...

All the declarations needed can be found in your API text viewer.
If you have any questions, just tell me.
APIs would work, but you really want to do something more like this.

To copy a file, use the FileCopy command like this

FileCopy SourcePath, DestinationPath

Here is how you would go through a directory and get the file attrbiutes you want:

Dim Attr1 As String
Dim Attr2 As Long
Dim MyPath, MyName

MyPath = "c:\*.*"  ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.

Do While MyName <> ""   ' Start the loop.
    'Ignore the current directory and the encompassing directory.
    If MyName <> "." And MyName <> ".." Then
        Attr1 = FileDateTime("c:\" & MyName)
        Attr2 = FileLen("c:\" & MyName)
        MsgBox "c:\" & MyName & vbCrLf & Attr1 & vbCrLf & Attr2
    End If
    MyName = Dir    ' Get next entry.
Loop


Avatar of carla1

ASKER

Your answer was helpful, but jmnolan's answer was what I was looking for.

jmnolan: Your code worked great.  If you want the points, please post your answer for me to accept.
Thanks
ASKER CERTIFIED SOLUTION
Avatar of jmnolan
jmnolan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial