File Attributes, Testing for and setting API
Posted: Sun Dec 30, 2001 11:50 am
Code updated For 5.20+
Restored from previous forum. Originally posted by Fangbeast.
Just for fun, I tried to see if my tiny brain could begin to understand some of the terminology of the Windows API as I needed a way to get and set file attributes. Below, I have included a tiny program to show what I did and how and the comments list both the API commands and the PureBasic translation of them. I also included the attribute codes in Binary and decimal (which is what we'll use)
Hope some of this is useful to someone.
Fangles
Restored from previous forum. Originally posted by Fangbeast.
Just for fun, I tried to see if my tiny brain could begin to understand some of the terminology of the Windows API as I needed a way to get and set file attributes. Below, I have included a tiny program to show what I did and how and the comments list both the API commands and the PureBasic translation of them. I also included the attribute codes in Binary and decimal (which is what we'll use)
Hope some of this is useful to someone.
Code: Select all
MyFilename$ = "C:\WhateverYouAreTesting.exe"
If OpenConsole() ; Open a console window, easiest
AttributeResult = GetFileAttributes_(MyFilename$) ; Return the current file attributes in "AttributeResult"
PrintN(Str(AttributeResult)) ; Print it to the screen as a string
SetFileAttributes_(MyFilename$, 35) ; Change the attributes to ReadOnly + Archive + Hidden
AttributeResult = GetFileAttributes_(MyFilename$) ; Get the new attributes just to prove it
PrintN(Str(AttributeResult)) ; Print them to the screen
temp$ = Input() ; Wait for any key press to close the window
EndIf ; End the whole routine if the window could not be opened
CloseConsole() ; Close the console window
End ; End of the program (good idea for really long code)
;-----------------------------------------------------------------------------------------------------------------------------
; API GetFileAttributes LIB "KERNEL32.DLL" ALIAS "GetFileAttributesA" (lpFileName AS ASCIIZ) AS LONG
;
; PureBasic GetFileAttributes_(FileName$)
;-----------------------------------------------------------------------------------------------------------------------------
; API SetFileAttributes LIB "KERNEL32.DLL" ALIAS "SetFileAttributesA" (lpFileName AS ASCIIZ, BYVAL dwFileAttributes AS DWORD) AS LONG
;
; PureBasic SetFileAttributes_(FileName$, CombinedAttributes)
;-----------------------------------------------------------------------------------------------------------------------------
;
;
; API File attributes Specifications
;-----------------------------------------------------------------------------
; Name Binary Decimal
;-----------------------------------------------------------------------------
; %Normal = &B000000 0 (Testing this under PureBasic returned 128. not 0)
; %ReadOnly = &B000001 1
; %Hidden = &B000010 2
; %System = &B000100 4
; %vLabel = &B001000 8
; %SubDir = &B010000 16
; %Archive = &B100000 32
;-----------------------------------------------------------------------------
;
; Combine attributes to set them using the decimal values... I.e
;
; Read Only = 1
; Read Only + Hidden = 3
; Read Only + System = 5
; Read Only + Archive = 33
;
; SubDirectories can also be hidden, Volume Labels can have no other attribute
;
;
; The attribute is returned as -1 if the file specified does not exist where you tell it to
; DisableDebugger
Fangles