2

I modify a file and then try to rename it and rename his directory. But I get a permission denied error.

Here is my code :

# 1) Modify the xml file

Xml_file = "Directory/foo.xml"

begin
    contents = File.new(Xml_file).read
    $document = REXML::Document.new(contents)
    $document.root.elements["label"].text = "some text"  

    File.open(Xml_file, "w") do |data|
    data<<$document
end

ensure
    $document = nil
end

# 2) Then rename the file and the directory

old_name = Xml_file
new_name = "Directory/new_name.xml"
File.rename(old_name, new_name) # OK, this works !

old_dir = "Directory"
new_dir = "New_Directory"
FileUtils.mv(old_dir, new_dir) # Crash, saying "permission denied"

1 Answers1

1

As it clearly stated in an example in the documentation, one should add

force: true

option to FileUtils.mv in case when target directory does not exist.

Aleksei Matiushkin
  • 105,980
  • 9
  • 87
  • 132