- 953 名前:デフォルトの名無しさん mailto:sage [2020/07/09(木) 04:53:54.78 ID:gEWbrPAM.net]
- Ruby で作った。
ただし、DryRun なので実際には実行されません! require 'fileutils' # 絶対パスのディレクトリ名の後ろに、* を付けること! # . で始まる、隠し directory, file を除く glob_pattern = "C:/Users/Owner/Documents/*.txt" num = 5 # 更新時刻の新しいものからの順で、フォルダに残しておくファイルの数 Dir.glob( glob_pattern ) .select { |full_path| File.file?( full_path ) } # ファイルのみ処理する .sort_by { |full_path| File.mtime( full_path ) } # 更新時刻の古い順で、ソートする .reverse # 更新時刻の新しい順へ、逆転 .drop( num ) # 新しいものから、num個を処理しない .each { |full_path| FileUtils::DryRun.rm( full_path ) } # ファイルを削除 表示 rm C:/Users/Owner/Documents/a.txt rm C:/Users/Owner/Documents/b.txt
|

|