Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Changing file attributes
Message
 
À
01/02/2001 17:40:02
Christian Cote
Les Logiciels Onoma Inc.
Longueuil, Québec, Canada
Information générale
Forum:
Visual Basic
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00471513
Message ID:
00471544
Vues:
19
>HI,
>I would like to know how I can change a file attribute to a file copied with File System Object or another way. The file I copied (from a CD ROM) is read only and archive (RA) and I would like to delete it after the using it. But when I call the File Object System method (DeleteFile), VB tells me : "Permission Denied".
>
>Any way to remove the read only from the copied file with FSO or Win32API.


I have used this code:
Public Function ChangeFileAttributes(ByVal pstrFile As String, _
                                     ByVal pstrAttributes As String, _
                                     ByVal pblnOn As Boolean _
                                    ) As Boolean
Dim fso As FileSystemObject
Dim intAttributes As Integer
Dim intI As Integer
Dim objFile As File

    For intI = 1 To Len(pstrAttributes)
        Select Case Mid$(pstrAttributes, intI, 1)
            Case "N"    'Normal file. No attributes are set.
                intAttributes = intAttributes + 0
            Case "R"    'Read-only file. Attribute is read/write.
                intAttributes = intAttributes + 1
            Case "H"    'Hidden file. Attribute is read/write.
                intAttributes = intAttributes + 2
            Case "S"    'System file. Attribute is read/write.
                intAttributes = intAttributes + 4
            Case "A"    'Archive. File has changed since last backup. Attribute is read/write.
                intAttributes = intAttributes + 32
        End Select
        'Other attributes (Read-only)
        'Volume 8 Disk drive volume label. Attribute is read-only.
        'Directory 16 Folder or directory. Attribute is read-only.
        'Alias 64 Link or shortcut. Attribute is read-only.
        'Compressed 128 Compressed file. Attribute is read-only.
    Next intI
    
    'Renverse les attributs s'il faut les mettre à OFF
    If Not pblnOn Then intAttributes = intAttributes * -1
    
    Set fso = New FileSystemObject
    If fso.FileExists(pstrFile) Then
        Set objFile = fso.GetFile(pstrFile)
        objFile.Attributes = objFile.Attributes + intAttributes
    End If
End Function
This is how to use it:
    'Set attributes to Read-Only
    Call ChangeFileAttributes(pstrFileName, "R", True)
Éric Moreau, MCPD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc.
http://www.emoreau.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform