Функции для работы с файловой системой

Скрыть папку : SetFileAttributes(PChar(‘c:\TestDir’),faHidden);

Создать три папки вложенных друг в друга : ForceDirectories(‘C:\MyDir\Test\Dir1’);

Создание папки : createdir(‘c:\TestDir’); // если не указать директорию, то папка будет создана там же где и программа

Удаление не пустой папки :

[cc lang=»delphi» tab_size=»2″ line_numbers=»false» no_links=»false»]

Function MyRemoveDir(sDir: String): Boolean;
var
iIndex: Integer;
SearchRec: TSearchRec;
sFileName: String;
begin
Result := False;
sDir := sDir + ‘\*.*’;
iIndex := FindFirst(sDir, faAnyFile, SearchRec);

while iIndex = 0 do
begin
sFileName := ExtractFileDir(sDir) + ‘\’ + SearchRec.Name;
if SearchRec.Attr = faDirectory then
begin
if (SearchRec.Name <> ») and (SearchRec.Name <> ‘.’) and
(SearchRec.Name <> ‘..’) then
MyRemoveDir(sFileName);
end
else
begin
if SearchRec.Attr <> faArchive then
FileSetAttr(sFileName, faArchive);
if NOT DeleteFile(sFileName) then
ShowMessage(‘Could NOT delete ‘ + sFileName);
end;
iIndex := FindNext(SearchRec);
end;

FindClose(SearchRec);

RemoveDir(ExtractFileDir(sDir));
Result := True
end;

//Вызов функции

if NOT MyRemoveDir(‘C:\TestDir’) then
ShowMessage(‘Не могу удалить эту директорию’);
[/cc]

Узнать дату и время создания файла :

[cc lang=»delphi» tab_size=»2″ line_numbers=»false» no_links=»false»]

function GetDirTime(const Dir: string): TDateTime;
var
H: Integer;
F: TFileTime;
S: TSystemTime;
begin
H := CreateFile(PChar(Dir), $0080, 0, nil, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, 0);
if H <> -1 then
begin
GetFileTime(H, @F, nil, nil);
FileTimeToLocalFileTime(F, F);
FileTimeToSystemTime(F, S);
Result := SystemTimeToDateTime(S);
CloseHandle(H);
end
else
Result := -1;
end;
//Пример использования:

ShowMessage(DateTimeToStr(GetDirTime(‘c:\TestDir’)));

[/cc]

Узнать размер каталога(папки) :

[cc lang=»delphi» tab_size=»2″ line_numbers=»false» no_links=»false»]

procedure GetDirSize(const aPath: string; var SizeDir: Int64);
var
SR: TSearchRec;
tPath: string;
begin
tPath := IncludeTrailingBackSlash(aPath);
if FindFirst(tPath + ‘*.*’, faAnyFile, SR) = 0 then
begin
try
repeat
if (SR.Name = ‘.’) or (SR.Name = ‘..’) then
Continue;
if (SR.Attr and faDirectory) <> 0 then
begin
GetDirSize(tPath + SR.Name, SizeDir);
Continue;
end;
SizeDir := SizeDir + (SR.FindData.nFileSizeHigh shl 32) +
SR.FindData.nFileSizeLow;
until FindNext(SR) <> 0;
finally
SysUtils.FindClose(SR);
end;
end;
end;
//Пример использования:

procedure TForm1.Button1Click(Sender: TObject);
var
SizeDir: Int64;
begin
SizeDir := 0;
GetDirSize(‘c:\TestDir’, SizeDir);
ShowMessage(‘Размер каталога ‘ + IntToStr(SizeDir));
end;

[/cc]

Вывести сообщение при существовании директории :

[cc lang=»delphi» tab_size=»2″ line_numbers=»false» no_links=»false»]

if DirectoryExists(‘C:\MyDir’)  then showmessage(‘Директория существует’);

[/cc]

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *