Moving files with ruby...

D

darren kirby

--nextPart1200391.Mms6yP9lv3
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hello all,

A script I have written needs the ability to move files. I thought I would =
be=20
clever and use File.rename(old, newdir + new) but this is giving me an erro=
r:=20
`rename': Invalid cross-device link. I gather this is because I am renaming=
=20
the file to a different partition.

So then I found FileUtils and used:
require 'fileutils'
include FileUtils
mv( old, new)

This is working but seems an overly verbose and convoluted way to move a fi=
le.=20
I had a look at the fileutils.rb source and found this (for 'mv'):
# Moves file(s) +src+ to +dest+. If +file+ and +dest+ exist on the differe=
nt
# disk partition, the file is copied instead
=20
So I guess my real question is: is there a technical reason or some arbitra=
ry=20
reason there is no File.move() method that can handle this transparently? I=
=20
would think it would be quite useful...

Any insight is appreciated,
=2Dd
=2D-=20
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
=2D Dennis Ritchie and Ken Thompson, June 1972

--nextPart1200391.Mms6yP9lv3
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQBE7OPowPD5Cr/3CJgRAhUCAJ9l96iPulTADd6DVxuYHP3/eYxCPwCeNkl7
+lUriMRAgr5tCBSE48bxZ5U=
=PUtC
-----END PGP SIGNATURE-----

--nextPart1200391.Mms6yP9lv3--
 
D

darren kirby

--nextPart1906406.5LyV87yNX1
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

quoth the Paul Lutus:
No, it is because you are trying to use "rename" to physically move a fil= e,
and the system is objecting to this misconception. File.rename can only
rename files, it cannot move them. It works only on one storage
device/volume/whatever.

Well, I think that's pretty much what I said. If you 'rename' a file=20
from /somepath/somefile.txt to /someotherpath/somefile.txt within the same=
=20
partition you have effectively 'moved' it haven't you...
Umm, compared to what? It moves the file using a method named "mv", just
like the Linux/Unix command of the same name.

I am typically very impressed with how much you can do with a little amount=
of=20
code in Ruby. For instance, I only had to add one line to add some recursiv=
e=20
directory handling in my script:

=46rom:
files +=3D Dir.glob("*.#{t}")
To:
files +=3D Dir.glob("**/*.#{t}")

Whereas to move a file, a fairly basic operation, I need four lines of code:

require 'fileutils'
include FileUtils
cp(old, new )
rm (old)

I have to change "mv" to "cp" and add the "rm" because the old file is not=
=20
automatically removed...
That is what "mv" does. If the new and old filenames and paths allow the
file to remain on the same device, a simple rename is carried out. If not,
the file is copied to the destination and deleted from the source.

Right, I've got that...
It might be because not all operating systems on which Ruby can be
installed know the "mv" operation as described above. Just a guess.

I am just wondering because there are (AFAIK) other methods that handle the=
=20
operation transparently even though the underlying OS does it differently.=
=20

Not trying to troll, I am just curious...

=2Dd
=2D-=20
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
=2D Dennis Ritchie and Ken Thompson, June 1972

--nextPart1906406.5LyV87yNX1
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQBE7O4twPD5Cr/3CJgRAs26AJsEl+avWGgLRQKxAfJW9I4e+lpSSACcCrhp
PZtohOlv6YHoYWhW2IMbl3o=
=0HoE
-----END PGP SIGNATURE-----

--nextPart1906406.5LyV87yNX1--
 
D

darren kirby

--nextPart1727284.zytX3azMdk
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

quoth the Marc Heiler:
well
system("mv #{file}")
is also possible ,)

I may be wrong, but I always had the impression that using a system command=
to=20
do something available within the language is considered a kludge.
include FileUtils isnt needed so that should reduce your
LOC by -1

When I comment the line out I get:
undefined method `cp' for main:Object (NoMethodError)
Doesnt FileUtils have
mv(src, dest, options)
?

Yes, it does. But the thing is that if you are moving the file across=20
partitions it will only copy and not delete the original file. I will not=20
know if we are moving cross-partition, so I have to use cp and rm...

=46rom the link: "If file and dest exist on the different disk partition, t=
he=20
file is copied instead."

=2Dd
=2D-=20
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
=2D Dennis Ritchie and Ken Thompson, June 1972

--nextPart1727284.zytX3azMdk
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQBE7SRMwPD5Cr/3CJgRAhcmAKCuDpaHhYN6BFQsVYDJn+USkqUz5gCgwKKp
XPgGaFeFfSzycMmKCkUc9bs=
=VYL1
-----END PGP SIGNATURE-----

--nextPart1727284.zytX3azMdk--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top