PDA

View Full Version : File attributes/status bits/protection bits



harsh
17-03-2008, 21:57
There are a handful of functions that depend on them, but I haven't found a way tinker with them directly. How does one go about getting or setting the flags (Hidden, Archive, Read-only, System) on a particular file?

I acknowledge that the solution may be in plain sight and I just haven't stumbled on it yet. I do not speak (and have little desire to fully understand) Windows SDK.

ErosOlmi
17-03-2008, 22:04
Hi harsh.

There are no native thinBasic functions to get/set file attributes.
But this is an idea for us so I will add them quite soon. Even if it is quite simple, we too do not like to go directly to Windows SDK, so we prefer to add a native function when asked by thinBasic users.

I will give you back soon.

Ciao
Eros

ErosOlmi
18-03-2008, 01:08
harsh,

please find here enclosed a new thinBasic_FILE.dll module that implements 2 new functions: FILE_GetAttr, FILE_SetAttr
To test it, place thinBasic_FILE.dll into \thinBasic\Lib\ directory replacing your current one. BE SURE TO HAVE thinBasic preview version 1.6.0.2 installed before doing that.

Here a script example on how to use new functions:


uses "FILE"

dim sFile as string value APP_SourceFullName & ".bak"
'---Create a new temp file to work with copying current script file
file_save(sFile, File_load(APP_SourceFullName))

'---Save current attribute
dim SaveOriginalAttr as long value file_getattr(sFile)

msgbox 0, "File attributes for file" & $crlf & _
sFile & $crlf & _
"is:" & $crlf & _
SaveOriginalAttr

'---Change attribute to ReadOnly
file_setattr(sFile, %FILE_READONLY)

msgbox 0, "File attributes for file" & $crlf & _
sFile & $crlf & _
"is now:" & $crlf & _
file_getattr(sFile) & $crlf(2) & _
"Attribute will be changed back to " & SaveOriginalAttr

'---Back to original attribute and delete it
file_setattr(sFile, SaveOriginalAttr)
file_kill(sFile)


'---Legend---
'%FILE_NORMAL 0
'%FILE_READONLY 1
'%FILE_HIDDEN 2
'%FILE_SYSTEM 4
'%FILE_VLABEL 8
'%FILE_SUBDIR 16
'%FILE_ARCHIVE 32
'%FILE_NORMAL 128


Functions will be present in next release.

Regards
Eros


Attached file removed. Features present in thinBasic preview version 1.6.0.3

Michael Clease
18-03-2008, 02:06
Nice work Eros but I think it would be nice to have attributes as an option on FILE_SAVE also.

It would save the file then set the desired attributes of that file thus saving the use of another keyword.

What do you think?

Petr Schreiber
18-03-2008, 11:17
Abraxas,

I was thinking about exactly the same!
I am also all for it, as long as it is optional.


Petr

ErosOlmi
18-03-2008, 15:54
Updated attached file to previous post (did I originally attached a file ??? )

FILE_Save has now the following syntax:
FILE_Save(File, Buffer [, Attr])

See following example:


uses "FILE"

dim sFile as string value APP_SourceFullName & ".bak"
'---Create a new temp file to work with copying current script file
file_save(sFile, File_load(APP_SourceFullName), %FILE_HIDDEN)

'---Save current attribute
dim SaveOriginalAttr as long value file_getattr(sFile)
dim NewAttr as long

msgbox 0, "File attributes for file" & $crlf & _
sFile & $crlf & _
"is:" & $crlf & _
SaveOriginalAttr & "->" & AttrToStr(SaveOriginalAttr)

'---Change attribute to ReadOnly
file_setattr(sFile, %FILE_READONLY)

NewAttr = file_getattr(sFile)
msgbox 0, "File attributes for file" & $crlf & _
sFile & $crlf & _
"is now:" & $crlf & _
NewAttr & _
"->" & AttrToStr(NewAttr) & $crlf(2) & _
"Attribute will be changed back to " & SaveOriginalAttr

'---Back to original attribute and delete it
file_setattr(sFile, SaveOriginalAttr)
file_kill(sFile)


function AttrToStr(lAttr as long) as string
local sOut as string

if lAttr and %FILE_NORMAL then sOut += "NORMAL "
if lAttr and %FILE_READONLY then sOut += "READONLY "
if lAttr and %FILE_HIDDEN then sOut += "HIDDEN "
if lAttr and %FILE_SYSTEM then sOut += "SYSTEM "
if lAttr and %FILE_VLABEL then sOut += "VLABEL "
if lAttr and %FILE_SUBDIR then sOut += "SUBDIR "
if lAttr and %FILE_ARCHIVE then sOut += "ARCHIVE "
if lAttr and %FILE_NORMAL then sOut += "NORMAL "

function = sOut

end function


Features will be present in next release.
Ciao
Eros

Petr Schreiber
18-03-2008, 18:25
Works good here,

is it safe to assign to "normal" file for example %FILE_SUBDIR?


Thanks,
Petr

ErosOlmi
18-03-2008, 18:49
Who knows ;) (joke)

Well, I will limit it to just use some of them.
The list contains possible cases.

Ciao
Eros

Petr Schreiber
18-03-2008, 21:20
I think it is enough to put red comment in helpfile ... only in case it is really harmful.

I am little shy to experiment with files, because on my WinME box I accidentaly wrong-renamed one file and till the end of PC I was not able to delete it out :D


Petr

harsh
21-03-2008, 06:43
please find here enclosed a new thinBasic_FILE.dll module that implements 2 new functions: FILE_GetAttr, FILE_SetAttrThat was fast! I much appreciate your quick efforts.

ErosOlmi
21-03-2008, 10:54
That was fast! I much appreciate your quick efforts.

That is our style. We try our best to develop requests as fast and better as we can.
Sometimes we are able sometimes not. But we try.

Ciao
Eros