VB icon

Get & set a file's attributes

Email
Submitted on: 1/2/2015 6:12:00 AM
By: Timothy Pew (from psc cd)  
Level: Not Given
User Rating: By 3 Users
Compatibility: VB 5.0, VB 6.0
Views: 905
 
     Sets a file's attributes. With this you can create archive, hidden, normal, read-only, and system files.
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Get & set a file's attributes
'**************************************
Declare Function SetFileAttributes Lib "kernel32.dll" Alias "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Get & set a file's attributes
' Description:Sets a file's attributes. With this you can create archive, hidden, normal,
read-only, and system files.
' By: Timothy Pew (from psc cd)
'
' Inputs:FullFilePath is a string containing the path and filename of a file.
FileAttributes is a long integer that contains the value to set as the file's
attributes. Use the constants listed below to set this. (SetAttributes only)
'
' Returns:SetAttributes returns true (1) if successful, otherwise it returns false (0).
GetAttributes returns the attributes of FullFilePath
'
' Assumes:The default for this function is to set the file's attributes to "archive",
which is the standard type of file.
To set more than one attribute for a file use the "or" statement.
I.E.- FILE_ATTRIBUTE_READONLY or FILE_ATTRIBUTE_HIDDEN.
Test for a certain attribute like this: If (GetAttribute And attributeconstant) <> 0 then GetAttribute = attribute defined by constant.
'
' Side Effects:The FILE_ATTRIBUTE_NORMAL (&H80) attribute use to create attribute-less files
CAN NOT be combined with any other attribute.
'**************************************

'use these constants to set the attributes you want
FILE_ATTRIBUTE_ARCHIVE = &H20
FILE_ATTRIBUTE_COMPRESSED = &H800
FILE_ATTRIBUTE_DIRECTORY = &H10
FILE_ATTRIBUTE_HIDDEN = &H2
FILE_ATTRIBUTE_NORMAL = &H80
FILE_ATTRIBUTE_READONLY = &H1
FILE_ATTRIBUTE_SYSTEM = &H4
Public Function SetAttributes(ByVal FullFilePath As String, Optional ByVal FileAttributes As Long = &H20) As Long
 'makes sure that the file path is not too long
 FullFilePath = Left(FullFilePath, 255)
 SetAttributes = SetFileAttributes(FullFilePath, FileAttributes)
End Function
Public Function GetAttributes(ByVal FullFilePath as String) as Integer
 GetAttributes = GetFileAttributes(FullFilePath)
End Function


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.