Advertisement
Kulverstukas

CountMyFiles

Aug 7th, 2010
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.96 KB | None | 0 0
  1. { Written by Kulverstukas on 2010.04.05 for EZ community - evilzone.org || last update: 2010.04.19 }
  2. program CountMyFiles;
  3.  uses Dos, SysUtils, Crt;
  4.  var SRecord : TSearchRec;
  5.      EnvVariable : string;
  6.      TotalSize : longint;
  7.      path, extension : string;
  8.  
  9. //=======================================
  10. {
  11.   This program searches for files with a given extension in a given directory and outputs file name and size.
  12.   WildCard is supported. All folders and files are visible to this - even those set with +s attribute.
  13. }
  14. {$R-}  // turns Array Range checking off. If turned on - it gives "Range overrun" because file sizes are too big to fit into a fixed array
  15. //=======================================
  16. Procedure Search(path, extension : string); // procedure with 2 calls
  17. begin
  18.   path := SysUtils.IncludeTrailingBackSlash(path); // This checks whether there is a trailing delimiter for given path at the end or not ("\" for Windows; "/" for UNIX).
  19.   //================  // check if entered extension has a dot. If it has then skip it, if not then add it along with extension
  20.    if (Pos('.',extension) = 0) then // Pos(char,string) searches in "string" for "char". If "char" is not found in "string" then 0 is returned
  21.     begin
  22.  EnvVariable := path+'*.'+extension;
  23.     end
  24.    else
  25.     begin
  26.  EnvVariable := path+'*'+extension;
  27.     end;
  28.   //================
  29.   FindFirst(EnvVariable,AnyFile,SRecord);  // EnvVariable - path with extension, AnyFile - attribute, SRecord - found files;
  30.     ClrScr;
  31.    TextColor(Green);
  32.     WriteLn('Filename'+Space(52),'FileSize'); // Write FileName and FileSize in one line with 22 spaces between
  33.    TextColor(White);
  34.     WriteLn('=============================================================================');
  35.   //================ // if SRecord.Size is 0 and file name doesn't have "." then it's a dir
  36.    Repeat // search for files until no match. If no match ir found then FindNext(); returns something else then 0
  37.    begin
  38.      if (SRecord.Size = 0) and (Pos('.exe',SRecord.Name) = 0) then  // Pos(char,string) searches in "string" for "char". If "char" is not found in "string" then 0 is returned
  39.       begin
  40.     WriteLn(SRecord.Name+Space(59-Length(SRecord.Name)),'...is a Dir')
  41.       end
  42.   //================ // if SRecord.Size is bigger then 1073741824 bytes (1 GB) then say it's over a Gigabyte
  43.      else
  44.       begin
  45.        if (SRecord.Size >= 1073741824) then
  46.         begin
  47.     WriteLn(SRecord.Name+Space(59-Length(SRecord.Name)),'over a Gigabyte');
  48.         end
  49.   //================
  50.        else
  51.         begin
  52.     WriteLn(SRecord.Name+Space(59-Length(SRecord.Name)),SRecord.Size:9,' Bytes');
  53.         end;
  54.       end;
  55.   //================
  56. {
  57.   This sums all files together. Files bigger then 900 MB are not included as it causes runtime error (215)
  58.   for some unknown reason. Might be because it overflows the array or something:
  59.      [215] Arithmetic overflow error - This error is reported when the result of an arithmetic operation is
  60.      outside of its supported range.
  61. }
  62.     TotalSize := TotalSize + SRecord.Size;
  63.       FindNext(SRecord);
  64.      if (TotalSize > 943718400) then
  65.       begin
  66.        TotalSize := 0;
  67.       end;
  68.    end;
  69.    Until FindNext(SRecord) <> 0;
  70.     WriteLn('=============================================================================');
  71.     WriteLn('Total Size: ',TotalSize,' Bytes');
  72. end;
  73. //=======================================
  74. begin
  75.   ClrScr;
  76.  //================== // user instructions starts from here
  77.    GotoXY(6,10);
  78.     Writeln('Input ''*'' for extension to search for everything in the given path.');
  79.    GotoXY(6,11);
  80.     Write('Don''t input anything for extension to search for folders only.');
  81.    GotoXY(6,12);
  82.     Write('Run this program from CMD to use copy/paste.');
  83.  //==================
  84.    GotoXY(1,2);
  85.   Write('Enter the full path to search in ~$> ');
  86.  ReadLn(path);
  87.   Write('Enter only the extension of the file ~$> ');
  88.  ReadLn(extension);
  89.   Search(path,extension);
  90.  ReadKey;
  91. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement