[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Stupid Question #498 - How do I move a directory?

Allton, Paul

11/26/2003 5:57:00 PM

> -----Original Message-----
> From: Berger, Daniel [mailto:djberge@qwest.com]

> That means there was something wrong with your choice of file names
> because File.move in ftools.rb attempts to call 'rename' first. Only if
> that fails does it resort to readlink. My guess would be that either
> 'old' does not exist, or that you forgot to include a double backslash
> inside double quotes, e.g. "C:\temp1" vs "C:\\temp1".

FileTest.exist?(old) #> true

I'm using forward slashes, instead of escaped backslashes as that seems to
work well for the rest of the file stuff. (I've tried it with double
backslashes anyway, but it made no difference).

> You can avoid ftools or fileutils altogether and simply use File.rename.
> Just tested it on my Windows XP box and it works fine.

I actually tried File.rename(old, newdir) and it kind of worked if I did it
on its own. However, when I tried this:

File.open(file).readlines.each do | line |
#do stuff
end
File.rename(old, newdir)

I got permission denied. 'file', is located in 'old' so if it wasn't closed

then that would make sense. I initially read that File.open automatically
closed itself at the end, but re-reading I now see that's only if you give
it a block.

Doh! ... so finally, this worked:

f = File.open(file)
f.readlines.each do | line |
#do stuff
end
f.close
File.rename(olddir, newdir)



--------------------------------------------------------------------------------
The information contained herein is confidential and is intended solely for the
addressee. Access by any other party is unauthorised without the express
written permission of the sender. If you are not the intended recipient, please
contact the sender either via the company switchboard on +44 (0)20 7623 8000, or
via e-mail return. If you have received this e-mail in error or wish to read our
e-mail disclaimer statement and monitoring policy, please refer to
http://www.drkw.com/d... or contact the sender.
--------------------------------------------------------------------------------


1 Answer

ts

11/26/2003 6:04:00 PM

0

>>>>> "A" == Allton, Paul <Paul.Allton@DrKW.com> writes:

A> f = File.open(file)
A> f.readlines.each do | line |
A> #do stuff
A> end
A> f.close
A> File.rename(olddir, newdir)

IO::foreach(file) do |line|
# ...
end

The file is automatically closed at the end of the block


Guy Decoux