文件读写问题,非常急~````(100分)

  • 主题发起人 主题发起人 flashwolf
  • 开始时间 开始时间
除此之外,还要把每个文件第一行删除掉。谁能搞定分数全给。。。
 
有没有搞错,分开字符用函数StringReplace(nkeyword,',',#13,[rfReplaceAll]);
不就可以了。
找文件:
var SearchRec: TSearchRec;
retval:integer;
begin
RetVal := FindFirst(direc, faAnyFile, SearchRec);
//dirc是你的文件路径,如c:/aa/*.txt
try
while RetVal = 0 do
begin
// 读文件 操作
RetVal := FindNext(SearchRec);//继续
end;
finally
FindClose(SearchRec);
end;
 
利用一个TStringList变量作为文件的读取变量,如:
strlistFile: TStringList;

procedure Read();
var
strlistLine: TStringList;
begin
strlistFile.LoadFromFile('*.txt');
strlistLine := TStringList.Create;
for i := 1 to strlistFile.Count - 1 do //从1开始,去掉第一行
begin
strlistLine.CommaText := strlistFile;
for j := 0 to strlistLine.Count -1 do
//InsertDB Statement
end;
strlistLine.Free
end;
 
其实自己写的并不见得有现有工具来得效率高的。 [:D]

代码:
var
  SearchRec: TSearchRec;
  FindResult: Integer;
  Strs1, Strs2: TStrings;
  I, J: Integer;
begin
  FindResult := FindFirst(edtDirectory.Text {'C:/Import/*.CSV'}, faAnyFile, SearchRec);
  try
    Strs1 := TStringList.Create;
    try
      Strs2 := TStringList.Create;
      try
        while FindResult = 0 do
        begin
          Strs1.LoadFromFile(ExtractFilePath(edtDirectory.Text) + SearchRec.Name);//全部文件内容读进内存,如果文件很大的话。。。[:(]
          if Strs1.Count > 1 then
            for I := 1 to Strs1.Count - 1 do
            begin
              Strs2.CommaText := Strs1[I]; //字符串的分割功能可以用Pos与Copy两个函数结合来实现,这儿就直接用了TStringList的功能
              for J := 0 to Strs2.Count - 1 do
              begin
//                MyTable.Append;
//                MyTable.FieldByName('').AsString := Strs2[J];
//                MyTable.Post;
              end;
            end;
          FindResult := FindNext(SearchRec); //查找下一个文件
        end;
      finally
        Strs2.Free;
      end;
    finally
      Strs1.Free;
    end;
  finally
    FindClose(SearchRec);
  end;
好了,编译执行吧! 保证你要等上半天,最好先加个ProgressBar,免得到时不知是死了还是慢啊。[:D]
 
弄完了,我的程序如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids, inifiles, Buttons, Db, DBTables, DBGrids;

type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Button3: TButton;
DBGrid1: TDBGrid;
Table1: TTable;
Database1: TDatabase;
DataSource1: TDataSource;
ListBox1: TListBox;
Label1: TLabel;
procedure GetValue(stockname,Value: string);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}


procedure Tform1.GetValue(stockname,Value: string);
var iPosion: integer;
s1,s2,s3,s4,s5,s6:string;
begin
s1:=stockname;

iPosion := Pos(',', Value)-1;
s2:=copy(value,0,iposion);

value:=copy(value,iposion+2,length(value)-iposion);
iPosion := Pos(',', Value)-1;
s3:=copy(value,0,iposion);

value:=copy(value,iposion+2,length(value)-iposion);
iPosion := Pos(',', Value)-1;
s4:=copy(value,0,iposion);

value:=copy(value,iposion+2,length(value)-iposion);
iPosion := Pos(',', Value)-1;
s5:=copy(value,0,iposion);

value:=copy(value,iposion+2,length(value)-iposion);
iPosion := Pos(',', Value)-1;
s6:=copy(value,0,iposion);

if not table1.Active then table1.Open;
table1.append;
table1.FieldByName('stockid').asstring:=s1;
table1.FieldByName('curdate').asstring:=s2;
table1.FieldByName('openprice').asstring:=s3;
table1.FieldByName('highest').asstring:=s4;
table1.FieldByName('lowest').asstring:=s5;
table1.FieldByName('latest').asstring:=s6;
table1.post;

end;

procedure TForm1.Button3Click(Sender: TObject);
var
I, J: integer;
F: TextFile;
FirstLine: string;
openprice, hightest: string;
tempstr: string;

begin
OpenDialog1.Options := [ofAllowMultiSelect, ofFileMustExist];
OpenDialog1.Filter := 'Text files (*.txt)|*.txt|All files (*.*)|*.*';
OpenDialog1.FilterIndex := 2; { start the dialog showing all files }
if OpenDialog1.Execute then
with OpenDialog1.Files do
for I := 0 to Count - 1 do
begin
listbox1.Items.LoadFromFile(Strings);
tempstr := ExtractFilename(Strings);
tempstr := copy(tempstr, 0, pos('.', tempstr) - 1);
for j := 1 to listbox1.Items.Count - 1 do
GetValue(tempstr,listbox1.Items[j]);
table1.Close;
table1.open;
end;
end;

end.
 
procedure TForm1.Button1Click(Sender: TObject);
var TempString: TStringList;
i:integer;
TempSQl :string;
function GetValue(Value:string):string;
var iPosion :integer;
TempStr1,TempStr2 :string;
begin
iPosion:=Pos(',',Value); //这个,号可以替换成任可格式
if iPosion>0 then
begin
TempStr2 :=copy(Value,iPosion+1,length(Value)-iPosion);
TempStr1 :=copy(Value,0,iPosion-1)+','+GetValue(TempStr2); //处理成SQL语句中的VALUES格式
iPosion:=Pos(',',TempStr2);;
end
else TempStr1 :=Value;
result :=TempStr1;
end;
begin
TempString :=TStringList.Create;
try
TempString.LoadFromFile('d:/temp/'+'test'+'.txt'); //此处的test可替换为变量名,再在这个程序基础上加个大循环就可以了,0001,0002,0003.。。。。
TempSQl :='insert into tablename (fieldname1,fieldnamee2,fieldnamee3,fieldname4...) values ('''+test+''',';
for i := 1 to TempString.Count - 1 do //从1开始,去掉第一行
begin
Memo1.Lines.Add(GetValue(TempString.Strings));
TempSQl :=TempSQl +GetValue(TempString.Strings));
end;
TempSQl :=TempSQl+')'
//将它赋给sql就可以执行了嘛
finally
TempString.Free
end;
end;

补充一句,如果你的分隔符为,号不变的化,你就可以直接将读出的每一个串再加上言文件名就可以直接用于sql语句了。。。。呵呵很简单
try
TempString.LoadFromFile('d:/temp/'+'test'+'.txt'); //此处的test可替换为变量名
TempSQl :='insert into tablename (filedname1,filedname2,filedname3,filedname4...) values (';
for i := 1 to TempString.Count - 1 do //从1开始,去掉第一行
begin
TempSQl:=文件名+ TempString.Strings);
end;
TempSQl :=TempSQl+')' ;
//将它赋给sql就可以执行了嘛
finally
TempString.Free
end;
 
多人接受答案了。
 

Similar threads

回复
0
查看
815
不得闲
回复
0
查看
867
不得闲
回复
0
查看
1K
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部