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

Discussion in 'Ruby' started by Peter Bailey, Oct 7, 2007.

  1. Peter Bailey

    Peter Bailey Guest

    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-forum.com/.
    Peter Bailey, Oct 7, 2007
    #1
    1. Advertising

  2. 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
    Nobuyoshi Nakada, Oct 7, 2007
    #2
    1. Advertising

  3. On Oct 7, 12:07 am, 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


    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
    Daniel Berger, Oct 7, 2007
    #3
  4. Daniel Berger wrote:
    > On Oct 7, 12:07 am, Nobuyoshi Nakada <> 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:
    ICQ: 205544826
    Sebastian Hungerecker, Oct 7, 2007
    #4
  5. On Oct 7, 7:30 am, Sebastian Hungerecker <>
    wrote:
    > Daniel Berger wrote:
    > > On Oct 7, 12:07 am, Nobuyoshi Nakada <> 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
    Daniel Berger, Oct 7, 2007
    #5
  6. Peter Bailey

    Peter Bailey Guest

    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-forum.com/.
    Peter Bailey, Oct 7, 2007
    #6
  7. Peter Bailey

    7stud -- Guest

    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-forum.com/.
    7stud --, Oct 7, 2007
    #7
  8. Peter Bailey

    Peter Bailey Guest

    > 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-forum.com/.
    Peter Bailey, Oct 7, 2007
    #8
  9. 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:
    ICQ: 205544826
    Sebastian Hungerecker, Oct 7, 2007
    #9
  10. Peter Bailey

    Peter Bailey Guest

    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-forum.com/.
    Peter Bailey, Oct 7, 2007
    #10
  11. Peter Bailey

    Peter Bailey Guest

    Peter Bailey, Oct 7, 2007
    #11
  12. Peter Bailey

    Phlip Guest

    Jay Levitt wrote:

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

    --
    Phlip
    Phlip, Oct 8, 2007
    #12
  13. Peter Bailey

    7stud -- Guest

    Peter Bailey wrote:
    >
    > 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")
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Oct 8, 2007
    #13
  14. Peter Bailey

    Peter Bailey Guest

    > 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. . . .
    --
    Posted via http://www.ruby-forum.com/.
    Peter Bailey, Oct 8, 2007
    #14
  15. * 7stud -- <> (01:57) schrieb:

    > 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
    Simon Krahnke, Oct 8, 2007
    #15
  16. On Oct 8, 12:09 pm, Simon Krahnke <> wrote:
    > * 7stud -- <> (01:57) schrieb:
    >
    > > 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}


    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)
    William James, Oct 8, 2007
    #16
  17. * William James <> (22:01) schrieb:

    >>| 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}


    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
    Simon Krahnke, Oct 8, 2007
    #17
  18. On Oct 8, 5:17 pm, Simon Krahnke <> wrote:
    > * William James <> (22:01) schrieb:
    >
    > >>| 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}

    >
    > 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.
    William James, Oct 9, 2007
    #18
  19. Hi --

    On Tue, 9 Oct 2007, William James wrote:

    > 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!
    David A. Black, Oct 9, 2007
    #19
  20. Jay Levitt wrote:
    > On Mon, 08 Oct 2007 16:56:11 -0700, William James wrote:
    >
    >> That's a violation of common sense. '%!PS-Adobe' isn't going
    >> to change, and therefore 10 won't need changing.

    >
    > 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
    Daniel Berger, Oct 9, 2007
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Meyer
    Replies:
    5
    Views:
    2,447
    =?iso-8859-1?q?J=E9r=F4me_Laheurte?=
    Dec 3, 2005
  2. Eric S. Johansson
    Replies:
    0
    Views:
    240
    Eric S. Johansson
    Jun 10, 2007
  3. Mmcolli00 Mom
    Replies:
    1
    Views:
    166
    Reid Thompson
    Dec 12, 2008
  4. Marek Stepanek

    [newbie] rename not working

    Marek Stepanek, Jan 25, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    110
    Marek Stepanek
    Jan 26, 2005
  5. Tom Bates
    Replies:
    5
    Views:
    193
    Chris Mattern
    Apr 25, 2005
Loading...

Share This Page