Forum: Ruby removing directories

Posted by Peter Bailey (peterbailey)
on 2012-11-29 14:39
Hello,
I've googled around and I've discovered that "FileUtils.rm_rf('#{dir}')
will remove a directory. But, I can't get it to work. I need to go into
a created subdirectory, move the files in there to the parent directory,
go back into the parent directory and remove the subdirectory. This is
all part of a graphic workflow I'm working on.

There is only one subdirectory created here, no more.

Dir.chdir("T:/asura/ads_books/out/tiff")
list = Dir.entries('.')
list.delete(".")
list.delete("..")

list.each do |dir|
  Dir.chdir("T:/asura/ads_books/out/tiff/#{dir}")
  Dir.glob("*.tiff").each do |tifffile|
    FileUtils.mv(tifffile, "T:/asura/ads_books/out/tiff")
  end
  Dir.chdir("T:/asura/ads_books/out/tiff")
  FileUtils.rm_rf('#{dir}')
end

Thank you,
Peter
Posted by Carlo E. Prelz (Guest)
on 2012-11-29 14:55
(Received via mailing list)
Subject: removing directories
  Date: Thu 29 Nov 12 10:39:52PM +0900

Quoting Peter Bailey (lists@ruby-forum.com):

>   FileUtils.rm_rf('#{dir}')

When strings are included in single quotes, #{} expansion does not
take place. Try with

FileUtils.rm_rf("#{dir}")

(even if here, you already have a string, so

FileUtils.rm_rf(dir)

should equally do the trick)

Carlo
Posted by Peter Bailey (peterbailey)
on 2012-11-29 17:49
Thanks, Carlo. I actually figured this out myself, googling around. I 
used double quotes instead of singles, and, I put the full path in there 
in front of my directory variable.

Thanks again,
Peter
Posted by Alex Mcmillan (me-wrong)
on 2012-11-30 11:51
(Received via mailing list)
Take a look at a lesson on oldkingjames.org FilUtils is used to delete 
directory even with files inside.
Posted by Alex Mcmillan (me-wrong)
on 2012-11-30 17:44
(Received via mailing list)
require 'fileUtils'
# And now the code to delete the directory tree.

FileUtils.remove_dir("C:/Users/your/filepath/to_directory")

To can read this lesson 27 @
http://www.oldkingjames.org/RUBY_LESSONS/ruby_less...
Posted by Peter Bailey (peterbailey)
on 2012-11-30 18:33
Alex Mcmillan wrote in post #1087317:
> require 'fileUtils'
> # And now the code to delete the directory tree.
>
> FileUtils.remove_dir("C:/Users/your/filepath/to_directory")
>
> To can read this lesson 27 @
> http://www.oldkingjames.org/RUBY_LESSONS/ruby_less...

Beautiful! Thank you.
Posted by Peter Bailey (peterbailey)
on 2012-11-30 18:34
Great stuff. Thank you very much.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.