S
Spencer Rogers
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)
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)