File and Dir class problems

Discussion in 'Ruby' started by Spencer Rogers, Jan 8, 2010.

  1. I'm running into some problems when renaming and deleting files through
    ruby. Originally I built a function to rename certain directories.
    However when renaming a directory that has folders, files, and
    subfolders inside, I get the ENOTEMPTY "Directory Not Empty" error -
    even if the directory being renamed is the only directory inside the
    parent folder.

    In order to by-pass this problem I decided to program it manually using
    a recursive copy and then a recursive delete. However when I do the
    recursive delete, I get the same error for folders that have subfolders
    and files inside (and I am deleting all sub-folders/files first before
    deleting the parent folder, but the parent folder is the one throwing
    the error!).

    I don't get it. It almost seems as though the commands are executing
    too fast - before the file system has time to recognize that the folders
    are empty.

    This is driving me absolutely bonkers, so if there is anyone that has
    experienced the same thing or has some suggestions, I would greatly
    appreciate it!


    For reference, here is the code I used for the recursive delete:
    The "Me" folder contains several folders + subfolders and several files.
    So, for example:

    /Me/
    - /fullsize/
    - /folder2/
    - image.png
    - image 2.png
    - /scaled/
    - image3.png
    - simpsoneatsdonuts.png

    etc.



    path = "/Users/eagle/Desktop/Me/"

    files = Array.new
    everything = Array.new

    # get all the files
    files << Dir.glob(path + "**/**.*")
    # get all files/folders
    everything << Dir.glob(path + "**/**/")


    files.flatten!
    everything.flatten!

    # get the folders
    folders = everything - files
    # make sure sub-directories appear before parent directories
    folders.reverse!


    # loop through each file and delete
    files.each {|file|
    puts file
    File.delete(file)
    }

    p "-----------"

    # loop through each directory and delete
    folders.each {|f|
    puts f
    Dir.delete(f)
    }

    # now delete the base folder
    Dir.delete(path)
    --
    Posted via http://www.ruby-forum.com/.
    Spencer Rogers, Jan 8, 2010
    #1
    1. Advertising

  2. Spencer Rogers

    pharrington Guest

    On Jan 8, 4:25 pm, Spencer Rogers <> wrote:
    > I'm running into some problems when renaming and deleting files through
    > ruby.  Originally I built a function to rename certain directories.
    > However when renaming a directory that has folders, files, and
    > subfolders inside, I get the ENOTEMPTY "Directory Not Empty" error -
    > even if the directory being renamed is the only directory inside the
    > parent folder.
    >
    > In order to by-pass this problem I decided to program it manually using
    > a recursive copy and then a recursive delete.  However when I do the
    > recursive delete, I get the same error for folders that have subfolders
    > and files inside (and I am deleting all sub-folders/files first before
    > deleting the parent folder, but the parent folder is the one throwing
    > the error!).
    >
    > I don't get it.  It almost seems as though the commands are executing
    > too fast - before the file system has time to recognize that the folders
    > are empty.
    >
    > This is driving me absolutely bonkers, so if there is anyone that has
    > experienced the same thing or has some suggestions, I would greatly
    > appreciate it!
    >
    > For reference, here is the code I used for the recursive delete:
    > The "Me" folder contains several folders + subfolders and several files.
    > So, for example:
    >
    > /Me/
    > - /fullsize/
    >      - /folder2/
    >      - image.png
    >      - image 2.png
    > - /scaled/
    >      - image3.png
    >      - simpsoneatsdonuts.png
    >
    > etc.
    >
    > path = "/Users/eagle/Desktop/Me/"
    >
    > files = Array.new
    > everything = Array.new
    >
    > # get all the files
    > files << Dir.glob(path + "**/**.*")
    > # get all files/folders
    > everything << Dir.glob(path + "**/**/")
    >
    > files.flatten!
    > everything.flatten!
    >
    > # get the folders
    > folders = everything - files
    > # make sure sub-directories appear before parent directories
    > folders.reverse!
    >
    > # loop through each file and delete
    > files.each {|file|
    >   puts file
    >   File.delete(file)
    >   }
    >
    >   p "-----------"
    >
    > # loop through each directory and delete
    > folders.each {|f|
    >   puts f
    >   Dir.delete(f)
    >   }
    >
    > # now delete the base folder
    > Dir.delete(path)
    > --
    > Posted viahttp://www.ruby-forum.com/.


    You're thinking of FileUtils.rm_r http://ruby-doc.org/core/classes/FileUtils.html#M004336
    pharrington, Jan 8, 2010
    #2
    1. Advertising

  3. Spencer Rogers

    pharrington Guest

    On Jan 8, 4:25 pm, Spencer Rogers <> wrote:
    > I'm running into some problems when renaming and deleting files through
    > ruby.  Originally I built a function to rename certain directories.
    > However when renaming a directory that has folders, files, and
    > subfolders inside, I get the ENOTEMPTY "Directory Not Empty" error -
    > even if the directory being renamed is the only directory inside the
    > parent folder.
    >
    > In order to by-pass this problem I decided to program it manually using
    > a recursive copy and then a recursive delete.  However when I do the
    > recursive delete, I get the same error for folders that have subfolders
    > and files inside (and I am deleting all sub-folders/files first before
    > deleting the parent folder, but the parent folder is the one throwing
    > the error!).
    >
    > I don't get it.  It almost seems as though the commands are executing
    > too fast - before the file system has time to recognize that the folders
    > are empty.
    >
    > This is driving me absolutely bonkers, so if there is anyone that has
    > experienced the same thing or has some suggestions, I would greatly
    > appreciate it!
    >
    > For reference, here is the code I used for the recursive delete:
    > The "Me" folder contains several folders + subfolders and several files.
    > So, for example:
    >
    > /Me/
    > - /fullsize/
    >      - /folder2/
    >      - image.png
    >      - image 2.png
    > - /scaled/
    >      - image3.png
    >      - simpsoneatsdonuts.png
    >
    > etc.
    >
    > path = "/Users/eagle/Desktop/Me/"
    >
    > files = Array.new
    > everything = Array.new
    >
    > # get all the files
    > files << Dir.glob(path + "**/**.*")
    > # get all files/folders
    > everything << Dir.glob(path + "**/**/")
    >
    > files.flatten!
    > everything.flatten!
    >
    > # get the folders
    > folders = everything - files
    > # make sure sub-directories appear before parent directories
    > folders.reverse!
    >
    > # loop through each file and delete
    > files.each {|file|
    >   puts file
    >   File.delete(file)
    >   }
    >
    >   p "-----------"
    >
    > # loop through each directory and delete
    > folders.each {|f|
    >   puts f
    >   Dir.delete(f)
    >   }
    >
    > # now delete the base folder
    > Dir.delete(path)
    > --
    > Posted viahttp://www.ruby-forum.com/.


    Also, files << Dir.glob(path + "**/**.*") does not glob the list of
    files; it globs the list of filesystem entries that have a "."
    somewhere other than the beginning of the name. This is very different
    from the complete list of files, and nothing at all stops a directory
    name from containing a "."

    In a different scenario, when you actually do need to test if a given
    file name is a file or a directory, use File.file? or File.directory?

    stdlib is a prettttty nice thing to be acquainted with.
    pharrington, Jan 8, 2010
    #3
  4. Thanks to both of you, those are all good points. It looks like I will
    be able to do this without building my own function for it. And that was
    a good catch with the glob. If I wanted, how would I return a list of
    files going down to the nth sub-directory with glob?

    Also, I found out that some of the files were set as read-only which
    could be why the rename failed. I will give it another shot.
    --
    Posted via http://www.ruby-forum.com/.
    Spencer Rogers, Jan 8, 2010
    #4
  5. Hello Spencer,

    2010/1/8 Spencer Rogers <>:
    > Thanks to both of you, those are all good points. It looks like I will
    > be able to do this without building my own function for it. And that was
    > a good catch with the glob. =A0If I wanted, how would I return a list of
    > files going down to the nth sub-directory with glob?


    The problem with glob is that it doesn't by default show the so-called
    "hidden" files (which on Unix-like system have a name beginning with a
    dot). If you want absolutely all the files, you should use the
    'entries' method of a Dir object that get you the same result as a 'ls
    -a', but then you also get '.' (the current working directory) and
    '..' (the parent directory) which you usually would want to get rid
    of.

    >> d =3D Dir.new(Dir.pwd)

    =3D> #<Dir:/Users/jfleck/cours/sites/pcsi>
    >> d.entries

    =3D> [".", "..", ".DS_Store", "config.yaml", "MD5SUM", "MD5SUM_OLD",
    "metainfo.yaml", "mise_a_jour_site_web_mpsi.rb", "output", "README",
    "src", "truc"]

    Cheers,

    --=20
    JJ Fleck
    PCSI1 Lyc=E9e Kl=E9ber
    Fleck Jean-Julien, Jan 9, 2010
    #5
    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. =?Utf-8?B?UnVkeQ==?=

    Sub Dir, Virtual dir, what do I use?

    =?Utf-8?B?UnVkeQ==?=, Jun 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    422
    =?Utf-8?B?UnVkeQ==?=
    Jun 12, 2005
  2. Matthew Denner
    Replies:
    1
    Views:
    181
  3. Kga Agk
    Replies:
    2
    Views:
    147
    Kga Agk
    Jun 29, 2009
  4. Nick Gnedin
    Replies:
    2
    Views:
    160
  5. MRAB
    Replies:
    0
    Views:
    119
Loading...

Share This Page