簡體   English   中英

如何使用Ruby刪除文件夾內容?

[英]How to delete the folder contents using Ruby?

假設我有一張Excel表格,其中有一些數字說:

  • 77787
  • 45877
  • 78985等......

現在我在Windows 7機器中有一個名為“D:// Filehosting”的目錄。 在該目錄下,我有大約500個文件夾,每個文件夾中有120個文件。 現在我想刪除每個文件夾的內容,這些內容距離當前日期還有2個月。 現在文件夾的排列如下:

  • d:// Filehosting / Document77787
  • D:// Filehosting / Document45877 ..等等

腳本應該采用上面提到的數字,並相應地找到正確的目錄並相應地刪除內容。在內容刪除方法之前,必須檢查文件夾是否存在。

可以用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM