Dr_Rick
(Fresh Scripter)
2009-09-04 03:47 AM
File Permissions

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.

AllenAdministrator
(KiX Supporter)
2009-09-04 03:50 AM
Re: File Permissions

Checkout GetFileAttr() in the manual.

Dr_Rick
(Fresh Scripter)
2009-09-04 03:56 AM
Re: File Permissions

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.

AllenAdministrator
(KiX Supporter)
2009-09-04 05:37 AM
Re: File Permissions

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


Richard H.Administrator
(KiX Supporter)
2009-09-04 11:57 AM
Re: File Permissions

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.


Dr_Rick
(Fresh Scripter)
2009-09-05 04:06 PM
Re: File Permissions

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.

Richard H.Administrator
(KiX Supporter)
2009-09-07 12:06 PM
Re: File Permissions

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.