OS X / Cocoa - 如何从目录中读取每个文件?

时间:2011-07-16 23:35:43

标签: cocoa macos

使用Cocoa从OS X上的目录中读取每个文件的“正确方法”是什么?我需要获取每个文件的文件名,然后我需要将其内容拉入一个NSString。

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

使用NSFileManager获取相关路径的枚举器:

  TForm1 = class(TForm)
    [...]
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    ListBox1: TListBox;
    ADOCommand1: TADOCommand;
    Button1: TButton;
    [...]
const
  scSheetName = 'test';
  scCreateSheet = 'create table %s (id  integer, name char(80))';
  scSelect = 'select * from [%s]';

procedure TForm1.FormDestroy(Sender: TObject);
begin
  AdoQuery1.Close;
  AdoQuery2.Close;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if ListBox1.Items.IndexOf(scSheetName)  < 0 then begin
    AdoCommand1.CommandText := Format(scCreateSheet, [scSheetName]);
    AdoCommand1.Execute;
  end;
  AdoQuery2.SQL.Text := Format(scSelect, [scSheetName]);
  if AdoQuery2.Active then
    AdoQuery2.Close;
  AdoQuery2.Open;
  if AdoQuery2.RecordCount = 0 then begin
    AdoQuery2.InsertRecord([1, 'Name1']);
    AdoQuery2.InsertRecord([2, 'Name2']);
  end;
  GetTables;
end;

procedure TForm1.GetTables;
begin
  AdoConnection1.GetTableNames(ListBox1.Items,True);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  AdoConnection1.Connected := True;
  AdoQuery1.Open; // this just selects whatever is on the first worksheet of the spreadsheet
  GetTables;
end;

然后在循环中调用枚举器上的nextObject,直到它返回nil。