Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- { Written by Kulverstukas on 2010.04.05 for EZ community - evilzone.org || last update: 2010.04.19 }
- program CountMyFiles;
- uses Dos, SysUtils, Crt;
- var SRecord : TSearchRec;
- EnvVariable : string;
- TotalSize : longint;
- path, extension : string;
- //=======================================
- {
- This program searches for files with a given extension in a given directory and outputs file name and size.
- WildCard is supported. All folders and files are visible to this - even those set with +s attribute.
- }
- {$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
- //=======================================
- Procedure Search(path, extension : string); // procedure with 2 calls
- begin
- path := SysUtils.IncludeTrailingBackSlash(path); // This checks whether there is a trailing delimiter for given path at the end or not ("\" for Windows; "/" for UNIX).
- //================ // check if entered extension has a dot. If it has then skip it, if not then add it along with extension
- if (Pos('.',extension) = 0) then // Pos(char,string) searches in "string" for "char". If "char" is not found in "string" then 0 is returned
- begin
- EnvVariable := path+'*.'+extension;
- end
- else
- begin
- EnvVariable := path+'*'+extension;
- end;
- //================
- FindFirst(EnvVariable,AnyFile,SRecord); // EnvVariable - path with extension, AnyFile - attribute, SRecord - found files;
- ClrScr;
- TextColor(Green);
- WriteLn('Filename'+Space(52),'FileSize'); // Write FileName and FileSize in one line with 22 spaces between
- TextColor(White);
- WriteLn('=============================================================================');
- //================ // if SRecord.Size is 0 and file name doesn't have "." then it's a dir
- Repeat // search for files until no match. If no match ir found then FindNext(); returns something else then 0
- begin
- 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
- begin
- WriteLn(SRecord.Name+Space(59-Length(SRecord.Name)),'...is a Dir')
- end
- //================ // if SRecord.Size is bigger then 1073741824 bytes (1 GB) then say it's over a Gigabyte
- else
- begin
- if (SRecord.Size >= 1073741824) then
- begin
- WriteLn(SRecord.Name+Space(59-Length(SRecord.Name)),'over a Gigabyte');
- end
- //================
- else
- begin
- WriteLn(SRecord.Name+Space(59-Length(SRecord.Name)),SRecord.Size:9,' Bytes');
- end;
- end;
- //================
- {
- This sums all files together. Files bigger then 900 MB are not included as it causes runtime error (215)
- for some unknown reason. Might be because it overflows the array or something:
- [215] Arithmetic overflow error - This error is reported when the result of an arithmetic operation is
- outside of its supported range.
- }
- TotalSize := TotalSize + SRecord.Size;
- FindNext(SRecord);
- if (TotalSize > 943718400) then
- begin
- TotalSize := 0;
- end;
- end;
- Until FindNext(SRecord) <> 0;
- WriteLn('=============================================================================');
- WriteLn('Total Size: ',TotalSize,' Bytes');
- end;
- //=======================================
- begin
- ClrScr;
- //================== // user instructions starts from here
- GotoXY(6,10);
- Writeln('Input ''*'' for extension to search for everything in the given path.');
- GotoXY(6,11);
- Write('Don''t input anything for extension to search for folders only.');
- GotoXY(6,12);
- Write('Run this program from CMD to use copy/paste.');
- //==================
- GotoXY(1,2);
- Write('Enter the full path to search in ~$> ');
- ReadLn(path);
- Write('Enter only the extension of the file ~$> ');
- ReadLn(extension);
- Search(path,extension);
- ReadKey;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement