Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Changing File Attribute

0.00/5 (No votes)
22 Feb 2010 1  
Teaches you how to accept files and folders and change their attributes
File_Attribute

Introduction

This is a simple VB6 application that can change file or folder attributes and teach you how to accept dropped files, and files from command line. The purpose of this article is for beginners to know how to create WndProc function and translate the message WM_DROPFILES.

Accept Files

First you have to post a flag to your window that it is accept files, the easy way to use DragAcceptFiles(hWnd,fAccept). Then your window proc will reserve a message WM_DROPFILES that contains information about number of dropped files and the full path for every file.

Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long,
    ByVal lParam As Long) As Long
On Error Resume Next
        If uMsg = WM_DROPFILES Then
            On Error Resume Next
            Dim All_Number_Of_Drag_Files As Integer
            Dim Drag_File_Path           As String
            Dim Drag_File_Size           As String
            Dim Numbers                  As Long
            Dim Res                      As String
            All_Number_Of_Drag_Files = DragQueryFile(wParam, -1, vbNullString, 0)
            
            If All_Number_Of_Drag_Files = 1 Then
                Drag_File_Size = DragQueryFile(wParam, 0, vbNullString, 0)
                Drag_File_Path = Space$(255)
                DragQueryFile wParam, 0, Drag_File_Path, (Drag_File_Size + 1)
                Form1.Text1.Text = Drag_File_Path
            Else
                Form2.Show
                Form2.List1.Clear
                For Numbers = 0 To (All_Number_Of_Drag_Files - 1)
                    Drag_File_Size = DragQueryFile(wParam, Numbers, vbNullString, 0)
                    Drag_File_Path = Space$(255)
                    DragQueryFile wParam, Numbers, Drag_File_Path, (Drag_File_Size + 1)
                    If Do_File(True, Drag_File_Path) = True Then Res = 
                        "True : " Else: Res = "False: "
                    Form2.List1.AddItem (Res & Drag_File_Path)
                Next
            End If
        End If
    WindowProc = CallWindowProc(PrevProc, hWnd, uMsg, wParam, lParam)
End Function

Changing File Attributes

The file attributes is:

  • ReadOnly: This flag means that applications can't edit or delete the file or folder.
  • Hidden: This flag means that the file is invisible to you and windows explorer will discard it.
  • Archive: This flag means that the file is archived in the hard disk.
  • SystemFile: This flag means that the file is a system file and it's important.

I've used File System Object here to edit the file attributes, you can use API's instead like this:

Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
Public Const FILE_ATTRIBUTE_HIDDEN = &H2
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Public Const FILE_ATTRIBUTE_READONLY = &H1
Public Const FILE_ATTRIBUTE_SYSTEM = &H4
Public Const FILE_ATTRIBUTE_TEMPORARY = &H100
Public Const FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = &H2000
Public Const FILE_ATTRIBUTE_OFFLINE = &H1000
Public Declare Function SetFileAttributes Lib "kernel32" Alias _
"SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
Public Function SetAttr(ByVal lpFile As String, ByVal Flags As Long) As Boolean
    SetAttr = SetFileAttributes(lpFile, Flags)
End Function 

You can learn more about file attributes by reading this.

Finally, I would like to thank you for reading my article.

History

  • 22nd February, 2010: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here