| Bytes | Lang | Time | Link |
|---|---|---|---|
| 071 | Tcl | 181224T000614Z | sergiol |
| 021 | Uiua | 241128T201531Z | noodle p |
| 224 | Pascal | 241128T101002Z | Kai Burg |
| 035 | 05AB1E nolazy | 210527T025554Z | Makonede |
| nan | zsh | 131228T194958Z | Ry- |
| 062 | Two bash based answers 52 and | 140511T221401Z | F. Hauri |
| 066 | AWK | 210609T065105Z | cnamejj |
| 049 | Powershell | 181224T053548Z | mazzy |
| 052 | PHP | 181226T090840Z | Titus |
| 035 | SmileBASIC | 181226T031158Z | 12Me21 |
| 7170 | Lua | 181224T032225Z | cyclamin |
| 077 | Python | 131230T121314Z | AsksAnyw |
| 039 | Windows Batch | 171127T201453Z | peter fe |
| 120 | Perl | 140517T012047Z | Heiko Ob |
| 072 | Ruby | 140517T003424Z | Aearnus |
| 065 | C | 140516T004044Z | Fors |
| nan | 140515T091339Z | jaybee3 | |
| 099 | Groovy | 140514T214707Z | Michael |
| 089 | PHP | 140514T084328Z | MisterBl |
| 045 | PHP | 140511T123041Z | mleko |
| nan | 140511T151936Z | draegtun | |
| 172 | PHP | 140511T010111Z | Spedward |
| 162 | C | 140511T002032Z | bacchusb |
| 048 | Windows Batch File | 140510T191344Z | kitcar20 |
| nan | 131230T112419Z | tamasgal | |
| 024 | Shell script | 131229T000810Z | Darren S |
| nan | 131228T203012Z | user1148 |
Tcl, 71 bytes
file ren i.txt m
file ren -force o.txt i.txt
file ren m o.txt
file de m
With renaming allowed, things get shorter:
file rename i.txt m
file rename -force o.txt i.txt
file rename m o.txt
file delete m
set A [read [open i.txt]]
puts [set i [open i.txt w]] [read [open o.txt]]
puts [set o [open o.txt w]] $A
close $i
close $o
Uiua, 21 bytes
⍜∩&frab:∩$"_.txt"@i@o
Or, with file names input to the function, 8 bytes:
⍜∩&frab:
This is a very beautiful piece of code which anybody who has not seen Uiua before should learn from. The crux of it is the modifier ⍜ under, which applies a transformation function, another function, and the inverse function of the transformation.
Here, the transformation function is ∩&frab and the applied function is :.
∩ both applies a function to two values, and &frab is a system function to read the file at a given path into an array of bytes, so ∩&frab says "read both files".
The function : flip takes the two arrays and flips them, so the first becomes the second and the second becomes the first.
Finally, the transformation function is undone, writing both files to the corresponding paths from before.
Pascal, 224 B
This complete Pascal program requires a processor meeting the requirements of ISO standard 7185 “Standard Pascal” or ISO standard 10206 “Extended Pascal”.
program X(A,B);var A,B,C:text;Z:char;procedure M(var S,D:text);begin
reset(S);rewrite(D);while not EOF(S) do
begin while not EOLn(S) do
begin read(S,Z);write(D,Z)end;readLn(S);writeLn(D)end
end;begin M(A,C);M(B,A);M(C,B)end.
The notion of files being organized in a file system is foreign to Pascal so renaming files is not an option. Commented:
program swap(A, B);
var
A, B, C: text;
procedure copy(var source, destination: text);
var
buffer: char;
begin
reset(source);
rewrite(destination);
while not EOF(source) do
begin
{ A `text` file is a (possibly empty) sequence of lines.
Each line is a (possibly empty) sequence of `char` values
followed by one implementation‐defined end‐of‐line sequence. }
while not EOLn(source) do
begin
read(source, buffer);
write(destination, buffer)
end;
{ `Read` can (by design) not read across lines. }
readLn(source);
{ Upon reaching the end of a line, the file buffer `source↑`
returns the `char` value of a space character.
Therefore, in Pascal the only correct method of
emitting an end‐of‐line character sequence is `writeLn`. }
writeLn(destination)
end
end;
begin
copy(A, C); { original A = C }
copy(B, A); { original B = new A }
copy(C, B) { C = original A = new B }
end.
Note that the method of associating program parameters with external entities is implementation‐defined.
For example the GNU Pascal Compiler prompts you for pathnames when reset or rewrite are invoked for the first time:
Input file `A': i.txt
Input file `B': o.txt
The i.txt and o.txt are user input (i. e. not generated by the program) and only program parameters are queried in this fashion (so not C).
The FreePascal Compiler in its (not yet mature) ISO compiler compatibility mode, on the other hand, expects pathnames as command‐line arguments.
However, as of version 3.2.4 the FreePascal Compiler does not support anonymous files; the text file C is not associated with a pathname and the FPC cannot deal with that (fails miserably).
05AB1E --no-lazy, 39 37 35 bytes
-2 thanks to Kevin Cruijssen.
'°ŽDÀøεS’.í¼t"’«`’File.‚™‚ˆ"ÿ,"ÿ’.E
Try it online! Link includes a header and footer to create the files and prove that it worked.
'°ŽDÀøεS’...’«`’...’.E # trimmed program
ε # for each in...
ø # all characters of...
'°Ž # "bio"...
ø # paired with corresponding characters of...
'°ŽD # "bio"...
À # rotated left one character...
.E # evaluate...
’...’ # "File.rename\"ÿ\",\"ÿ\""...
# (implicit) with the first ÿ replaced by...
` # second element of...
S # list of characters of...
# (implicit) current element...
# (implicit) with each element...
« # concatenated with...
’...’ # ".txt"...
# (implicit) with the second ÿ replaced by...
` # first element of...
S # list of characters of...
# (implicit) current element...
# (implicit) with each element...
« # concatenated with...
’...’ # ".txt"...
.E # as Elixir code
====================
File.rename # full program
File.rename # rename file with name argument 1 to argument 2
zsh, 20 + 4 = 24
The script needs to be named .txt.
(mv [io]$0;>i$0)<o$0
or 14 with parameters:
(mv $@;>$1)<$2
Two bash based answers; 52 and 62 chars
shell: diff + patch (+ tee + sed...) 52
Maybe not the shorter, but I find this fun (and there is no use of temporary file):
diff -u [io]*|tee >(patch -r -)|sed 1s/i/o/|patch -R
Where content is swapped and files are modified in place:
Sample run
swapContent() { diff -u $1 $2|tee >(patch -r -)|sed 1s/$1/$2/|patch -R ;}
while read page file ;do man -Pcol\ -b $page >$file.txt;done <<<$'man i\nbash o'
printf "%s %8d %s\n" $(join -j 2 <(stat -c '%s %n' [io]*) <(md5sum [io]*))
swapContent [io]*
printf "%s %8d %s\n" $(join -j 2 <(stat -c '%s %n' [io]*) <(md5sum [io]*))
Could produce something like:
i.txt 46007 1da1f7533e0eab1e97cce97bb7ca1d3b
o.txt 321071 7dcd230890faf4ef848d4745eda14407
patching file o.txt
i.txt 321071 7dcd230890faf4ef848d4745eda14407
o.txt 46007 1da1f7533e0eab1e97cce97bb7ca1d3b
use of xargs to simplify mv requests
Not as funny, but nice anyway.
set -- {i,o}.txt t&&eval 'xargs -n2 mv<<<"' \${1,3,2,1,3,2} \"
AWK, 66 bytes
a=FILENAME,b=!b?a:b{c[a]=c[a]$0RS}END{printf c[a]>b;printf c[b]>a}
Note: I provided a TIO line, but I have no idea how to make it work when the code needs to read from input files?
The rename approaches seem to be much shorter, but here's one in AWK that copies the data instead. At a high level, it takes a commandline like,
gawk '...code...' i.txt o.txt
which feeds both files into the program on STDIN. It uses the magic variable FILENAME to cache the contents of each one into a separate entry in an associative array. Then the END clause just overwrites each file with the content from the other one.
The "test" per line, which is always truthy, sets a to the current filename over and over. And it also ensures b is to the first filename seen.
a=FILENAME,b=!b?a:b
The code block executed per line, just appends the current line to the accumulated data for the current filename.
c[a]=c[a]$0RS
Then once all the lines from both files have been read, the END code block writes out the files with the content swapped.
printf c[a]>b;printf c[b]>a
Powershell, 44 49 bytes
$t='.txt'
ren i$t a$t -fo
ren o$t i$t
ren a$t o$t
where ren is alias for Rename-Item. The script uses and delete a third file a.txt.
PHP, 52 bytes
AsksAnyway´s php modernized & golfed:
($r=rename)(i.$e=".txt",x);$r(o.$e,i.$e);$r(x,o.$e);
Run with php -nr '<code>'.
SmileBASIC, 36 35 bytes
@L
RENAME@OI[2-I],@IO[I]I=I+1GOTO@L
Lua, 71 70 bytes
_ENV=os t='.txt'i,o,r='i'..t,'o'..t,rename r(i,t)r(o,i)r(t,o)remove(t)
Define os, the operating system library, as the global table so we can write rename and remove instead of os.rename and os.remove. (Incidentally this also means the one-letter variables are actually fields in the os table.) Make short alias for os.rename to save some space. Set up filename variables, using '.txt' as temporary file. Do the renaming and deleting.
Python, 77
import os;t='.txt';r,i,o,x=[os.rename,'i'+t,'o'+t,'1'+t];r(i,x);r(o,i);r(x,o)
Python, 65
import os;t='.txt'
for a,b in zip('iox','xio'):os.rename(a+t,b+t)
Python, 63
import os;t='.txt'
for a,b in 'ix','oi','xo':os.rename(a+t,b+t)
PHP, 68
<?$r=rename;$t='.txt';$r("i$t","x");$r("o$t","i$t");$r("x","o$t");?>
Windows Batch File, 42
move i.txt x&move o.txt i.txt&move x o.txt
Windows Batch File (args), 30
move %1 x&move %2 %1&move x %2
Windows Batch, 39 bytes
ren i.txt x&ren o.txt i.txt&ren x o.txt
Perl, 120 bytes (Contents swapping without file renaming)
use open IO,':bytes';undef$/;open I,"<i.txt";$I=<I>;open I,"<o.txt";open O,">i.txt";print O<I>;open O,">o.txt";print O$I
The file contents is put into memory and written back to the other file. Thus i.txt and o.txt must fit into memory.
Since the file contents are actually exchanged, hard links are updated automatically, see AJManfield's comment.
Ungolfed:
use open IO => ':bytes'; # binmode, not needed for Unix, but needed for Windows
undef $/; # read whole file instead of lines
open I, "<i.txt"; # open i.txt for reading
$I = <I>; # read i.txt
open I, "<o.txt"; # open o.txt for reading
open O, ">i.txt"; # open i.txt for writing
print O <I>; # read o.txt and put the contents in i.txt
open O, ">o.txt"; # open o.txt for writing
print O $I; # write o.txt with contents of old i.txt
Ruby, 72 bytes
Wow! A Ruby code golf! I don't think that's ever been attempted before!
In all reality though, this required some nice Ruby shortcuts and a couple patterns which I found. It was my first golf ever and it was very fun to do. Without further ado, here's the golfed code:
3.times{|x|x*=2;t=".txt";a=([?i,?a,?o]*2);File.rename(a[x]+t,a[x+1]+t)}
And the ungolfed version
3.times do |x|
x = x * 2
t=".txt"
a=([?i,?a,?o]*2)
File.rename(a[x]+t, a[x+1]+t)}
end
The key factor in this is that the parameters passed to the File.rename are this, exactly:
File.rename "i.txt", "a.txt"
File.rename "o.txt", "i.txt"
File.rename "a.txt", "o.txt"
Hope this (doesn't) make sense!
C: 65 characters
#define r(a,b)rename(#a".txt",#b".txt");
main(){r(i,)r(o,i)r(,o)}
A quite simple solution in C that does the job. It uses a temporary name (.txt) for one of the files before giving it its proper new name.
Ungolfed (note how the syntax highlighting fails in the define, a bug has been uncovered!):
#include <stdio.h>
#define r(a, b) rename(#a ".txt", #b ".txt");
int main()
{
r(i, ) // rename("i.txt", ".txt");
r(o, i) // rename("o.txt", "i.txt");
r( , o) // rename( ".txt", "o.txt");
return 0;
}
VBA (148...132) and (126...110)
Renaming with a temp file t in the c:\ drive. Also first attempt at golf :S
Sub s():Set f=CreateObject("Scripting.FileSystemObject"):i="c:\i.txt":t="c:\t":f.MoveFile i,t:f.MoveFile "c:\o.txt",i:Kill t:End Sub
If scrrun.dll is referenced already, could cut it down a bit to 126...110.
Sub s():Set f=new FileSystemObject:i="c:\i.txt":t="c:\t":f.MoveFile i,t:f.MoveFile "c:\o.txt",i:Kill t:End Sub
Groovy - 99 chars
This is my attempt, with Groovy 2.2.1. I tried to do it without renaming:
f={new File(it+".txt")}
w={x,y->x.withWriter{it.write y}}
i=f "i"
o=f "o"
t=i.text
w i,o.text
w o,t
Ungolfed:
file = { new File(it+".txt") }
writeTextToFile = { x,y -> x.withWriter{it.write y} }
iFile = file("i")
oFile = file("o")
iText = iFile.text
writeTextToFile (iFile,oFile.text)
writeTextToFile (oFile,iText)
PHP, 89
I thought I'd give it a shot.
<?php $f1='f1.txt';$f2='f2.txt';$ft='ft.txt';copy($f1,$ft);copy($f2,$f1);rename($ft,$f2);
Ungolfed version:
<?php
$f1 = 'f1.txt';
$f2 = 'f2.txt';
$ft = 'ft.txt';
copy($f1, $ft);
copy($f2, $f1);
rename($ft, $f2);
Apparently I took 2 answers of here and combined them.. oh well.
PHP, 45
<?php
copy('i','t');copy('o','i');rename('t','o');
Not very golfy but shortest PHP so far.
Rebol - 46 (rename file) or 55 (r/w contents)
Rename file (using t as temporary file):
r: :rename
r i: %i.txt %t
r o: %o.txt i
r %t o
Read in then write out file contents:
a: read i: %i.txt
b: read o: %o.txt
write o a
write i b
PHP - 172
Golfed version of @EisaAdil's answer
$f1='file1.txt';$f2='file2.txt';$f1contents=file_get_contents($f1);$f2contents=file_get_contents($f2);file_put_contents($f1,$f2contents);file_put_contents($f2,$f1contents);
C 162
Golfed: uses t.txt as tmp file and swaps names then removes t.txt.
#include <stdio.h>
#define R(x,y) rename(x,y)
#define X(x) remove(x)
int main(){char *i="i.txt",*o="o.txt",*t="t.txt";R(i,t);X(i);R(o,i);R(t,o);X(t);return 0;}
Edit: removed 2 spaces
Windows Batch File (48)
type i.txt>a&type o.txt>i.txt&type a>o.txt&del a
I forgot about the move command when I wrote this...
Python:
import os
l,e='i_o'*2,'.txt'
for x,y in zip(l,l[1:])[::2]:
os.rename(x+e,y+e)
Shell script, 24
Works in Bash and probably most shells. Pass with your two filenames as parameters.
mv $1 ੴ;mv $2 $1;mv ੴ $2
If you want fixed filenames then this will do it, for a 12 char penalty:
mv i.txt ੴ;mv o.txt i.txt;mv ੴ o.txt
Ruby
i1="i.txt"
i2="o.txt"
c1=IO.readlines(i2)
c2=IO.readlines(i1)
File.open(i1){|x|x.puts(c1)}
File.open(i2){|x|x.puts(c2)}
Shortened version:
a=["i.txt","o.txt"]
(0..1).each{|x|b[x]=IO.readlines(a[x])*"\n"}
a.reverse!
(0..1).each{|x|IO.write(a[x],b[x])}
Not the shortest, but very simple and easy to read. Also, no intermediate files, only RAM.