- •1. Цель курсовой работы
- •2. Командный процессор ос ms Windows cmd
- •2.1. Оболочка (интерпретатор) командной строки command.Com/cmd.Exe
- •Листинг 1. Процедура инициализации
- •2.3. Структура команд. Параметры и ключи. Вывод справочной информации
- •2.4. Команды
- •2.4.4. Команды Date и Time
- •2.4.7.2. Команда cd
- •2.4.7.3. Команда dir
- •2.4.7.4. Команды mkdir
- •2.4.7.5. Команда del или erase
- •3. Назначение ключей и параметров, описание команд
- •3.1. Команда type
- •Листинг 2. Процедура type
- •3.2. Команда time
- •Листинг 3. Процедура отображения и установки времени
- •3.3. Команда date
- •Листинг 4. Отображение и установка системного времени
- •3.4. Команда mkdir
- •Листинг 5. Процедура mkdir
- •3.5. Команда rd
- •Листинг 6. Процедура rd
- •3.6. Команда cd
- •3.7. Команда attrib
- •Листинг 7. ПроцедураAttrib
- •3.8. Команда cls
- •Листинг 8. Процедура cls
- •3.9. Команда sort
- •Листинг 9. Процедура sort
- •3.10. Команда echo
- •Листинг 10. Процедура echo
- •3.11. Команда del|erase
- •Листинг 11. Процедура del
- •3.12. Команда dir
- •Листинг 12. ПроцедураDir
- •3.13. Команда invertcolor
- •Листинг 13. Процедура invertcolor
- •3.14. Разбиение на лексемы, синтаксический анализ, регулярные выражения
- •Листинг 14. Проверка на соответстиве команд
- •3.15. Процедуры упорядочивания массива (сортировка)
- •3.17. Русификация вывода командного процессора
- •4. Примеры выполнения работы команд
- •4.1. Команда dir
- •4.2. Команда attrib
- •4.3. Команда date и time
- •4.4. Команда type и sort
- •4.5. Команда mkdir, rd, cd
- •4.7. Команда cls и echo
- •4.8. Команда del | erase
- •5. Литература
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;