[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

The old File.rename not working again. . . .

Peter Bailey

10/7/2007 2:15:00 AM

This always throws me. I keep getting "permission denied" messages when
I simply try to rename files. Here's my code. The files in the directory
start out without an extension.

Dir.chdir("F:/images")
Dir.glob("*").each do |file|
File.new(file, "r").gets
if $_ =~ /%!PS-Adobe/
newfile = file + ".eps"
File.rename(file, newfile)
end
end

Thanks,
Peter
--
Posted via http://www.ruby-....

24 Answers

Nobuyoshi Nakada

10/7/2007 6:07:00 AM

0

Hi,

At Sun, 7 Oct 2007 11:15:04 +0900,
Peter Bailey wrote in [ruby-talk:272952]:
> This always throws me. I keep getting "permission denied" messages when
> I simply try to rename files. Here's my code. The files in the directory
> start out without an extension.
>
> Dir.chdir("F:/images")

Maybe on Windows?

> Dir.glob("*").each do |file|
> File.new(file, "r").gets

This leaves the file still opened, and on Windows, it's
prohibited by the OS to move/rename files while they are
opened.

if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}
File.rename(file, file + ".eps")
end

--
Nobu Nakada

Daniel Berger

10/7/2007 1:23:00 PM

0



On Oct 7, 12:07 am, Nobuyoshi Nakada <n...@ruby-lang.org> wrote:
> Hi,
>
> At Sun, 7 Oct 2007 11:15:04 +0900,
> Peter Bailey wrote in [ruby-talk:272952]:
>
> > This always throws me. I keep getting "permission denied" messages when
> > I simply try to rename files. Here's my code. The files in the directory
> > start out without an extension.
>
> > Dir.chdir("F:/images")
>
> Maybe on Windows?
>
> > Dir.glob("*").each do |file|
> > File.new(file, "r").gets
>
> This leaves the file still opened, and on Windows, it's
> prohibited by the OS to move/rename files while they are
> opened.
>
> if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}
> File.rename(file, file + ".eps")
> end

Why would he need to open the file at all?

Dir.chdir("F:/images")
Dir.glob("*").each do |file|
if file =~ /%!PS-Adobe/
newfile = file + ".eps"
File.rename(file, newfile)
end
end

Regards,

Dan


Sebastian Hungerecker

10/7/2007 1:30:00 PM

0

Daniel Berger wrote:
> On Oct 7, 12:07 am, Nobuyoshi Nakada <n...@ruby-lang.org> wrote:

> > if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}
> > File.rename(file, file + ".eps")
> > end
>
> Why would he need to open the file at all?

Because he's checking the first line of the file, not the filename.


--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Daniel Berger

10/7/2007 1:45:00 PM

0



On Oct 7, 7:30 am, Sebastian Hungerecker <sep...@googlemail.com>
wrote:
> Daniel Berger wrote:
> > On Oct 7, 12:07 am, Nobuyoshi Nakada <n...@ruby-lang.org> wrote:
> > > if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}
> > > File.rename(file, file + ".eps")
> > > end
>
> > Why would he need to open the file at all?
>
> Because he's checking the first line of the file, not the filename.

Oops, my mistake.

Regards,

Dan


Peter Bailey

10/7/2007 1:57:00 PM

0

Nobuyoshi Nakada wrote:
> Hi,
>
> At Sun, 7 Oct 2007 11:15:04 +0900,
> Peter Bailey wrote in [ruby-talk:272952]:
>> This always throws me. I keep getting "permission denied" messages when
>> I simply try to rename files. Here's my code. The files in the directory
>> start out without an extension.
>>
>> Dir.chdir("F:/images")
>
> Maybe on Windows?
>
>> Dir.glob("*").each do |file|
>> File.new(file, "r").gets
>
> This leaves the file still opened, and on Windows, it's
> prohibited by the OS to move/rename files while they are
> opened.
>
> if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}
> File.rename(file, file + ".eps")
> end

Yes, I'm on Windows. And, yes, it makes sense that it won't let me do it
because the file is, apparently, still open. But, I guess I'm stumped as
to how I can open the file to check that first line, match against that
first line, close the file, and then be able to rename the filename of
that particular file. Once the file is closed I really can't do anything
more inside that same if-end loop, can I?

-Peter

--
Posted via http://www.ruby-....

7stud 7stud

10/7/2007 2:58:00 PM

0

Peter Bailey wrote:
> Nobuyoshi Nakada wrote:
>>
>> if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}
>> File.rename(file, file + ".eps")
>> end
>
> Yes, I'm on Windows. And, yes, it makes sense that it won't let me do it
> because the file is, apparently, still open. But, I guess I'm stumped as
> to how I can open the file to check that first line, match against that
> first line, close the file, and then be able to rename the filename of
> that particular file. Once the file is closed I really can't do anything
> more inside that same if-end loop, can I?
>
> -Peter


This line:

>> if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}

opens the file, gets the first line, closes the file, and checks the
line against the regex for a match. If there is a match, this code
executes:

>> File.rename(file, file + ".eps")
>> end

That line renames the file.

> But, I guess I'm stumped as
> to how I can open the file to check that first line, match against that
> first line, close the file, and then be able to rename the filename of
> that particular file. Once the file is closed I really can't do anything
> more inside that same if-end loop, can I?

What more do you want to do?

--
Posted via http://www.ruby-....

Peter Bailey

10/7/2007 3:12:00 PM

0

> This line:
>
>>> if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}
>
> opens the file, gets the first line, closes the file, and checks the
> line against the regex for a match. If there is a match, this code
> executes:
>
>>> File.rename(file, file + ".eps")
>>> end
>
> That line renames the file.

> What more do you want to do?

I want to rename the file(s), and, that's what's not working. I'm
getting file permission errors.
--
Posted via http://www.ruby-....

Sebastian Hungerecker

10/7/2007 4:19:00 PM

0

Peter Bailey wrote:
> > This line:
> >>> if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}
> >
> > opens the file, gets the first line, closes the file, and checks the
> > line against the regex for a match. If there is a match, this code
> >
> > executes:
> >>> File.rename(file, file + ".eps")
> >>> end
> >
> > That line renames the file.
> >
> > What more do you want to do?
>
> I want to rename the file(s), and, that's what's not working. I'm
> getting file permission errors.

The code posted above gives you permission errors? Or your original code gives
you permission errors? Because the code posted above really shouldn't. If the
code above gives you permission error, it's not because of open file handles.
Maybe you don't have permission to change the contents of the directory you
are working on?


--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Jay Levitt

10/7/2007 5:06:00 PM

0

On Mon, 8 Oct 2007 01:19:01 +0900, Sebastian Hungerecker wrote:

> The code posted above gives you permission errors? Or your original code gives
> you permission errors? Because the code posted above really shouldn't. If the
> code above gives you permission error, it's not because of open file handles.

Actually, the code above should give him

TypeError: can't convert String into Integer

because IO.open wants an integer file descriptor :) He wants Kernel.open.

The working code is:

Dir.glob("*").each do |file|
if /%!PS-Adobe/ =~ open(file) {|f| f.gets}
File.rename(file, file + ".eps")
end
end

Peter, the key to Nobuyoshi's change above is that the rename now happens
*outside* the Kernel.open block. It doesn't matter that it happens inside
the if/end block; that's not what's holding the file open - Kernel.open is.
Now, the only thing that's done in the block is the gets, and the block is
closed on the very same line, before the rename happens.

--
Jay Levitt |
Boston, MA | My character doesn't like it when they
Faster: jay at jay dot fm | cry or shout or hit.
http://... | - Kristoffer

Peter Bailey

10/7/2007 10:41:00 PM

0

Jay Levitt wrote:
> On Mon, 8 Oct 2007 01:19:01 +0900, Sebastian Hungerecker wrote:
>
>> The code posted above gives you permission errors? Or your original code gives
>> you permission errors? Because the code posted above really shouldn't. If the
>> code above gives you permission error, it's not because of open file handles.
>
> Actually, the code above should give him
>
> TypeError: can't convert String into Integer
>
> because IO.open wants an integer file descriptor :) He wants
> Kernel.open.
>
> The working code is:
>
> Dir.glob("*").each do |file|
> if /%!PS-Adobe/ =~ open(file) {|f| f.gets}
> File.rename(file, file + ".eps")
> end
> end
>
> Peter, the key to Nobuyoshi's change above is that the rename now
> happens
> *outside* the Kernel.open block. It doesn't matter that it happens
> inside
> the if/end block; that's not what's holding the file open - Kernel.open
> is.
> Now, the only thing that's done in the block is the gets, and the block
> is
> closed on the very same line, before the rename happens.

Yes, Jay, that worked! And, I beg your pardon, Nobuyoshi, because, I
rather glibly just glanced over your code thinking you were just
repeating my code.

Now, I have to admit, I really need to look at your code, Jay, because,
to me, it's like a different "dialect." My primitive Ruby experience is
showing, I guess. I've certainly worked with blocks before, and I've
certainly worked with opening files before. And, I thought I was being
clever here by using "gets" for the first time, knowing that I only
wanted to read that first line. Previously, I've used File.read a lot,
but, I actually did want to read in the whole file then. But, your
casual use of a black and opening a file here kind of blows me away,
actually.

Thank you everyone.

Cheers,
Peter
--
Posted via http://www.ruby-....