Swap contents in two files

I have two files, I want to swap the contents of the files in my
program, suggestions?

2006/12/14, Christopher L. [email protected]:

I have two files, I want to swap the contents of the files in my
program, suggestions?

Renaming with a third temp file name is the most efficient if you know
that no file handles are open on these files.

robert

Robert K. wrote:

2006/12/14, Christopher L. [email protected]:

I have two files, I want to swap the contents of the files in my
program, suggestions?

Renaming with a third temp file name is the most efficient if you know
that no file handles are open on these files.

robert

How the code for that program going to look like?

On 12/14/06, Christopher L. [email protected] wrote:

How the code for that program going to look like?

How abt:
require ‘fileutils’

FileUtils.copy_file(“a.exe”,“b.exe”)
FileUtils.mv “a.exe.stackdump”,“a.exe”
FileUtils.mv “b.exe”,“a.exe.stackdump”

Hi –

On Fri, 15 Dec 2006, [email protected] wrote:

a, b, ignored = ARGV

You can also do:

a, b = *ARGV

(which I know you know but it’s just another option for anyone who
doesn’t want the extra variable :slight_smile:

David

On Thu, 14 Dec 2006, Christopher L. wrote:

How the code for that program going to look like?
harp:~ > cat a.rb
require ‘tempfile’
require ‘fileutils’

def swap a, b
fu, f = FileUtils, File
tmpdir = f.dirname b
t = Tempfile.new f.basename(b), f.dirname(b)
t.close
c = t.path
fu.mv b, c
fu.mv a, b
fu.mv c, a
end

a, b, ignored = ARGV

swap a, b

harp:~ > cat a
a

harp:~ > cat b
b

harp:~ > ruby a.rb a b

harp:~ > cat a
b

harp:~ > cat b
a

-a

On Fri, 15 Dec 2006 [email protected] wrote:

(which I know you know but it’s just another option for anyone who
doesn’t want the extra variable :slight_smile:

quite right. i’ve moved to the former because it’s self doccumenting :
a
crutch for my feeble mind and almost complete lack of doccumentation
skills!

:wink:

-a

thanks

Hi –

On Fri, 15 Dec 2006, [email protected] wrote:

a, b = *ARGV

(which I know you know but it’s just another option for anyone who
doesn’t want the extra variable :slight_smile:

quite right. i’ve moved to the former because it’s self doccumenting : a
crutch for my feeble mind and almost complete lack of doccumentation skills!

Come to think of it… you can do:

a, b = ARGV

I’m not sure why the star presented itself to my brain.

David

On Dec 14, 2006, at 9:50 AM, Robert K. wrote:

I usually use

a,b, = *ARGV

Yuck. :wink:

James Edward G. II

On 14.12.2006 16:23, [email protected] wrote:

a, b = *ARGV

(which I know you know but it’s just another option for anyone who
doesn’t want the extra variable :slight_smile:

quite right. i’ve moved to the former because it’s self doccumenting : a
crutch for my feeble mind and almost complete lack of doccumentation
skills!

I usually use

a,b, = *ARGV

Kind of to be sure… :slight_smile:

robert

Christopher L. wrote:

I have two files, I want to swap the contents of the files in my
program, suggestions?

Why not just rename the files? Or am I missing your meaning?

Tom

Hemant K. wrote:

On 12/14/06, Christopher L. [email protected] wrote:

How the code for that program going to look like?

How abt:
require ‘fileutils’

FileUtils.copy_file(“a.exe”,“b.exe”)
FileUtils.mv “a.exe.stackdump”,“a.exe”
FileUtils.mv “b.exe”,“a.exe.stackdump”

I can’t get your code to work, got a error message:
/usr/local/lib/ruby/1.8/fileutils.rb:501:in rename': No such file or directory - a.stackdump or a (Errno::ENOENT) from /usr/local/lib/ruby/1.8/fileutils.rb:501:inmv’
from /usr/local/lib/ruby/1.8/fileutils.rb:1379:in
fu_each_src_dest' from /usr/local/lib/ruby/1.8/fileutils.rb:1395:infu_each_src_dest0’
from /usr/local/lib/ruby/1.8/fileutils.rb:1377:in
fu_each_src_dest' from /usr/local/lib/ruby/1.8/fileutils.rb:490:inmv’
from test.rb:4

Christopher L. wrote:

Hemant K. wrote:

On 12/14/06, Christopher L. [email protected] wrote:

How the code for that program going to look like?

How abt:
require ‘fileutils’

FileUtils.copy_file(“a.exe”,“b.exe”)
FileUtils.mv “a.exe.stackdump”,“a.exe”
FileUtils.mv “b.exe”,“a.exe.stackdump”

what’s stackdump?

Christopher L. wrote:

I have two files, I want to swap the contents of the files in my
program, suggestions?

I arrived late, here’s my untested solution:

sa,sb, nevermind = ARGV

da = File.read(sa)
db = File.read(sb)

File.open(sa,“w”) { |f| f.write db }
File.open(sb,“w”) { |f| f.write da }

Should work for all but the biggest files.

On 12/14/06, Christopher L. [email protected] wrote:

FileUtils.mv “a.exe.stackdump”,“a.exe”
FileUtils.mv “b.exe”,“a.exe.stackdump”

what’s stackdump?

Heh… don’t worry abt stackdump its the stackdump file of of a.exe,
with whom I am going to swap the content. b.exe was the temporary
file. I would suggest go for Ara’s solution, I jumped the gun a tad
too quickly.

but, before running mine script, please put your actual file names…
in stead of dummy one that I have put for you. Obviously, in your
current directory those files are not there and hence the error:

require ‘fileutils’

FileUtils.copy_file(“src_file”,“temp_file”)
FileUtils.mv “dest_file”,“src_file”
FileUtils.mv “temp_file”,“dest_file”

but if you are planning to use, above code, you will have to make sure
that your program doesn’t have another temp_file open with same name.

Got to see this topic just now. I do agree with Tom’s opinion.
Shiwei

Shiwei Z. wrote:

Got to see this topic just now. I do agree with Tom’s opinion.
Shiwei

http://pastie.org/1066992

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs