Reputation: 1705
I want to create an empty file or do nothing if a file already exists. Truncating an existing file would be fine too.
Currently I do this:
exec touch file/name.txt
This works, but depends on an external command and will only work on UNIX-y OS. Is there a way to do the same that is both concise and portable?
Upvotes: 0
Views: 3897
Reputation: 1705
Just open a file for writing and immediately close it:
Create or truncate (The result is always an empty file):
close [open file/name.txt w]
Create or do nothing (thanks, Donal Fellows):
close [open file/name.txt a]
Thanks to Shawn for this suggestion.
Upvotes: 2