System:
Windows Server 2008 Ruby 192
in 'delete': Permission denied Errno:EACCES
the line: File.delete('filename.ext')
If you know any other method to delete the file bypassing this error I'm glad to distribute you some points :) I mean any help would be appreciated ;-p
I was suspecting that file wasn't closed but it is closed.
Source code:
Dir.foreach(FileUtils.pwd()) do |f|
a[i] = f
if a[i].end_with?('log')
file = File.open(a[i])
if file.ctime < TIME_TO_REMOVE_LOGS || file.mtime < TIME_TO_REMOVE_LOGS || File.size(a[i]) > MAX_FILE_SIZE
puts a[i]
puts file.ctime
puts file.mtime
# zipping the file
orig = a[i]
Zlib::GzipWriter.open('arch_log.gz') do |gz|
gz.mtime = File.mtime(orig)
gz.orig_name = orig
gz.write IO.binread(orig)
end
file.close
end
end
File.delete(a[i])
i = i + 1
end
One is simply using the delete option, and the other one is deleting files permanently. When you can't delete a file normally, you can delete undeletable files Windows 10 by selecting the target file or folder and then press Shift + Delete keys on the keyboard for a try.
It's most likely because another program is currently trying to use the file. This can occur even if you don't see any programs running. When a file is open by another app or process, Windows 11/10 puts the file into a locked state, and you can't delete, modify, or move it to another location.
Use Shift + Delete to Force Delete File/Folder. You can select the target file or folder and press Shift + Delete keyboard shortcut to delete the file/folder permanently. This file deletion method won't pass the Recycle Bin.
It's not closed. The file only gets closed sometimes depending on ctime. There's a lot wrong with your code but the main point is this: be safe with your files and use a block.
File.open(a[i]) do |file|
# access file
end
# now you don't need to wonder if it's closed.
File.delete(a[i])
Also give your variables better names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With