Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Obrazets.doc
Скачиваний:
21
Добавлен:
10.06.2015
Размер:
3.99 Mб
Скачать

3.7. Команда attrib

Для получения атрибутов файла и установки атрибутов используются процедуры модуля SysUtils FileGetAttr() и FileSetAttr().

Алгоритм работы процедуры:

1. Если есть ключ справки выводим ее, возвращаемся на начало.

2. Извлекаем путь из строки, в зависимотси от результатов выполняем действия.

3. Если нет пути то выводим состояние файлов в текущем каталоге

3. Извлекаем ключи из строки, в зависимотси от ключей выполняем действия.

4. Если нет ключей то выводим состояние атрибутов файла, или файлов каталога

5. Возвращаемся в начало.

Пример использования процедур приведен ниже:

if UpperCase(rg.Match[0]) = 'ATTRIB ' then

begin

rg.Expression := '\/\?';

rg.Exec(tl);

if rg.Match[0] = '/?' then

begin

MyWriteln('ATTRIB [+R|-R] [+A|-A ] [+S|-S] [+H|-H] [DISK:][PATH:][FILE_NAME:]'

+#13#10+'Задает атрибуты файлу либо по полному, либо по ркаткому пути');

goto g1;

end

else

begin

if Length(Trim(tl)) = Length(Trim('ATTRIB')) then

begin

Writeln(DecodeToOEM('R'):3,DecodeToOEM('H'):3,DecodeToOEM('S'):3,DecodeToOEM('A'):3,DecodeToOEM('I'):3,DecodeToOEM('TYPE'):7,DecodeToOEM('NAME'):25);

help := Copy(curpath,1,length(curpath)-1);

if FindFirst(help+'*.*',SysUtils.faAnyFile,SR) = 0 then

repeat

attr := SysUtils.faReadOnly;

if (SR.Attr and attr) = attr

then rddd.rd := 'r'

else rddd.rd := ' ';

attr := SysUtils.faDirectory;

if (SR.Attr and attr) = attr

then rddd.dir := '<DIR>'

else rddd.dir := '<FILE>';

attr := SysUtils.faHidden;

if (SR.Attr and attr) = attr

then rddd.hid := 'h'

else rddd.hid := ' ';

attr := SysUtils.faSysFile;

if (SR.Attr and attr) = attr

then rddd.sys := 's'

else rddd.sys := ' ';

attr := SysUtils.faSymLink;

if (SR.Attr and attr) = attr

then rddd.ind := 'i'

else rddd.ind := ' ';

attr := SysUtils.faArchive;

if (SR.Attr and attr) = attr

then rddd.arch := 'a'

else rddd.arch := ' ';

Writeln(DecodeToOEM(rddd.rd):3,DecodeToOEM(rddd.hid):3,DecodeToOEM(rddd.sys):3,DecodeToOEM(rddd.arch):3,DecodeToOEM(rddd.ind):3,DecodeToOEM(rddd.dir):7,DecodeToOEM(SR.Name):30);

until FindNext(SR) <> 0;

goto g1;

end;

rg.Expression := '[A-Za-z][:][\\A-Za-z0-9]*[.A-Za-z0-9]*';

rg.Exec(tl);

if rg.Match[0] <> '' then

begin

attr_fn := rg.Match[0];

if not Exists(attr_fn)

then

begin

MyWriteln('Объект не существует');

goto g1;

end;

at_bol := True;

for i := 0 to 3 do

begin

case i of

0: rg.Expression := '\/R+[\+\-]';

1: rg.Expression := '\/A+[\+\-]';

2: rg.Expression := '\/S+[\+\-]';

3: rg.Expression := '\/H+[\+\-]';

end;

rg.Exec(UpperCase(tl));

if rg.Match[0] <> '' then at_bol:= False;

help := rg.Match[0];

if Trim(help) = '' then Continue;

case help[2] of

'R': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faReadOnly)

else FileSetAttr(PAnsiChar(attr_fn),not(faReadOnly));

end;

'A': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faArchive)

else FileSetAttr(PAnsiChar(attr_fn),not(faArchive));

end;

'S': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faSysFile)

else FileSetAttr(PAnsiChar(attr_fn),not(faSysFile));

end;

'H': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faHidden)

else FileSetAttr(PAnsiChar(attr_fn),not(faHidden));

end;

end;

end;

if at_bol = true

then

begin

if not Exists(attr_fn)

then

begin

MyWriteln('Объект не существует');

goto g1;

end;

FindFirst(attr_fn,faAnyFile,SR);

attr := SysUtils.faReadOnly;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.rd := 'r'

else rddd.rd := ' ';

attr := SysUtils.faDirectory;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.dir := '<DIR>'

else rddd.dir := '<FILE>';

attr := SysUtils.faHidden;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.hid := 'h'

else rddd.hid := ' ';

attr := SysUtils.faSysFile;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.sys := 's'

else rddd.sys := ' ';

attr := SysUtils.faSymLink;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.ind := 'i'

else rddd.ind := ' ';

attr := SysUtils.faArchive;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.arch := 'a'

else rddd.arch := ' ';

Writeln(DecodeToOEM(rddd.rd):3,DecodeToOEM(rddd.hid):3,DecodeToOEM(rddd.sys):3,DecodeToOEM(rddd.arch):3,DecodeToOEM(rddd.ind):3,DecodeToOEM(rddd.dir):7,DecodeToOEM(ExtractFileName(attr_fn)):30);

end;

goto g1;

end

else

begin

rg.Expression := '[ ]+[ \\A-Za-z0-9]+[.A-Za-z0-9]+';

rg.Exec(trimleft(tl));

if rg.Match[0] <> ''

then

begin

rg2.Exec(rg.Match[0]);

if rg2.Match[0] <> ''

then

begin

attr_fn := Copy(curpath,1,length(curpath)-1);

if Trim(attr_fn[Length(attr_fn)]) <> '\'

then attr_fn :=attr_fn + TrimLeft(rg.Match[0])

else

begin

if trimleft(rg.Match[0][1]) <> '\'

then attr_fn :=attr_fn + trimleft(rg.Match[0])

else attr_fn :=copy(attr_fn,1,Length(attr_fn)-1) + trimleft(rg.Match[0]);

end;

if not Exists(attr_fn)

then

begin

MyWriteln('Объект не существует');

goto g1;

end;

at_bol:= True;

for i := 0 to 3 do

begin

case i of

0: rg.Expression := '\/R+[\+\-]';

1: rg.Expression := '\/A+[\+\-]';

2: rg.Expression := '\/S+[\+\-]';

3: rg.Expression := '\/H+[\+\-]';

end;

rg.Exec(UpperCase(tl));

if rg.Match[0] <> '' then at_bol:= False;

help := rg.Match[0];

if Trim(help) = '' then Continue;

case help[2] of

'R': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faReadOnly)

else FileSetAttr(PAnsiChar(attr_fn),not(faReadOnly));

end;

'A': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faArchive)

else FileSetAttr(PAnsiChar(attr_fn),not(faArchive));

end;

'S': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faSysFile)

else FileSetAttr(PAnsiChar(attr_fn),not(faSysFile));

end;

'H': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faHidden)

else FileSetAttr(PAnsiChar(attr_fn),not(faHidden));

end;

end;

end;

if at_bol = true

then

begin

if not Exists(attr_fn)

then

begin

MyWriteln('Объект не существует');

goto g1;

end;

FindFirst(attr_fn,faAnyFile,SR);

attr := SysUtils.faReadOnly;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.rd := 'r'

else rddd.rd := ' ';

attr := SysUtils.faDirectory;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.dir := '<DIR>'

else rddd.dir := '<FILE>';

attr := SysUtils.faHidden;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.hid := 'h'

else rddd.hid := ' ';

attr := SysUtils.faSysFile;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.sys := 's'

else rddd.sys := ' ';

attr := SysUtils.faSymLink;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.ind := 'i'

else rddd.ind := ' ';

attr := SysUtils.faArchive;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.arch := 'a'

else rddd.arch := ' ';

Writeln(DecodeToOEM(rddd.rd):3,DecodeToOEM(rddd.hid):3,DecodeToOEM(rddd.sys):3,DecodeToOEM(rddd.arch):3,DecodeToOEM(rddd.ind):3,DecodeToOEM(rddd.dir):7,DecodeToOEM(ExtractFileName(attr_fn)):30);

end;

goto g1;

end

else

begin

attr_fn := Copy(curpath,1,length(curpath)-1);

if Trim(attr_fn[Length(attr_fn)]) <> '\'

then attr_fn :=attr_fn + TrimLeft(rg.Match[0])

else

begin

if trimleft(rg.Match[0][1]) <> '\'

then attr_fn :=attr_fn + trimleft(rg.Match[0])

else attr_fn :=copy(attr_fn,1,Length(attr_fn)-1) + trimleft(rg.Match[0]);

end;

if not Exists(attr_fn)

then

begin

MyWriteln('Объект не существует');

goto g1;

end;

at_bol:= True;

for i := 0 to 3 do

begin

case i of

0: rg.Expression := '\/R+[\+\-]';

1: rg.Expression := '\/A+[\+\-]';

2: rg.Expression := '\/S+[\+\-]';

3: rg.Expression := '\/H+[\+\-]';

end;

rg.Exec(UpperCase(tl));

if rg.Match[0] <> '' then at_bol:= False;

help := rg.Match[0];

if Trim(help) = '' then Continue;

case help[2] of

'R': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faReadOnly)

else FileSetAttr(PAnsiChar(attr_fn),not(faReadOnly));

end;

'A': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faArchive)

else FileSetAttr(PAnsiChar(attr_fn),not(faArchive));

end;

'S': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faSysFile)

else FileSetAttr(PAnsiChar(attr_fn),not(faSysFile));

end;

'H': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faHidden)

else FileSetAttr(PAnsiChar(attr_fn),not(faHidden));

end;

end;

end;

if at_bol = true

then

begin

if not Exists(attr_fn)

then

begin

MyWriteln('Объект не существует');

goto g1;

end;

FindFirst(attr_fn,faAnyFile,SR);

attr := SysUtils.faReadOnly;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.rd := 'r'

else rddd.rd := ' ';

attr := SysUtils.faDirectory;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.dir := '<DIR>'

else rddd.dir := '<FILE>';

attr := SysUtils.faHidden;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.hid := 'h'

else rddd.hid := ' ';

attr := SysUtils.faSysFile;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.sys := 's'

else rddd.sys := ' ';

attr := SysUtils.faSymLink;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.ind := 'i'

else rddd.ind := ' ';

attr := SysUtils.faArchive;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.arch := 'a'

else rddd.arch := ' ';

Writeln(DecodeToOEM(rddd.rd):3,DecodeToOEM(rddd.hid):3,DecodeToOEM(rddd.sys):3,DecodeToOEM(rddd.arch):3,DecodeToOEM(rddd.ind):3,DecodeToOEM(rddd.dir):7,DecodeToOEM(ExtractFileName(attr_fn)):30);

end;

goto g1;

end;

end

else

begin

help := stringreplace(tl, 'ATTRIB ','',[rfIgnoreCase,rfReplaceAll]);

help := Trim(help);

attr_fn := Copy(curpath,1,length(curpath)-1);

if Trim(attr_fn[Length(attr_fn)]) <> '\'

then attr_fn :=attr_fn + TrimLeft(help)

else

begin

if trimleft(help[1]) <> '\'

then attr_fn :=attr_fn + trimleft(help)

else attr_fn :=copy(attr_fn,1,Length(attr_fn)-1) + trimleft(help);

end;

if not Exists(attr_fn)

then

begin

MyWriteln('Объект не существует');

goto g1;

end;

at_bol:= True;

for i := 0 to 3 do

begin

case i of

0: rg.Expression := '\/R+[\+\-]';

1: rg.Expression := '\/A+[\+\-]';

2: rg.Expression := '\/S+[\+\-]';

3: rg.Expression := '\/H+[\+\-]';

end;

rg.Exec(UpperCase(tl));

if rg.Match[0] <> '' then at_bol:= False;

help := rg.Match[0];

if Trim(help) = '' then Continue;

case help[2] of

'R': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faReadOnly)

else FileSetAttr(PAnsiChar(attr_fn),not(faReadOnly));

end;

'A': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faArchive)

else FileSetAttr(PAnsiChar(attr_fn),not(faArchive));

end;

'S': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faSysFile)

else FileSetAttr(PAnsiChar(attr_fn),not(faSysFile));

end;

'H': begin

if help[3] = '+'

then SetFileAttributes(PAnsiChar(attr_fn),faHidden)

else FileSetAttr(PAnsiChar(attr_fn),not(faHidden));

end;

end;

end;

if at_bol = true

then

begin

if not Exists(attr_fn)

then

begin

MyWriteln('Объект не существует');

goto g1;

end;

FindFirst(attr_fn,faAnyFile,SR);

attr := SysUtils.faReadOnly;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.rd := 'r'

else rddd.rd := ' ';

attr := SysUtils.faDirectory;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.dir := '<DIR>'

else rddd.dir := '<FILE>';

attr := SysUtils.faHidden;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.hid := 'h'

else rddd.hid := ' ';

attr := SysUtils.faSysFile;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.sys := 's'

else rddd.sys := ' ';

attr := SysUtils.faSymLink;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.ind := 'i'

else rddd.ind := ' ';

attr := SysUtils.faArchive;

attr1 := SR.Attr;

if (attr1 and attr) = attr

then rddd.arch := 'a'

else rddd.arch := ' ';

Writeln(DecodeToOEM(rddd.rd):3,DecodeToOEM(rddd.hid):3,DecodeToOEM(rddd.sys):3,DecodeToOEM(rddd.arch):3,DecodeToOEM(rddd.ind):3,DecodeToOEM(rddd.dir):7,DecodeToOEM(ExtractFileName(attr_fn)):30);

end;

goto g1;

end;

end;

end;

goto g1;

end;

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]