mayooran mayooran - 1 year ago 149
Ruby Question

Removing file using name contains in Ruby

I have written the below code to keep a file that matches the passed in name and removes the rest of the files in the folder.

def removeFiles2(path, namePart)
aft_logger=AFTLogger.new

Dir[path+'/*'].each do |fname|
break if !(fname.include? namePart)
else File.delete(fname)
end
end
end
end


But I am getting the below error.

Operation not permitted - Operation not permitted - //CSLK-CISP-81-01/jboss/server/TeamCI_CEPHEUS_OR_R81_Spider_110/home/RoSModule/XML/In/Error:
Operation not permitted - Operation not permitted - //CSLK-CISP-81-
01/jboss/server/TeamCI_CEPHEUS_OR_R81_Spider_110/home/RoSModule/XML/In/Error tempDevMode.rb line 37 in function test


What am I doing wrong here? Any help would be much appreciated.

Answer Source

Below code should work. just try it out

def removeFiles(path, namePart)

Dir[path+'/*'].each do |fname|
    if fname.include? namePart
     //just print something you want

    else  
      File.delete(fname)
  end
 end
end
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download