Using FileDialog, Directory, and Copy - How can I save a file (like a Word document) as read-only?

I have an app where the user can add an attachment using FileDialog, Directory, and Copy. But, I need to make the document they are saving (regardless of extension) a read-only document when it is saved to a new location. Is this possible?

I’m only a year into using Clarion 9 - so please be kind/programmer friendly with the examples/explanations.

Thanks in advance.

Hi,

This is untested, but you could do this with the Win32 API SetFileAttributesA

Inside Global Map

	MODULE('win32.lib')
SetFileReadOnly(*CSTRING lpFileName, LONG dwFileAttributes),SIGNED,RAW,PASCAL,NAME('SetFileAttributesA')
	END

Then create a procedure as follows

SetFileAttributesReadOnly  PROCEDURE(STRING fn)
READONLY_FILE_ATTRIBUTE         EQUATE(1)
ARCHIVE_FILE_ATTRIBUTE          EQUATE(32)
FileName	    CSTRING(260)
Attributes    	LONG
ReturnValue 	SIGNED

	CODE
	ATTR = READONLY_FILE_ATTRIBUTE + ARCHIVE_FILE_ATTRIBUTE
	FileName = CLIP(SHORTPATH(fn))
	ReturnValue = SetFileReadOnly(FileName, Attributes)
	RETURN(ReturnValue)

As I said I haven’t tested this, just typed it here so please let us know either way. I’ll remove if it fails.

Regards

Mark

I know this works. You want to Get, modify then Set to preserve other existing attributes. Just reverse the Strip logic to Add with BOR(Attrs,FILE_ATTRIBUTE_READONLY)

    SetFileAttributes(*CSTRING,LONG),BOOL,PASCAL,DLL(1),RAW,NAME('SetFileAttributesA'),PROC
    GetFileAttributes(*CSTRING),LONG,PASCAL,DLL(1),RAW,NAME('GetFileAttributesA')
 

FileStripReadOnly    FUNCTION (string _filename)!,BOOL     ! Declare Procedure
Attrs                           LONG,AUTO
CFN                             CSTRING(261),AUTO
RetBool                         BOOL,AUTO
FILE_ATTRIBUTE_READONLY         EQUATE( 00000001h)
INVALID_FILE_ATTRIBUTES         EQUATE(0FFFFFFFFh)  !(DWORD (-1))
  CODE  
    RetBool = 0
    CFN = CLIP(_filename)
    Attrs = GetFileAttributes(CFN)                !If the function fails, the return value is INVALID_FILE_ATTRIBUTES.
    IF Attrs <> INVALID_FILE_ATTRIBUTES
       IF BAND(Attrs, FILE_ATTRIBUTE_READONLY)
          Attrs -= FILE_ATTRIBUTE_READONLY
          IF SetFileAttributes(CFN,Attrs)         !If the function succeeds, the return value is nonzero.
             RetBool = True
          END
       END
    END

    RETURN RetBool

What they said. You need to save the file as usual and then use some method to change the file attribute to read only.
There are many 3rd part utils for this in addition to that above