Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Checking if writing a file is allowed

42 views
Skip to first unread message

Luc Moulinier

unread,
Jan 17, 2018, 11:20:10 AM1/17/18
to
Hello all,

First of all, my best wishes of health and happiness for this new year !

What is the best way to test if a user can save a file in a givent place ? My solution is :

...
set file [tk_getSaveFile -filetypes $types -initialfile $FichierParDefaut]
if {$file ne ""} {
if {[catch {set o [open $file w]}]} {
FaireLire "Error !\nWrite-protected !"
set file ""
} else {
# let's caller manage opening
close $o
file delete $file
}
}
return $file

Is there a better way to do so ? Checking the directory means checking ownership and permission rights ...
Shouldn't this functionallity be included in tk_getSaveFile ?

Many thanks !

Luc

Gerald Lester

unread,
Jan 17, 2018, 11:48:16 AM1/17/18
to
On 01/17/2018 10:20 AM, Luc Moulinier wrote:
> Hello all,
>
> First of all, my best wishes of health and happiness for this new year !
>
> What is the best way to test if a user can save a file in a givent place ? My solution is :
>
> ...
> set file [tk_getSaveFile -filetypes $types -initialfile $FichierParDefaut]
> if {$file ne ""} {
> if {[catch {set o [open $file w]}]} {
> FaireLire "Error !\nWrite-protected !"
> set file ""
> } else {
> # let's caller manage opening
> close $o
> file delete $file
> }
> }
> return $file
>
> Is there a better way to do so ? Checking the directory means checking ownership and permission rights ...

Read the file man/help page. Pay particular attention to the section on
the writable subcommand.

> Shouldn't this functionallity be included in tk_getSaveFile ?

Sounds good, but.. tk_getSaveFile utilizes native dialogs on some
platforms (to give the proper look and feel) that do not support such
functionality (or at least they did not and, to the best of my knowledge
still do not). Thus the answer is: No.


--
+----------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+----------------------------------------------------------------------+

Francois Vogel

unread,
Jan 17, 2018, 2:43:59 PM1/17/18
to
Le 17/01/2018 à 17:48, Gerald Lester a écrit :
> On 01/17/2018 10:20 AM, Luc Moulinier wrote:
>> What is the best way to test if a user can save a file in a givent
>> place ?
> Read the file man/help page.  Pay particular attention to the section on
> the writable subcommand.

Beware of bugs with file writable/readable:

https://core.tcl.tk/tcl/tktview?name=184358681b
https://core.tcl.tk/tcl/info/1613456fffffffff

F.

Harald Oehlmann

unread,
Jan 17, 2018, 4:11:59 PM1/17/18
to
Dear Luc,

also all the best for 2018 !

Your method is the best way to do it.

One may use "file writable", but this does not give the right answer in
a couple of circumstances. I specially have issues with linux shares on
windows.

All the best,
Harald
0 new messages