Search code examples
rubywindowsfilerakedelete-file

Ruby can not delete file after cp on windows


I am using rake to copy files I receive from one folder to two sub-folders.

After the copy I try to delete these files from their original folder.

I can't seem to delete some of the files (usually only 1 or 2 out of 5 or so).

When using mv (and not CLEAN) I receive an error message of access denied.

I believe that Windows (my OS) still holds a reference to the file/s and therefore won't allow me to delete them.

I can delete the files out of code no problem. There should not be a permissions issue.

If my theory is correct that there is still a reference open to the file, then how could i close those references?

Could it be something else?

The code:

DOCK      = '/path' 
NEW_FILES = DOCK + '/NewFiles'

dock_stock  = FileList.new(DOCK + '/*.xml')

file target_path do |t|
  unless dock_stock.empty?
    mkdir t.name
    dock_stock.each do |f|
      target_new_files_folder = f.pathmap(NEW_FILES + '/%f') 

      mv f, target_new_files_folder
    end
  end 
end

Also it should be noted this task is a dependency to a multitask(really a dependency of a dependency of a dependency). task :clean => target_path being one of them.

So maybe the issue is multi thread related or the :clean task.


Solution

  • It seems there was a McAfee Agent that was referencing the files not allowing me to delete them.

    For the most part, if I tried to delete them later on in time they would delete.

    When I write for the most part I mean once in a blue moon it could hold the files for days until released manually.

    The workaround would be to record the problematic files and exclude them from the file list and try to erase them at a later point.

    Wish I had a better solution.