Advertisement
Guest User

Untitled

a guest
Mar 15th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.97 KB | None | 0 0
  1. #!/usr/bin/env rdmd
  2. import std.stdio;
  3. import std.getopt;
  4. import std.file;
  5. import std.array;
  6. import std.algorithm.iteration;
  7. import std.algorithm.sorting;
  8.  
  9. string folder; //путь к папке
  10. uint limit; //предел
  11.  
  12. int main(string[] args)
  13. {
  14.     auto helpInfo = getopt(
  15.         args,
  16.         "f|folder", "Folder path", &folder,
  17.         "l|limit", "Limit in megabytes", &limit
  18.     );
  19.  
  20.     if (!limit || !folder || helpInfo.helpWanted) {
  21.         defaultGetoptPrinter("Removes old files while space limit is exceeded.\nParams:", helpInfo.options);
  22.         return 0;
  23.     }
  24.  
  25.     DirEntry[] entries = dirEntries(folder, SpanMode.shallow).filter!(f => f.isFile).array;
  26.     entries.sort!((x,y) => x.timeLastModified < y.timeLastModified);
  27.  
  28.     ulong total = entries.map!(f => f.size).sum;
  29.    
  30.     while (total > limit*1024*1024) {
  31.         total -= entries[0].size;
  32.         remove(entries[0].name);
  33.         entries = entries[1..$];
  34.     }
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement