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)
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
Restored from previous forum. Originally posted by Rings.
Thanx Fangbeast for the hint.I use Set/GetAttribute onto my forthcoming project;
a SelfXTract and VLinker .
How to extract one bit ?
Normaly i use the AND-operator with 2^Bitnumber to check if a Bit is set.
But 'AND' crashes the compiler.How to make ?
Any hints ?
Restored from previous forum. Originally posted by fred.
Thanx Fangbeast for the hint.I use Set/GetAttribute onto my forthcoming project;
a SelfXTract and VLinker .
How to extract one bit ?
Normaly i use the AND-operator with 2^Bitnumber to check if a Bit is set.
But 'AND' crashes the compiler.How to make ?
Any hints ?
The 'AND' operand is for logic only operation. For numeric, just use '&' like in C:
; Check if the 4th bit is set:
IsThisBitSet = YourVar & %1000
Procedure.l setfileattribute_jc(wfile$,attribs,type,value)
; ------ IN
; attribs if already known, obtained before from GetFileAttributes_
; or from DirectoryEntryAttributes()
; otherwise zero.
; type must be:
; #PB_FileSystem_Hidden : File is hidden
; #PB_FileSystem_Archive : File has been changed since the last time
; #PB_FileSystem_ReadOnly: File is in readonly mode
; #PB_FileSystem_System : File is a system file
; value must be:
; 0 = remove attribute
; 1 = set attribute on
; ------ OUT
; result = 0 = error
; 1 = attribute changed
; 2 = attribute not changed. Was already with good value.
result = 2
If attribs = 0
attribs = GetFileAttributes_(wfile$)
If attribs = $FFFFFFFF
result = 0
ProcedureReturn result
EndIf
EndIf
w = attribs & type
If w <> 0
w = 1
EndIf
If w <> value
result = SetFileAttributes_(wfile$,attribs ! type)
EndIf
ProcedureReturn result
EndProcedure
;wfile$ = "D:\CoolPix\00\0007\000716 Dijon\dscn0180.jpg"
;type = #PB_FileSystem_Archive
;If setfileattribute_jc(wfile$,0,type,0) = 0
; MessageRequester("Error attribute_set_archive",wfile$)
;EndIf
;End
PureBasic 6.20 beta 2 (x64) | Windows 10 Pro x64 | Intel(R) Core(TM) i7-8700 CPU @ 3.20Ghz 16 GB RAM, SSD 500 GB, PC locally assembled.
Come back to 6.11 LTS 64 bits because of an issue with #PB_ComboBox_UpperCase in ComboBoxGadget() (Oct. 10, 2024).