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

P

Peter Bailey

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
 
N

Nobuyoshi Nakada

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
 
D

Daniel Berger

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
 
P

Peter Bailey

Nobuyoshi said:
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
 
7

7stud --

Peter said:
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:

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:

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?
 
P

Peter Bailey

This line:
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:


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

Sebastian Hungerecker

Peter said:
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?
 
P

Peter Bailey

Jay said:
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
 
P

Phlip

Jay said:
In Ruby, though, the object gets destroyed by garbage collection, not by
explicit destruction or stack unwinding. So RAII wouldn't work; you can
never guarantee when (or if) the resource will be closed. Instead,
resource-wrappers like File often let you specify a block; the wrapper
opens the resource, yields to the block, and closes the resource for you.
It's just a different wrapping idiom. If you haven't discovered
http://gotapi.com already, load up the Ruby tab and check out IO#open.

A FAQ on the C++ mailing list is "shouldn't C++ learn from Java and add a
'finally' keyword"?

Block closures teach us that both RAII and 'finally' are hacks, and the
Execute Around Pattern is the Real Deal.
 
7

7stud --

Peter said:
Now, I have to admit, I really need to look at your code, Jay, because,
to me, it's like a different "dialect."
...
But, your
casual use of a black and opening a file here kind of blows me away,
actually.

1) First attempt at writing the code:

f = File.new(file)
line1 = f.gets
f.close

if /%!PS-Adobe/ =~ line1
File.rename(file, "#{file}.eps")

2) Hmmm...I think I can make those first three lines shorter. If I use
a File.open block that I read about in pickaxe, I won't have to call
close():

line1 = nil
File.open {|file| line1 = f.gets}

if /%!PS-Adobe/ =~ line1
File.rename(file, "#{file}.eps")

3) And, shorter still...:

line1 = File.open {|file| f.gets}

if /%!PS-Adobe/ =~ line1
File.rename(file, "#{file}.eps")


4) Hey, but look at 3). line1 appears on each line. So I can take what
line1 is equal to on the first line and substitute it's value in the
second line:

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

Peter Bailey

In Ruby, though, the object gets destroyed by garbage collection, not by
explicit destruction or stack unwinding. So RAII wouldn't work; you can
never guarantee when (or if) the resource will be closed. Instead,
resource-wrappers like File often let you specify a block; the wrapper
opens the resource, yields to the block, and closes the resource for
you.
It's just a different wrapping idiom. If you haven't discovered
http://gotapi.com already, load up the Ruby tab and check out IO#open.


* Wasn't RAII originally called RAIN, Resource Acquisition Is
iNitialization? I could swear it was, and it certainly seems a more
memorable choice for an acronym, but Google's not bearing me out on
this.
Then again, that was pre-Google. Anyone?

Well, I'd love to be able to give advice eventually in this forum, to
someone even newbier than me. It's such a generous forum that I feel I
have to give back in some way. . . .
 
S

Simon Krahnke

* 7stud -- said:
if /%!PS-Adobe/ =~ File.open {|file| f.gets}
File.rename(file, "#{file}.eps")

This is not C or Python, Ruby needs an end.

Or a one-liner:

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

mfg, simon .... l
 
W

William James

This is not C or Python, Ruby needs an end.

Or a one-liner:

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

I think that should be

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

More terse:

File.rename(file, "#{file}.eps") if /%!PS-Adobe/ =~ IO.read(file,10)
 
S

Simon Krahnke

* William James said:
I think that should be

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

Yeah, I correct one error and repeat another.
More terse:

File.rename(file, "#{file}.eps") if /%!PS-Adobe/ =~ IO.read(file,10)

That's a violation of the DRY principle.

Define PS = '%!PS-Adobe' then

| File.rename(file, "#{file}.eps") if IO.read(file, PS.length)[PS]

mfg, simon .... l
 
W

William James

I think that should be
File.rename(file, "#{file}.eps") if /%!PS-Adobe/ =~ File.open(file){|
f| f.gets}

Yeah, I correct one error and repeat another.
More terse:
File.rename(file, "#{file}.eps") if /%!PS-Adobe/ =~ IO.read(file,10)

That's a violation of the DRY principle.

Define PS = '%!PS-Adobe' then

| File.rename(file, "#{file}.eps") if IO.read(file, PS.length)[PS]

That's a violation of common sense. '%!PS-Adobe' isn't going
to change, and therefore 10 won't need changing. Unjustified
code inflation and a superfluous temporary variable.
Furthermore, why say ".length" when you can say ".size"?
You have a perverse, pronounced propensity for prolixity.
 
D

David A. Black

Hi --

Furthermore, why say ".length" when you can say ".size"?

For some reasons, I like #length for strings and #size for arrays.
I'm not worried about the two extra characters. I guess I'm profligate
with electrons :)


David

--
Upcoming training from Ruby Power and Light, LLC:
* Intro to Ruby on Rails, Edison, NJ, October 23-26
* Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!
 
D

Daniel Berger

Jay said:
But magic numbers suck.

That would only be true if vendors randomly and unexpectedly changed
them. From what I can gather (from Wikipedia and general experience) as
formats change vendors may extra add data to the end of the magic number
(or create a new one), but always leave the 'base' magic number alone.

Personally, I think you should stop worrying and learn to love the magic
number. :)

Regards,

Dan
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top