Make a backup file: Difference between revisions
Content deleted Content added
imported>Fth |
Added FreeBASIC |
||
Line 204: | Line 204: | ||
close-file throw ; |
close-file throw ; |
||
s" testfile" backup |
s" testfile" backup</syntaxhighlight> |
||
⚫ | |||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="vbnet">Sub saveWithBackup(filePath As String, lines() As String) |
|||
Dim As String origPath = filePath |
|||
If Len(Dir(origPath)) > 0 Then |
|||
Dim As String backupPath = origPath & ".backup" |
|||
Name(origPath, backupPath) 'Renames a file on disk |
|||
End If |
|||
Open origPath For Output As #1 |
|||
For i As Integer = 1 To Ubound(lines) |
|||
Print #1, lines(i) |
|||
Next i |
|||
Close #1 |
|||
End Sub |
|||
Dim lines(1 To 3) As String => {"fourth", "fifth", "sixth"} |
|||
saveWithBackup("original.txt", lines()) |
|||
Dim As String linea |
|||
' check it worked |
|||
Print "Current contents of original.txt:" |
|||
Open "original.txt" For Input As #1 |
|||
While Not Eof(1) |
|||
Line Input #1, linea |
|||
Print linea |
|||
Wend |
|||
Close #1 |
|||
Print !"\nContents of original.txt.backup:" |
|||
Open "original.txt.backup" For Input As #1 |
|||
While Not Eof(1) |
|||
Line Input #1, linea |
|||
Print linea |
|||
Wend |
|||
Close #1 |
|||
⚫ | |||
{{out}} |
|||
Contents of 'original.txt' ''before'' the program is run and of 'original.txt.backup' ''after'' it is run: |
|||
<pre> |
|||
first |
|||
second |
|||
third |
|||
</pre> |
|||
Contents of 'original.txt' ''after'' the program is run: |
|||
<pre> |
|||
fourth |
|||
fifth |
|||
sixth |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
===Rename=== |
===Rename=== |