File Attributes, Testing for and setting API

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

File Attributes, Testing for and setting API

Post by BackupUser »

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.

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
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

Nice source! We are learning together. :)

Now please find out how to set the attributes. could not be hard as well. :=)

Tranquilizer/ Secretly!
Registred PureBasic User
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

UUpppsss, sorry Fangbeast, I did not see the SetFileAttribute_() Command. :)
(It was late and I was drunken hehe)

Someone who can read is in advance. :)

Mike

Tranquilizer/ Secretly!
Registred PureBasic User
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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 ?



Siggi
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

thanx fred, i'm wrong coz i'm coming from Quick and VisualBasic,neither from C.

Thats it .

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


Siggi
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

Note Fangbeast,

Attributes can be combined.
That means a file can be archived hidden and readonly at once.
Thats why i want only test one bits.
Fred did it.

Siggi
User avatar
CONVERT
Enthusiast
Enthusiast
Posts: 130
Joined: Fri May 02, 2003 12:19 pm
Location: France

A set attribute routine if it can help...

Post by CONVERT »

Code: Select all

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).
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4791
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

WOw, that's old

Post by Fangbeast »

ROFL, people still read my/our posts from 2001..That's good:):)
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
Post Reply