1

I need to delete the directory after its creation but I am getting an error because the directory is still in use by ruby.exe or another ruby process is there any way that I can close the directory like closing the file so that it will delete after its creation. When i reload the page and then try to remove the directory then the directory successfully delete.

Here is the code which i am trying

if Dir.exists?("first/test")
     FileUtils.rm_rf("first/test")
     FileUtils.mkdir("first/test")
else
     Dir.mkdir("first/test")
end

Test folder does contain sub directories and files.

john
  • 611
  • 1
  • 7
  • 33
  • 1
    Share the code you've tried... – Uzbekjon Mar 17 '17 at 14:06
  • You probably need to close this directory resource before removing it. But it will be much better if you could show us your code and then we would help you. – Ed de Almeida Mar 17 '17 at 14:24
  • @EddeAlmeida exactly thats what i want that how to close the directory resource?. I have found the way to close the file but not found any way to close the directory.Is there any way so that we can close our directory resource? – john Mar 17 '17 at 14:42
  • @EddeAlmeida. Please see my question. i have updated the question with the code. – john Mar 17 '17 at 15:09
  • @Uzbekjon I have updated my question . Kindly have a look.thanks – john Mar 17 '17 at 15:11
  • Are you having an error when you do `FileUtils.rm_rf("first/test")`, right? What is the error exactly? – Ed de Almeida Mar 17 '17 at 15:27
  • yes, right. it is not giving error rm_rf. It doesn't delete the folder and give me file exist error on mkdir. I want to let you know that when i delete the test folder from windows by shift+delete. It give me error that an file is in use by ruby.exe. Also my front end is in angular. When i refresh the page. every thing works fine. – john Mar 17 '17 at 15:37
  • @EddeAlmeida any help? – john Mar 17 '17 at 17:52
  • Sorry, I was busy. I'll try to reproduce it now. – Ed de Almeida Mar 17 '17 at 18:33
  • Have you seen my answer? – Ed de Almeida Mar 19 '17 at 08:05
  • @EddeAlmeida Hi, thanks for you kind help. I have further dig in to the problem and here is the problem that I have found Kindly have a look at this link http://stackoverflow.com/questions/42866356/resource-lock-while-generating-the-zip-using-zipfilegenerator-class-ruby – john Mar 20 '17 at 13:16

2 Answers2

2

The stream was not closing after writing files in rubyzip class I have modified the code in rubyzip class like this

disk_file = File.open(diskFilePath, "rb")
io.get_output_stream(zipFilePath) { |f|
  f.puts(disk_file.read())
}
disk_file.close
Ed de Almeida
  • 3,675
  • 4
  • 25
  • 57
john
  • 611
  • 1
  • 7
  • 33
1

There are two main problems with your code, I think:

  • According to Ruby documentation, Dir.exists? is deprecated and should not be used. Use Dir.exist? (without the 's') instead;

  • You are trying to create a directory structure with FileUtils.mkdir or Dir.mkdir when, in fact, you need a 'stronger' method: FileUtils.mkdir_p.

Try this:

if Dir.exist?("first/test")
  FileUtils.rm_rf("first/test")
  FileUtils.mkdir_p("first/test")
else
  FileUtils.mkdir_p("first/test")
end

And see the corresponding documentation.

I believe that doing

FileUtils.mkdir("first")
FileUtils.mkdir("first/test")

would work fine, although I haven't tested it, because the second dir ('test') would be create inside an existing one. But if you need to create a whole structure in a single command you'd need the -p flag using a bash command and the corresponding FileUtils.mkdir_p method.

Let me also point you that this if..else structure is not good this way. You want to create the directory structure in both if and else, and if the same command appears in both if and else, it must be taken out of the if..else, like this.

if Dir.exist?("first/test")
  FileUtils.rm_rf("first/test")     
end
FileUtils.mkdir_p("first/test")

I hope this helps.

Ed de Almeida
  • 3,675
  • 4
  • 25
  • 57