假设我有一个 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
关于ruby - 如何使用 Ruby 删除文件夹内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14253814/