一个小程序的大错误:大家来捉虫
ccat 2001-06-24 05:51:00 我前几天写了一个Deltree,只要执行Deltree "目录名"就可以像MS的Deltree命令一样删指定目录,当时是找了程序员大本营光盘上的个函数,自己根据需要改了改就用了。昨晚偶然把它放在C:下的一个子目录下双击,结果它神不知鬼不觉地把C盘格了(或者说清空了)!就在Windows还在正常运行的情况下!现在兄弟已经找到了错,把错误代码拿出来。大家一起娱乐一下,找到错在哪儿就有分!
program deltree;
uses
SysUtils;
procedure DoDelTree(TheDir : String);
Var
Search : TSearchRec;
rec : word;
Begin
TheDir := IncludeTrailingBackslash(TheDir);
rec := FindFirst(TheDir + '*.*', faAnyFile, Search);
While rec = 0 Do
Begin
If Search.Name[1] <> '.' Then
Begin
// Is this a directory?
If (Search.Attr And faDirectory) = faDirectory Then
Begin
// If so, let's call DelTree again using this new
// directory as the TheDir parameter.
DoDelTree(TheDir + Search.Name);
// Not that all of the files are gone from this directoy,
// we can remove the directory.
Removedir(TheDir + Search.Name);
End
Else
Begin
// We found a file.
// Now lets reset its attributes so we don't have any problems
// deleting them.
FileSetAttr(TheDir + Search.Name, 0);
DeleteFile(TheDir + Search.Name);
End;
End;
rec := FindNext(Search);
End;
FindClose(Search);
{The author forgot remove the target directory. Let's do it.}
Removedir(TheDir);
End;
begin
DoDelTree(ParamStr(1));
end.