Verificar varios arquivos
Pessoal, estu iniciando no mundo delphi.
e veja a bronca que peguei...
eu tenho um diretorio com varios arquivos.
oe.txt
ob.txt
oa.txt
.
.
.
e nele tem varias linhas que preciso pegar para inserir em um arquivo gerado pela aplicação. (isso ta OK!)
meu problema é que não consigo mudar de arquivo.
o nome do arquivo ta amarrado, como faço para percorrer um a um??
veja meu codigo:
procedure TfrmVicImpFiacao.btnGerarClick(Sender: TObject);
var f, arquivo, log:TextFile;
data, NomeDoLog, valor, Maq, Efc, Peso, Linha:String;
i : integer; begin
i := 1;
data := edtData.Text;
while (i <= 20) do begin
AssignFile(f, 'C:\Fiacao\Destino\OE.txt');
NomeDoLog := 'C:\Fiacao\Destino\OE.txt';
AssignFile(log, NomeDoLog);
Reset(f);
While not Eof(f) do
begin
Readln(f, Linha);
if (copy(linha,1,2)='MA') then
maq := (copy(linha,6,2));
if (copy(linha,2,2)='MA') then
efc := (copy(linha,10,6));
if (copy(linha,1,2)='KG') then
peso := (copy(linha,7,7));
end;
closefile(f);
if FileExists(NomeDoLog) then
Append(log)
else
ReWrite(log);
try
WriteLn(log, maq+';'+efc+';'+peso);
finally
CloseFile(log)
end; end;
end;
end.
var f, arquivo, log:TextFile;
data, NomeDoLog, valor, Maq, Efc, Peso, Linha:String;
i : integer; begin
i := 1;
data := edtData.Text;
while (i <= 20) do begin
AssignFile(f, 'C:\Fiacao\Destino\OE.txt');
NomeDoLog := 'C:\Fiacao\Destino\OE.txt';
AssignFile(log, NomeDoLog);
Reset(f);
While not Eof(f) do
begin
Readln(f, Linha);
if (copy(linha,1,2)='MA') then
maq := (copy(linha,6,2));
if (copy(linha,2,2)='MA') then
efc := (copy(linha,10,6));
if (copy(linha,1,2)='KG') then
peso := (copy(linha,7,7));
end;
closefile(f);
if FileExists(NomeDoLog) then
Append(log)
else
ReWrite(log);
try
WriteLn(log, maq+';'+efc+';'+peso);
finally
CloseFile(log)
end; end;
end;
end.
Carlos
Curtidas 0
Respostas
Emerson Nascimento
22/03/2010
antes de tentar sanar o seu problema eu queria entender o código:
procedure TfrmVicImpFiacao.btnGerarClick(Sender: TObject);
var
f, log: TextFile;
NomeDoLog, Maq, Efc, Peso, Linha: String;
i : integer;
begin
i := 1;
while (i <= 20) do
begin
AssignFile(f, 'C:\Fiacao\Destino\OE.txt'); <- abre uma vez
NomeDoLog := 'C:\Fiacao\Destino\OE.txt';
AssignFile(log, NomeDoLog); <- abre outra vez
Reset(f);
While not Eof(f) do
begin
Readln(f, Linha);
if (copy(linha,1,2)='MA') then
maq := (copy(linha,6,2));
if (copy(linha,2,2)='MA') then
efc := (copy(linha,10,6));
if (copy(linha,1,2)='KG') then
peso := (copy(linha,7,7));
end;
closefile(f);
if FileExists(NomeDoLog) then
Append(log)
else
ReWrite(log);
try
WriteLn(log, maq+';'+efc+';'+peso);
finally
CloseFile(log)
end;
end;
end;
porque abrir duas vezes o mesmo arquivo?
procedure TfrmVicImpFiacao.btnGerarClick(Sender: TObject);
var
f, log: TextFile;
NomeDoLog, Maq, Efc, Peso, Linha: String;
i : integer;
begin
i := 1;
while (i <= 20) do
begin
AssignFile(f, 'C:\Fiacao\Destino\OE.txt'); <- abre uma vez
NomeDoLog := 'C:\Fiacao\Destino\OE.txt';
AssignFile(log, NomeDoLog); <- abre outra vez
Reset(f);
While not Eof(f) do
begin
Readln(f, Linha);
if (copy(linha,1,2)='MA') then
maq := (copy(linha,6,2));
if (copy(linha,2,2)='MA') then
efc := (copy(linha,10,6));
if (copy(linha,1,2)='KG') then
peso := (copy(linha,7,7));
end;
closefile(f);
if FileExists(NomeDoLog) then
Append(log)
else
ReWrite(log);
try
WriteLn(log, maq+';'+efc+';'+peso);
finally
CloseFile(log)
end;
end;
end;
porque abrir duas vezes o mesmo arquivo?
GOSTEI 0
Emerson Nascimento
22/03/2010
outra coisa:
qual o critério de seleção dos arquivos? todos os arquivos de uma pasta? todos os arquivos iniciados com uma determinada letra? todos os arquivos de uma certa extensão?
qual o critério de seleção dos arquivos? todos os arquivos de uma pasta? todos os arquivos iniciados com uma determinada letra? todos os arquivos de uma certa extensão?
GOSTEI 0
Carlos
22/03/2010
boa noite
todo arquivo com o mesmo inicio, por exemplo:
c:\fiacao\oe012323.txt
c:\fiacao\oe023323.txt
c:\fiacao\oe012133.txt
c:\fiacao\oe043123.txt
c:\fiacao\oe051232.txt
c:\fiacao\oe011029.txt
entao,
arquivo de mesma pasta e inicio.
Grato
Carlos
GOSTEI 0
Marcelo Cavalcanti
22/03/2010
Sobre como obter vários arquivos, tente procurá-los utilizando algo como:
O código abaixo foi retirado do próprio HELP do Delphi 7, é bem fácil o entendimento.
The following example uses an edit control, a button, a string grid, and seven check boxes. The check boxes correspond to the seven possible file attributes. When the button is clicked, the path specified in the edit control is searched for files matching the checked file attributes. The names and sizes of the matching files are inserted into the string grid.
procedure TForm1.Button1Click(Sender: TObject);
var
sr: TSearchRec; ----------------------------------IMPORTANTE
FileAttrs: Integer;
begin
StringGrid1.RowCount := 1;
if CheckBox1.Checked then
FileAttrs := faReadOnly
else
FileAttrs := 0;
if CheckBox2.Checked then
FileAttrs := FileAttrs + faHidden;
if CheckBox3.Checked then
FileAttrs := FileAttrs + faSysFile;
if CheckBox4.Checked then
FileAttrs := FileAttrs + faVolumeID;
if CheckBox5.Checked then FileAttrs := FileAttrs + faDirectory;
if CheckBox6.Checked then
FileAttrs := FileAttrs + faArchive;
if CheckBox7.Checked then FileAttrs := FileAttrs + faAnyFile; with StringGrid1 do
begin
RowCount := 0; if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then ----------------------------------IMPORTANTE begin
repeat
if (sr.Attr and FileAttrs) = sr.Attr then
begin
RowCount := RowCount + 1;
Cells[1,RowCount-1] := sr.Name;
Cells[2,RowCount-1] := IntToStr(sr.Size);
end;
until FindNext(sr) <> 0; ----------------------------------IMPORTANTE
FindClose(sr); ----------------------------------IMPORTANTE
end;
end;
end; Marcelo Rezende Cavalcanti
--------------------------------------------------------------------
www.swg2.com.br (Transformando suas informações em dados gerenciais)
sr: TSearchRec; ----------------------------------IMPORTANTE
FileAttrs: Integer;
begin
StringGrid1.RowCount := 1;
if CheckBox1.Checked then
FileAttrs := faReadOnly
else
FileAttrs := 0;
if CheckBox2.Checked then
FileAttrs := FileAttrs + faHidden;
if CheckBox3.Checked then
FileAttrs := FileAttrs + faSysFile;
if CheckBox4.Checked then
FileAttrs := FileAttrs + faVolumeID;
if CheckBox5.Checked then FileAttrs := FileAttrs + faDirectory;
if CheckBox6.Checked then
FileAttrs := FileAttrs + faArchive;
if CheckBox7.Checked then FileAttrs := FileAttrs + faAnyFile; with StringGrid1 do
begin
RowCount := 0; if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then ----------------------------------IMPORTANTE begin
repeat
if (sr.Attr and FileAttrs) = sr.Attr then
begin
RowCount := RowCount + 1;
Cells[1,RowCount-1] := sr.Name;
Cells[2,RowCount-1] := IntToStr(sr.Size);
end;
until FindNext(sr) <> 0; ----------------------------------IMPORTANTE
FindClose(sr); ----------------------------------IMPORTANTE
end;
end;
end; Marcelo Rezende Cavalcanti
--------------------------------------------------------------------
www.swg2.com.br (Transformando suas informações em dados gerenciais)
GOSTEI 0
Marcelo Cavalcanti
22/03/2010
Lembrando que no centro do loop ficará o seu código. Você passará apenas o nome do arquivo através da variável "sr.Name;".
Sds,
Marcelo Rezende Cavalcanti
--------------------------------------------------------------------
www.swg2.com.br (Transformando suas informações em dados gerenciais)
--------------------------------------------------------------------
www.swg2.com.br (Transformando suas informações em dados gerenciais)
GOSTEI 0