16
\$\begingroup\$

This is a code-golf question. You need to take the contents of two separate files (i.txt and o.txt) and swap them. You can create a third file to help you with the process, but have your program delete it after the file contents are swapped. Renaming the files is now allowed again.

Programs must work for any file content.

Shortest code wins, all eligible submissions will be up-voted.

\$\endgroup\$
5
  • 2
    \$\begingroup\$ Rename the files. No need to "take the contents" and swap them to achieve the result. \$\endgroup\$ Commented Dec 28, 2013 at 19:32
  • \$\begingroup\$ @Darren Stone I think that should be illegal. Thanks for catching that before answers were submitted. \$\endgroup\$ Commented Dec 28, 2013 at 19:33
  • \$\begingroup\$ Looks like a bad constraint, renaming is the best way to do this \$\endgroup\$ Commented Dec 28, 2013 at 20:47
  • 1
    \$\begingroup\$ Alright, I can remove this constraint. \$\endgroup\$ Commented Dec 28, 2013 at 20:55
  • 11
    \$\begingroup\$ @hdante but 'renaming files' is not exchanging their contents. On a system that uses inodes the goal here would be for i.txt's inode to contain the data from o.txt's inode, and vice versa, so that if there are hardlinks to those inodes elsewhere, their contents will appear swapped as well. Renaming can't accomplish that. \$\endgroup\$ Commented May 11, 2014 at 3:07

27 Answers 27

11
\$\begingroup\$

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 
\$\endgroup\$
6
  • 1
    \$\begingroup\$ I like the one without a temporary file better, though: i=`<i.txt`;<o*>i*;<<<$i>o*. It’s too bad this has to be shorter. \$\endgroup\$ Commented Dec 28, 2013 at 19:54
  • 1
    \$\begingroup\$ Rules changed to allow renaming files. You can update your code if you wish. \$\endgroup\$ Commented Dec 28, 2013 at 20:56
  • \$\begingroup\$ You could save 3 chars by replacing <t>o*;rm t by mv t o* ! \$\endgroup\$ Commented May 11, 2014 at 23:23
  • \$\begingroup\$ @F.Hauri: Ah, thanks! (There were rules against renaming at one point.) \$\endgroup\$ Commented May 11, 2014 at 23:44
  • \$\begingroup\$ This code is not correct. It works only in the special case where i* and o* will match only i.txt and o.txt respectively, i.e. there is no other file with a name beginning with either "i" or "o". \$\endgroup\$ Commented May 15, 2014 at 13:25
3
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ 59 bytes \$\endgroup\$ Commented Apr 7 at 14:25
3
\$\begingroup\$

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 
\$\endgroup\$
1
  • \$\begingroup\$ I didn't know you can use $r=rename to create aliases to functions in PHP. Thanks! \$\endgroup\$ Commented May 11, 2014 at 15:15
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Given that it’s a code-golf, you could at least take out the spaces and try somewhat for the shortest. \$\endgroup\$ Commented Dec 28, 2013 at 20:42
  • \$\begingroup\$ Totally didn't realize it was code-golf, sorry \$\endgroup\$ Commented Dec 28, 2013 at 21:15
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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 
\$\endgroup\$
2
  • \$\begingroup\$ Looks like your code has a lot of whitespace. Are you sure they are needed? You can have a much better score if you remove them. \$\endgroup\$ Commented May 15, 2014 at 10:27
  • \$\begingroup\$ I don't think it's needed the editor just threw it in. Removed apart from where it's necessary :) \$\endgroup\$ Commented May 15, 2014 at 12:32
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ This can fail with certain encoding. Because gc default is UTF8NoBOM but sc default is ASCII. Try some UTF-8 files or binary files and you can see how it can fail. \$\endgroup\$ Commented Dec 26, 2018 at 20:09
  • \$\begingroup\$ Indeed. Thanks. It need -e by -n parameters to ensure that the files unchanged. It would be nice to add a parameter -readCount 0 to performance. The script with rename is shorter :) Fixed. \$\endgroup\$ Commented Dec 27, 2018 at 6:26
2
\$\begingroup\$

Two 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} \" 
\$\endgroup\$
1
  • \$\begingroup\$ Nota: As first patch output is not redirected, they become garbage on second patch command. This is the reason because only one patching file o.txt appear. \$\endgroup\$ Commented May 11, 2014 at 22:20
2
\$\begingroup\$

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 
\$\endgroup\$
2
  • \$\begingroup\$ Nice answer! vy can be ε for -1. Also, í¼t and ‚™ can simply be txt and re; the dictionary string and hard-coded string are the same byte-count. Try it online. \$\endgroup\$ Commented Jun 2, 2023 at 12:26
  • \$\begingroup\$ Oh, and ’.txt’ can be ’.txt"’, allowing you to drop the " after both ÿ for another -1: try it online - 35 bytes \$\endgroup\$ Commented Jun 2, 2023 at 12:32
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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 
\$\endgroup\$
2
  • \$\begingroup\$ @minitech, yes it does. 'ੴ' becomes 'o.txt'. Those are mv operations, not cp. \$\endgroup\$ Commented Dec 29, 2013 at 1:44
  • \$\begingroup\$ Second best answer so far! Reduce it by two characters and you win! I do not care about fixed filenames. \$\endgroup\$ Commented Dec 29, 2013 at 4:28
1
\$\begingroup\$

Python:

import os l,e='i_o'*2,'.txt' for x,y in zip(l,l[1:])[::2]: os.rename(x+e,y+e) 
\$\endgroup\$
1
\$\begingroup\$

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...

\$\endgroup\$
2
  • \$\begingroup\$ You just overwrote my a file! \$\endgroup\$ Commented May 14, 2014 at 16:12
  • \$\begingroup\$ @Danny The question states that you may create another file, but it must be deleted. \$\endgroup\$ Commented May 14, 2014 at 16:21
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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); 
\$\endgroup\$
1
\$\begingroup\$

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 
\$\endgroup\$
1
\$\begingroup\$

PHP, 45

<?php copy('i','t');copy('o','i');rename('t','o'); 

Not very golfy but shortest PHP so far.

\$\endgroup\$
1
  • \$\begingroup\$ file extensions are missing. \$\endgroup\$ Commented Dec 26, 2018 at 9:05
1
\$\begingroup\$

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) 
\$\endgroup\$
1
\$\begingroup\$

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; } 
\$\endgroup\$
1
\$\begingroup\$

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 
\$\endgroup\$
1
\$\begingroup\$

Windows Batch, 39 bytes

ren i.txt x&ren o.txt i.txt&ren x o.txt
\$\endgroup\$
1
\$\begingroup\$

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) 

Try it online!

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.

\$\endgroup\$
1
\$\begingroup\$

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>'.

\$\endgroup\$
1
\$\begingroup\$

AWK, 66 bytes

a=FILENAME,b=!b?a:b{c[a]=c[a]$0RS}END{printf c[a]>b;printf c[b]>a} 

Try it online!

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 
\$\endgroup\$
1
\$\begingroup\$

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).

\$\endgroup\$
1
\$\begingroup\$

Tcl, 71 bytes

file ren i.txt m file ren -force o.txt i.txt file ren m o.txt file de m 

Try it online!


# [Tcl], 84 bytes

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 

Try it online!


# [Tcl], 122 bytes
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 

Try it online!

\$\endgroup\$
6
0
\$\begingroup\$

SmileBASIC, 36 35 bytes

@L RENAME@OI[2-I],@IO[I]I=I+1GOTO@L 
\$\endgroup\$