Page 1 of 1

File Attributes, Testing for and setting API

Posted: Sun Dec 30, 2001 11:50 am
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

Posted: Sun Dec 30, 2001 9:50 pm
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

Posted: Mon Dec 31, 2001 7:08 am
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

Posted: Mon Dec 31, 2001 9:22 am
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

Posted: Mon Dec 31, 2001 11:56 am
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

Posted: Mon Dec 31, 2001 3:17 pm
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

Posted: Mon Dec 31, 2001 3:19 pm
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

A set attribute routine if it can help...

Posted: Wed May 25, 2005 11:31 am
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

WOw, that's old

Posted: Wed May 25, 2005 2:16 pm
by Fangbeast
ROFL, people still read my/our posts from 2001..That's good:):)