[英]How to delete the folder contents using Ruby?
假設我有一張Excel表格,其中有一些數字說:
現在我在Windows 7機器中有一個名為“D:// Filehosting”的目錄。 在該目錄下,我有大約500個文件夾,每個文件夾中有120個文件。 現在我想刪除每個文件夾的內容,這些內容距離當前日期還有2個月。 現在文件夾的排列如下:
腳本應該采用上面提到的數字,並相應地找到正確的目錄並相應地刪除內容。在內容刪除方法之前,必須檢查文件夾是否存在。
可以用Ruby完成嗎?
def del_dir id
Dir["D:/Filehosting/Document#{id}/*"].each do |fname|
next unless File.file?(fname) # skip accident dirs
if Time.now-File.mtime(fname) > 2*30*24*3600 # nearly 2 months
puts "[!] will delete #{fname}, mtime=#{File.mtime(fname)}"
# File.unlink(fname) # uncomment this to actually delete
end
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.