truncate

Sets a file to the specified number of bytes (data fork).

OS version:

(*
truncate
Sets a file to the specified number of bytes. If you want truncate a file to 20 bytes and this contains 45, the last 25 bytes will be deleted. If you want truncate a file to 20 bytes and this contains 10, truncate will add 10 nulls to the end to the file (ascii 0).
If the file does not exist, it is created.

Parameters:
f: file path, alias, posix path (the file to be written, can exist or not)
n: integer, the number of bytes to keep in the file

Example:
truncate("path:to:file.txt", 2500) 
*)

to truncate(f, n)
	set f to f as Unicode text
	if f does not contain ":" then set f to POSIX file f as Unicode text
	
	set n to n as integer
	
	try
		set fref to (open for access file f with write permission)
		set eof of fref to n
		close access fref
	on error msg number n
		try
			close access fref
		end try
		error msg number n
	end try
end truncate


property |version| : 1.0
property |from| : "jfileLib"
property author : "Pescados Software · PescadosWeb.com"
property |date| : date "sábado, 24 julio 2004 00:00:00"
property license : "freeware, open-source"