Make a backup file: Difference between revisions

(Added Wren)
Line 388:
 
AMSDOS has automatic one-level backups which also work from Locomotive BASIC: If e.g. the file <tt>test.bas</tt> is saved, the data gets written to <tt>test.$$$</tt>. When the file is closed a preexisting <tt>test.bas</tt> gets renamed to <tt>test.bak</tt> and finally <tt>test.$$$</tt> is renamed to <tt>test.bas</tt>. (These backups affect all file types, not just BASIC source code.)
 
=={{header|Nim}}==
In case the backup cannot be done, an exception is raised.
<lang Nim>import os, strutils
 
const
Suffix = ".backup"
Dir = "working_dir"
SubDir = "dir"
f1 = "f1.txt"
f2 = "f2.txt"
f3 = SubDir / "file.txt"
 
proc newBackup(path: string): string =
## Create a backup file. Return the path to this file.
if not path.fileExists():
raise newException(IOError, "file doesn't exist.")
let path = path.expandFilename() # This follows symlinks.
result = path & Suffix
moveFile(path, result)
result = result.relativePath(getCurrentDir())
 
# Prepare test files.
let oldDir = getCurrentDir()
createDir(Dir)
setCurrentDir(Dir)
createDir(SubDir)
f1.writeFile("This is version 1 of file $#" % f1)
f3.writeFile("This is version 1 of file $#" % f3)
createSymlink(f3, f2)
 
# Display initial state.
echo "Before backup:"
echo f1, ": ", f1.readFile
echo f2, " → ", f3, ": ", f2.readFile()
 
# Create backups.
echo "\nBackup of regular file:"
let f1Backup = newBackup(f1)
f1.writeFile("This is version 2 of file $#" % f1)
echo f1, ": ", f1.readFile()
echo f1Backup, ": ", f1Backup.readFile()
 
echo "\nBackup of symbolic link to file:"
let f2Backup = newBackup(f2)
f2.writeFile("This is version 2 of file $#" % f3)
echo f2, " → ", f3, ": ", f2.readFile()
echo f2Backup, ": ", f2Backup.readFile()
 
# Cleanup.
setCurrentDir(oldDir)
removeDir(Dir)</lang>
 
{{out}}
<pre>Before backup:
f1.txt: This is version 1 of file f1.txt
f2.txt → dir/file.txt: This is version 1 of file dir/file.txt
 
Backup of regular file:
f1.txt: This is version 2 of file f1.txt
f1.txt.backup: This is version 1 of file f1.txt
 
Backup of symbolic link to file:
f2.txt → dir/file.txt: This is version 2 of file dir/file.txt
dir/file.txt.backup: This is version 1 of file dir/file.txt</pre>
 
=={{header|Perl}}==
Anonymous user