Make a backup file: Difference between revisions
Content deleted Content added
Applesoft BASIC |
Thundergnat (talk | contribs) m syntax highlighting fixup automation |
||
Line 17:
{{trans|Python}}
<
fs:rename(fs:path:canonical(targetfile), fs:path:canonical(targetfile)‘.bak’)
V f = File(fs:path:canonical(targetfile), ‘w’)
f.write(‘this task was solved during a talk about rosettacode at the PyCon China in 2011’)
f.close()</
=={{header|Applesoft BASIC}}==
Due to all the pitfalls in this task it is helpful to turn on debugging by default. It is also helpful, to show each DOS 3.3 Command, by entering MON C before running the program. There is an extra PRINT statement in line 540 to work-around an issue with the display of DOS 3.3 commands after an error occurs. Setting ERASEANYBACKUP will delete the backup file if it already exists. Delete lines 180 to 230 with DEL 180,230 and it is possible with DOS 3.3 to have backup files all with the same name due to a pitfall in the RENAME Command.
<
110 DEBUG = NOT FALSE
120 F$ = "FILE"
Line 62:
540 PRINT
550 IF DEBUG THEN PRINT " <<< "N$" DOES NOT EXIST."
560 RETURN</
<syntaxhighlight lang="text">DELETE FILE
DELETE BACKUP FILE
MON C</
<syntaxhighlight lang
{{out}}
<pre>
Line 76:
CLOSE FILE
</pre>
<syntaxhighlight lang
{{out}}
<pre>
Line 90:
CLOSE FILE
</pre>
<syntaxhighlight lang
{{out}}
<pre>
Line 101:
</pre>
=={{header|AutoHotkey}}==
<
if FileExist(targetfile)
FileMove, %targetfile%, %targetfile%.bak
Line 113:
}
file.Write("This is a test string.`r`n")
file.Close()</
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f MAKE_A_BACKUP_FILE.AWK filename(s)
# see: http://www.gnu.org/software/gawk/manual/gawk.html#Extension-Sample-Inplace
Line 133:
exit(0)
}
</syntaxhighlight>
=={{header|Batch File}}==
<
@echo off
setlocal enabledelayedexpansion
Line 157:
echo !line[1]!>"%filePath%"
for /l %%i in (2,1,%i%) do echo !line[%%i]!>>"%filePath%"
</syntaxhighlight>
=={{header|Common Lisp}}==
Appends a version number and increments it on each backup
<
(ignore-errors (apply #'parse-integer args)))
Line 179:
(rename-file file (get-next-version file))
(with-open-file (out file :direction :output)
(print data out))))</
=={{header|Elixir}}==
<
def backup_file(filename) do
backup = filename <> ".backup"
Line 193:
end
hd(System.argv) |> RC.backup_file</
=={{header|Go}}==
===Rename===
This is the technique of the task description.
<
import (
Line 230:
fmt.Println(err)
}
}</
===Copy===
Alternative technique copies an existing file to make the backup copy, then updates the origial file. In an attempt to keep operations atomic, the original file is opened as the first operation and is not closed until all operations are complete. In an attempt to avoid data loss, the original file is not modified until the backup file is closed.
<
import (
Line 313:
// backup complete (as long as err == nil)
return
}</
=={{header|Java}}==
{{works with|Java|7+}}
<
import java.io.IOException;
import java.io.PrintWriter;
Line 351:
}
}
}</
Contents of 'original.txt' ''before'' the program is run and of 'original.txt.backup' ''after'' it is run:
Line 368:
{{works with|Java|1.5+}}
<
import java.io.IOException;
import java.io.PrintWriter;
Line 397:
}
}
}</
{{out}}
Line 405:
{{works with|Julia|0.6}}
<
mv(realpath(targetfile), realpath(targetfile) * ".bak")
# "a+" for permissions of reading, writing, creating
open(targetfile, "w+") do io
println(io, "this task was solved during a talk about rosettacode at the PyCon China in 2011")
end</
=={{header|Kotlin}}==
{{trans|Java}}
<
import java.io.File
Line 433:
fun main(args: Array<String>) {
saveWithBackup("original.txt", "fourth", "fifth", "sixth")
}</
Contents of 'original.txt' ''before'' the program is run and of 'original.txt.backup' ''after'' it is run:
Line 450:
=={{header|Lasso}}==
<
// create file
Line 469:
// create new file with new contents
local(nf = file(#file2use))
#nf->doWithClose => { #nf->writeBytes(#contents_of_f->asBytes) }</
=={{header|Locomotive Basic}}==
Line 477:
=={{header|Nim}}==
In case the backup cannot be done, an exception is raised.
<
const
Line 525:
# Cleanup.
setCurrentDir(oldDir)
removeDir(Dir)</
{{out}}
Line 541:
=={{header|Perl}}==
<
use warnings;
Line 580:
# back up this program
backup($0,3,'.bk');</
=={{header|Phix}}==
{{trans|Go}}
<
if not rename_file(targetfile, targetfile&".bak", overwrite:=true) then
puts(1,"warning: could not rename file\n")
Line 594:
puts(fn,"this task was translated from the Python entry\n")
close(fn)
end if</
Before basing anything on the above code, though, I would recommend you take a look at <br>
function saveFile in demo\edix\edix.exw, which does this sort of thing for real: <br>
Line 601:
=={{header|PicoLisp}}==
PicoLisp makes use of external commands as much as possible (at least for not time-critical operations), to avoid duplicated functionality.
<
(call 'mv Path (pack Path ".backup"))
(out Path
(prinl "This is the new file") ) )</
=={{header|Pike}}==
<
targetfile = System.resolvepath(targetfile);
mv(targetfile, targetfile+"~");
Stdio.write_file(targetfile, "this task was solved at the pycon china 2011");</
=={{header|Python}}==
Using [https://docs.python.org/library/os.html os library]
<syntaxhighlight lang="python">
import os
targetfile = "pycon-china"
Line 621:
f.write("this task was solved during a talk about rosettacode at the PyCon China in 2011")
f.close()
</syntaxhighlight>
Or using a newer [https://docs.python.org/library/pathlib.html pathlib library] (Python >= 3.4):
<syntaxhighlight lang="python">
from pathlib import Path
Line 630:
with filepath.open('w') as file:
file.write("New content")
</syntaxhighlight>
=={{header|Racket}}==
Line 636:
This version keeps unlimited backups, with <tt>*.bak</tt> being the freshest one, <tt>*.bak1</tt> is an older backup, etc. So each backup moves all existing names up.
<syntaxhighlight lang="racket">
#lang racket
Line 654:
(revise "fff")
</syntaxhighlight>
=={{header|Raku}}==
Line 660:
{{works with|Rakudo|2017.10}}
<syntaxhighlight lang="raku"
# where n is in the range 1 - $limit (default 3).
# Prints 'File not found' to STDERR if the file does not exist.
Line 694:
# Optionally, specify limit, back-up extension pattern and whether to follow symlinks.
# Optional parameters can be in any order, in any combination.
backup 'myfile', :follow-symlinks, :limit(2), :ext('bak');</
=={{header|REXX}}==
This REXX version executes under DOS or DOS under Windows.
<
parse arg oFID . /*get a required argument from the C.L.*/
if oFID=='' then do /*No argument? Then issue an err msg.*/
Line 710:
'RENAME' oFID tFID /*rename the original file to backup. */
call lineout oFID, '═══This is line 1.' /*write one line to the original file. */
/*stick a fork in it, we're all done. */</
The contents of the original file (before execution): '''A.FILE''':
<pre>
Line 735:
=={{header|Ruby}}==
This version does not overwrite the backup file if it exists.
<
filename = File.realpath(filename)
bkup = filename + ".backup"
Line 753:
end
1.upto(12) {|i| backup_and_open(ARGV[0]) {|fh| fh.puts "backup #{i}"}}</
Example:
Line 791:
=={{header|Scala}}==
===Java Interoperability===
<
import java.nio.file.{Files, Paths, StandardCopyOption}
Line 813:
saveWithBackup("original.txt", "fourth", "fifth", "sixth")
}</
=={{header|Tcl}}==
<
proc backupopen {filename mode} {
Line 837:
}
return [open $filename $mode]
}</
=={{header|VBA}}==
<
If Len(Dir(filename)) > 0 Then
On Error Resume Next
Line 857:
Public Sub main()
backup "D:\test.txt"
End Sub</
=={{header|Wren}}==
{{libheader|Wren-ioutil}}
<
var saveWithBackup = Fn.new { |filePath, lines|
Line 879:
System.print(File.read("original.txt"))
System.print("Contents of original.txt.backup:")
System.print(File.read("original.txt.backup"))</
{{out}}
|