Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to tag a filename as readonly programatically
Message
De
03/03/2003 08:39:25
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00760275
Message ID:
00760323
Vues:
12
>Hi to all!
>
>Anyone who knows how to tag a filename as readonly programatically

The other answers you got are good ones, but if you're like me and try to stay away from the scripting host because you don't know if it's going to be available, you can use the Windows API.

I do it like this (for all file attributes): in the calling program (or whereever makes sense for you), set up your defines.
* File attributes
#DEFINE FILE_ATTRIBUTE_ARCHIVE 		32
#DEFINE FILE_ATTRIBUTE_COMPRESSED 	2048
#DEFINE FILE_ATTRIBUTE_DIRECTORY 	16
#DEFINE FILE_ATTRIBUTE_HIDDEN 		2
#DEFINE FILE_ATTRIBUTE_NORMAL 		128
#DEFINE FILE_ATTRIBUTE_READONLY 	1
#DEFINE FILE_ATTRIBUTE_SYSTEM 		4
#DEFINE FILE_ATTRIBUTE_TEMPORARY 	256
Then I have a function called 'FileAttributes()' as follows:
Function FileAttributes
LParameters tcFile, tnAttribute, tlSet

* declares for file attribute functions
Declare Integer GetFileAttributes in kernel32 string filename
Declare Integer SetFileAttributes in kernel32 string filename, integer fileattributes

Local llOk, lnCurrent

llOk = .F.

If PCount() = 3
   llOk = .T.
Endif

IF llOk AND File(tcFile)
   lnCurrent = GetFileAttributes(tcFile)
   If !(lnCurrent = -1 or lnCurrent > 2487) && -1 is error condition, 2487 is max
 					    && otherwise we have a problem
      * check for setting of requested attribute
      IF BitAnd(lnCurrent, tnAttribute) = tnAttribute	&& the attribute is already set
         If !tlSet   && wants attribute unset, do it, otherwise, do nothing
            llOk = (SetFileAttributes(tcFile, lnCurrent - tnAttribute) = 1) && 1 means ok
         else
            llOk = .T.
         Endif
      Else   && not currently set
         If tlSet   && want it set, set it, otherwise do nothing
            llOk = (SetFileAttributes(tcFile, lnCurrent + tnAttribute) = 1)
         Else
            llOk = .T.
         Endif
      endif
   Endif
Endif

Return llOk
Then if you want to set the read-only attribute, call it like so:
FileAttributes(<filename>, FILE_ATTRIBUTE_READONLY, .T.)
To be perfectly honest, I cannot recall now, whether I wrote this function, or stole it from someone else.

Alan
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform