Skip to main content

FileIsReadOnly

Function FileIsReadOnly( const FileName : string) : Boolean

Example

procedure ScriptEvent(var Value: variant);
begin
Value := FileIsReadOnly('C:\Fictional\Inbound\order.xml');
end;

Usage

FileIsReadOnly reports the read-only attribute of the first file-system entry matching a path or wildcard mask.

Parameters

NameTypeDescription
FileNamestring, constLiteral path or wildcard search mask. Relative input uses the process current directory.

Returns

True only when FindFirst succeeds and the first result contains faReadOnly; otherwise False.

Behaviour

  • A literal existing read-only file returns True.
  • A literal writable file returns False.
  • Wildcards are accepted and only the first returned match is inspected.
  • Directories can match because faAnyFile includes directory entries.

Errors

Normal FindFirst errors are returned as False; unexpected runtime/allocation failures can propagate.

Usage notes

Use a literal path when testing one entry. Do not use False as proof that a file exists or is writable; make those checks separately and handle failure at the actual write.

Additional Technical Info

FileIsReadOnly searches FileName with Delphi FindFirst(..., faAnyFile) and reports whether the first matching entry has the Windows read-only attribute.

The example is source-reviewed and was not executed by the documentation workflow.

Implementation

The wrapper initialises Result := False, calls installed Delphi FindFirst with faAnyFile, tests SearchRec.Attr and faReadOnly, and closes a successful search.

Edge cases and quirks

  • Missing paths, no wildcard match, malformed input, inaccessible storage and ordinary search errors all collapse to False.
  • With a wildcard, the result does not mean every match is read-only and enumeration order should not be treated as stable.
  • The attribute is a metadata snapshot and can change immediately after return.
  • Read-only is not the same as effective write permission; ACLs, sharing, directory permissions and storage policy can still prevent writes.
  • Reparse/link behavior follows Windows directory enumeration and is not configurable here.

Side effects

No persistent change. A search handle is opened and closed on success.

Performance and concurrency

One directory search/metadata lookup. Remote storage can block. No locking is retained, so the result is inherently racy.

Related entries

  • RemoveReadOnly uses this ambiguous query before changing attributes.
  • FileLocked tests a particular read/write open mode rather than an attribute.
  • FileExists distinguishes files from directories but still returns a snapshot.

External references

Created 2026-07-15