# ulock command

The ulock command locks or unlocks a file specified by a file
 variable.

## Format

```
ULOCK FileHandle LOCK|UNLOCK FOR n THEN statement(s) ELSE statement(s)
```

## Description

*FileHandle* must be opened for WRITE or READWRITE. The number of contiguous
 bytes from the current file pointer position to be locked or unlocked is indicated by
 *n*. If an error occurs, the ELSE path is executed. If *n* is
 -1, all bytes from the present position of the file pointer to the end of the file are locked.
 This statement does not move the file pointer. *n* has a maximum lockable
 region size of 4 MB.

 The ulock command may be used against a file opened in READ mode. The
 ulseek() function allows the file pointer to be positioned beyond the locked
 section of the file.

## Example(s)

The following statements open a file, and then lock the first 50 bytes of that file. Data is
 then written into the locked section of the file. The uwrite command advances
 the file pointer for the file (in this case, by 17, the length of the string). The
 ulseek function is used to move the file pointer back to the beginning of
 the file so that the first 50 bytes may be unlocked.

```
FILENAME="c:\books\chap2.txt"
UOPEN FILENAME FOR WRITE TO FILEDES ELSE
 PRINT "Unable to open ":FILENAME
 STOP
END
* Pointer by default, set to begining of file
ULOCK FILEDES LOCK FOR 50 ELSE
 PRINT "Unable to Lock first 50 bytes of ":FILENAME
 GOTO EOJ:
END
UWRITE "THIS IS TEST DATA" ON FILEDES THEN
 * NOTE: UWRITE will advance Pointer
END ELSE
 PRINT "Unable to write to ":FILENAME
 GOTO EOJ:
END
* Set pointer back to begining of file
IF (ULSEEK(FILEDES,0,0) = -1) THEN
 PRINT "ULSEEK failed"
 GOTO EOJ:
END
ULOCK FILEDES UNLOCK FOR 50 ELSE NULL
*
EOJ:
UCLOSE FILEDES ELSE NULL
```

## See also

- [O/S interoperability commands and functions](https://d3codex.com/pickbasic-flashbasic/os-interoperability-commands/)
- [uclose command](https://d3codex.com/pickbasic-flashbasic/uclose-command/)
- [ucreate command](https://d3codex.com/pickbasic-flashbasic/ucreate-command/)
- [udelete command](https://d3codex.com/pickbasic-flashbasic/udelete-command/)
- [uerror() function](https://d3codex.com/pickbasic-flashbasic/uerror-function/)
- [uexecute command](https://d3codex.com/pickbasic-flashbasic/uexecute-command/)
- [ulseek() function](https://d3codex.com/pickbasic-flashbasic/ulseek-function/)
- [umessage command](https://d3codex.com/pickbasic-flashbasic/umessage-command/)
- [uopen command](https://d3codex.com/pickbasic-flashbasic/uopen-command/)
- [uread command](https://d3codex.com/pickbasic-flashbasic/uread-command/)
- [ureadline() function](https://d3codex.com/pickbasic-flashbasic/ureadline-function/)
- [usystem() function](https://d3codex.com/pickbasic-flashbasic/usystem-function/)
- [uwaitfor command](https://d3codex.com/pickbasic-flashbasic/uwaitfor-command/)
- [uwrite command](https://d3codex.com/pickbasic-flashbasic/uwrite-command/)

---
Source: https://d3codex.com/pickbasic-flashbasic/ulock-command/ - part of the D3Codex reference.
