如题
本来是想做一个取得文件列表然后计算文件大小、压缩的东西的。
最后跟来跟去发现result没有初始化
var
  SearchRec:TSearchRec;
  dDir: string;
begin
  if not SysUtils.DirectoryExists(DestDir) then
    Exit;
  dDir := DestDir;
  if DestDir[Length(dDir)] <> '\' then
    dDir := dDir + '\';
  try
    if (SysUtils.FindFirst(dDir + '*.*',faAnyFile,SearchRec)=0)then
    begin
      repeat
        if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
        begin
          if SearchRec.Attr <> faDirectory then
          begin
            if UpperCase(SearchRec.Name) <> 'THUMBS.DB' then
            result := result + (dDir + SearchRec.Name) + #13
          end
          else
            result := result + RansackDir(dDir + SearchRec.Name);
        end;
      until(SysUtils.FindNext(SearchRec)<>0);
    end;
  finally
    SysUtils.FindClose(SearchRec);
  end;
end;

解决方案 »

  1.   

    变量没有初始化,但是他有默认值。
    boolean 型默认是false
    string 默认是 ''
    interger等默认是 0虽然有这个默认,但是还是编译器会给你警告提示的。
      

  2.   

    >>result没有初始化
    如果是integer 類型的, 沒有顯示初始化, 就是 0 了
      

  3.   

    在函数中返回值是string 应该初始化为''的是吧
    就算不初始化在堆栈中递归的调用也不应该读取上级的值吧
    您可以把这个函数试试
    会把结果重复叠加的
      

  4.   

    function RansackDir(DestDir: string): string;
    var
      SearchRec:TSearchRec;
      dDir: string;
    begin
      if not SysUtils.DirectoryExists(DestDir) then
        Exit;
      dDir := DestDir;
      if DestDir[Length(dDir)] <> '\' then
        dDir := dDir + '\';
      try
        if (SysUtils.FindFirst(dDir + '*.*',faAnyFile,SearchRec)=0)then
        begin
          repeat
            if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
            begin
              if SearchRec.Attr <> faDirectory then
              begin
                if UpperCase(SearchRec.Name) <> 'THUMBS.DB' then
                result := result + (dDir + SearchRec.Name) + #13
              end
              else
                result := result + RansackDir(dDir + SearchRec.Name);
            end;
          until(SysUtils.FindNext(SearchRec)<>0);
        end;
      finally
        SysUtils.FindClose(SearchRec);
      end;
    end;
      

  5.   

    最近一直天天加班 回去也没开delphi的机会了
    贴出我的代码如下,写的不好请见谅了
    但经过测试全部能够使用,如发现问题,请通知我 哈
    unit sFiles;interfaceuses Windows, SysUtils, Classes, Registry, ShellAPI, SHFolder;type
      TEnumDirectoryFileProc = procedure (Filename: string; var bContinue: Boolean) of object;procedure EnumDirectoryFiles(const sDir : String;
                                 const FileName: String;
                                 Attr: Integer;
                                 IncludeSubs : boolean;
                                 Callback : TEnumDirectoryFileProc);procedure EnumAllFiles(const sDir : String;
                           Attr: Integer;
                           IncludeSubs : boolean;
                           Callback : TEnumDirectoryFileProc);
    procedure EnumDirectoryFiles(const sDir : String;
                                 const Filename: String;
                                 Attr: Integer;
                                 IncludeSubs : boolean;
                                 Callback : TEnumDirectoryFileProc);
    var
      list: TStrings;
      SearchRec :TSearchRec;
      Result : LongInt;
      bc: Boolean;
    begin
      list := TStringList.Create;
      list.Add(sDir);
      if not IncludeSubs then
      begin
        Result := FindFirst(list.Strings[0]+'\'+Filename, Attr, SearchRec);
        while Result = 0 do
        begin
          if not (SearchRec.name[1]='.') then
          begin
            if (SearchRec.Attr and faDirectory) = 0 then
            begin
              { Call are callback function}
              Callback(list.Strings[0]+'\'+SearchRec.name, bc);
              if not bc then
              begin
                SysUtils.FindClose(SearchRec);
                exit;
              end;
            end;
          end; //end if
          if not bc then
          begin
            SysUtils.FindClose(SearchRec);
            exit;
          end;
          Result := FindNext(SearchRec);
        end;
        SysUtils.FindClose(SearchRec);
        exit;
      end;  while list.Count<>0 do
      begin
        Result := FindFirst(list.Strings[0]+'\'+Filename, Attr, SearchRec);
        while Result = 0 do
        begin
          { This makes sure its not the . or .. directorys}
          if not (SearchRec.name[1]='.') then
          begin
            if (SearchRec.Attr and faDirectory) = 0 then
            begin
              { Call are callback function}
              Callback(list.Strings[0]+'\'+SearchRec.name, bc);
              if not bc then
              begin
                SysUtils.FindClose(SearchRec);
                exit;
              end;
            end;
          end; //end if
          Result := FindNext(SearchRec);
        end;  //end while
        SysUtils.FindClose(SearchRec);
        Result := FindFirst(list.Strings[0]+'\*.*', faAnyFile, SearchRec);
        while Result = 0 do
        begin
          { This makes sure its not the . or .. directorys}
          if not (SearchRec.name[1]='.') then
          begin
            if (SearchRec.Attr and faDirectory) <> 0 then
            begin
              list.Add(list.Strings[0]+'\'+SearchRec.Name);
            end;
          end; //end if
          Result := FindNext(SearchRec);
        end;  //end while
        SysUtils.FindClose(SearchRec);
        list.Delete(0);
      end;  list.Free;
    end;procedure EnumAllFiles(const sDir : String;
                           Attr: Integer;
                           IncludeSubs : boolean;
                           Callback : TEnumDirectoryFileProc);
    var
      SearchRec :TSearchRec;
      Result : LongInt;
      bc: Boolean;
    begin
      try
      Result := FindFirst(sDir+'\'+'*.*', Attr, SearchRec);
      while Result = 0 do
      begin
        { This makes sure its not the . or .. directorys}
        if SearchRec.name[1]<>'.' then
        begin
          if (SearchRec.Attr and faDirectory) <> 0 then
          begin
            {its a dir so do a recursive call if subdirectorys wanted}
            if IncludeSubs then
              EnumAllFiles(sDir+'\'+SearchRec.name, Attr, IncludeSubs, callback);
          end
          else
          begin
            { Call are callback function}
            Callback(sDir+'\'+SearchRec.name, bc);
            if not bc then break;
          end
        end; //if . ..
        Result := FindNext(SearchRec);
      end;  //end while
      finally
      SysUtils.FindClose(SearchRec);
      end;
    end;end.由于是日文系统 注释全变成乱麻了。。
      

  6.   

    使用时这样:
    //当每次枚举到一个文件或文件夹的时候会自动调用这个函数
    //Filename是枚举到的文件或文件夹的名称,包含完整路径
    //bContinue通知枚举函数是否继续枚举文件或文件夹。为false的时候将停止继续枚举
    procedure mycallback(Filename: string; var bContinue: Boolean);
    begin
      //在这里对枚举到的文件进行操作
    end;EnumDirectoryFiles("c:\","test.bat",faReadonly,true, mycallback)
    //这个将搜索c:\下面名字为test.bat的并且文件属性是只读的所有文件,包括c:\下的所有子目录EnumAllFiles("c:\", faReadonly,true,mycallback)
    //这个将枚举c:\下所有文件属性为只读的文件,搜索包括c:\下的所有子目录EnumDirectoryFiles("c:\","*.*",faReadonly,true, mycallback)

    EnumAllFiles("c:\", faReadonly,true,mycallback)
    效果是一样的文件属性的相关内容可以参考borland的帮助文件
    提示:在原代码中找到fareadonly(这可以通过右键菜单做到),就可在其附近找到所有文件属性相关常量
      

  7.   

    以前也有一位大大提过,这种情况主要是出于优化的考虑,因为如果每次都初始化Result,有时候是不必要的,因为用户本身也有初始化而且Delphi语言手册上也明确说
    If the function exits without assigning a value to Result or the function name, then the function's return value is undefined.换句话说Delphi是不初始化Result的,这和局部变量是一样的