Page 1 of 1 1
Topic Options
#195756 - 2009-09-04 03:47 AM File Permissions
Dr_Rick Offline
Fresh Scripter

Registered: 2009-07-05
Posts: 38
Loc: Guam
Is there any way to get a users permission to a file before attempting to save it? Some of the ASCII files in my script are read only for some users. When they attepmt to save they produce undesired results and the script terminates.
_________________________
Dr Rick
Computer Specialist

Top
#195757 - 2009-09-04 03:50 AM Re: File Permissions [Re: Dr_Rick]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
Checkout GetFileAttr() in the manual.
Top
#195758 - 2009-09-04 03:56 AM Re: File Permissions [Re: Dr_Rick]
Dr_Rick Offline
Fresh Scripter

Registered: 2009-07-05
Posts: 38
Loc: Guam
Unfortuneatly GetFileAttr() does not give me the permission that a user has to write the file. Even if the file has an attribute of read only, a user with full control of the file can still change it or overwrite it. I need to enumerate the read and write permissions of the file. I hear that setACL returns these values but I am not interested in performing a shell command call to this external application.

Edited by Dr_Rick (2009-09-04 04:21 AM)
_________________________
Dr Rick
Computer Specialist

Top
#195759 - 2009-09-04 05:37 AM Re: File Permissions [Re: Dr_Rick]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
I went on the fact you said "read only"...

Okay... NTFS Permissions then. Arend wrote a UDF but its XP and above only.

NTFSPerms() - http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=180734#Post180734

Top
#195761 - 2009-09-04 11:57 AM Re: File Permissions [Re: Allen]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Probably no need to make it too complicated. Just open the file in write mode and check for an error. You can wrap it up in a simple UDF.

Try this, the UDF IsWriteable() returns 1 if the file exists and can be opened in write mode otherwise it returns 0:
 Code:
Break ON
$=SetOption("Explicit","ON")

Dim $sFile

For Each $sFile in Split("writeable.txt notwriteable.txt")
	$sFile+" is "+IIf(IsWriteable($sFile),"","not ")+"writeable."+@CRLF
	"Error code: ["+@ERROR+"] "+@SERROR+@CRLF
Next


Function IsWriteable($sFile)
	Dim $fh

	$IsWriteable=0
	$fh=FreeFileHandle()
	If @ERROR Exit @ERROR EndIf
	
	If Open($fh,$sFile,0+4) Exit @ERROR EndIf
	$IsWriteable=Close($sFile)
	$IsWriteable=1

	Exit 0
EndFunction


When the file notwritable.txt has write permissions removed I get:
 Quote:
writeable.txt is writeable.
Error code: [0] The operation completed successfully.
notwriteable.txt is not writeable.
Error code: [5] Access is denied.

Top
#195773 - 2009-09-05 04:06 PM Re: File Permissions [Re: Richard H.]
Dr_Rick Offline
Fresh Scripter

Registered: 2009-07-05
Posts: 38
Loc: Guam
This will provide enough information to the script to allow me to make decisions based on the results and setup the user environment to disallow save if permissions do not exist.
_________________________
Dr Rick
Computer Specialist

Top
#195779 - 2009-09-07 12:06 PM Re: File Permissions [Re: Dr_Rick]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
If the file does not already exist and you want the user to be able to create it then change the UDF:
 Code:
Function IsWriteable($sFile,Optional $bCreate)
	Dim $fh

	$IsWriteable=0
	$bCreate=Not Not $bCreate

	$fh=FreeFileHandle()
	If Not $fh Exit 6 EndIf ; Invalid handle error
	
	If Open($fh,$sFile,$bCreate+4) Exit @ERROR EndIf
	$IsWriteable=Close($fh)
	$IsWriteable=1

	Exit 0
EndFunction


Now, when you do the test pass a true value as the second optional parameter. This will create an empty file if it doesn't already exist and the user is allowed to write.

If the optional pararamer is missing or false then the UDF will return false if the file is missing.

IMPORTANT: This update fixes a bug in the previous UDF, caused because FreeFileHandel() does not (re)set @ERROR.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 466 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.066 seconds in which 0.025 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org