Delphi 2007에서는 어떻게하면 첫 번째 파일을 읽은 다음 마지막 파일까지 두 번째 파일을 읽는 닫는 프로그램을 만들 수 있습니까?Delphi : 여러 파일을 처리하는 방법?
1
A
답변
4
디렉토리의 모든 파일을 읽으려고합니까? 이 클래스를 사용해보십시오. 이것은 내 원래 코드가 아니지만 조금 수정하고 사용자 정의했습니다. TFilestream에 전달할 수있는 문자열 목록에 설정된 기준과 일치하는 모든 파일 이름을 제공합니다.
unit findfile;
interface
uses
Classes;
type
TFileAttrKind = (ffaReadOnly, ffaHidden, ffaSysFile, ffaDirectory, ffaArchive, ffaAnyFile);
TFileAttr = set of TFileAttrKind;
TFindFile = class(TObject)
private
s: TStringList;
fSubFolder : boolean;
fAttr: TFileAttr;
FPath : string;
FBasePath: string;
fFileMask : string;
FDepth: integer;
procedure SetPath(Value: string);
procedure FileSearch(const inPath : string);
function cull(value: string): string;
public
constructor Create(path: string);
destructor Destroy; override;
function SearchForFiles: TStringList;
property FileAttr: TFileAttr read fAttr write fAttr;
property InSubFolders : boolean read fSubFolder write fSubFolder;
property Path : string read fPath write SetPath;
property FileMask : string read fFileMask write fFileMask ;
end;
implementation
{$WARN SYMBOL_PLATFORM OFF}
{$WARN UNIT_PLATFORM OFF}
uses
Windows, SysUtils, FileCtrl;
constructor TFindFile.Create(path: string);
begin
inherited Create;
FPath := path;
FBasePath := path;
FileMask := '*.*';
FileAttr := [ffaReadOnly, ffaDirectory, ffaArchive];
s := TStringList.Create;
FDepth := -1;
end;
procedure TFindFile.SetPath(Value: string);
begin
if fPath <> Value then
begin
if (Value <> '') and (DirectoryExists(Value)) then
fPath := IncludeTrailingPathDelimiter(Value);
end;
end;
function TFindFile.SearchForFiles: TStringList;
begin
s.Clear;
try
FileSearch(Path);
finally
Result := s;
end;
end;
function TFindFile.cull(value: string): string;
begin
result := StringReplace(value, FBasePath, '', []);
end;
destructor TFindFile.Destroy;
begin
s.Free;
inherited Destroy;
end;
procedure TFindFile.FileSearch(const InPath : string);
var Rec : TSearchRec;
Attr : integer;
begin
inc(FDepth);
try
Attr := 0;
if ffaReadOnly in FileAttr then
Attr := Attr + faReadOnly;
if ffaHidden in FileAttr then
Attr := Attr + faHidden;
if ffaSysFile in FileAttr then
Attr := Attr + faSysFile;
if ffaDirectory in FileAttr then
Attr := Attr + faDirectory;
if ffaArchive in FileAttr then
Attr := Attr + faArchive;
if ffaAnyFile in FileAttr then
Attr := Attr + faAnyFile;
if SysUtils.FindFirst(inPath + FileMask, Attr, Rec) = 0 then
try
repeat
if (Rec.Name = '.') or (Rec.Name = '..') then
Continue;
s.Add(cull(inPath) + Rec.Name);
until SysUtils.FindNext(Rec) <> 0;
finally
SysUtils.FindClose(Rec);
end;
If not InSubFolders then
Exit;
if SysUtils.FindFirst(inPath + '*.*', faDirectory, Rec) = 0 then
try
repeat
if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name <> '.') and (Rec.Name <> '..') then
begin
FileSearch(IncludeTrailingPathDelimiter(inPath + Rec.Name));
end;
until SysUtils.FindNext(Rec) <> 0;
finally
SysUtils.FindClose(Rec);
end;
finally
dec(FDepth);
end;
end;
end.
1
DIFileFinder이 좋을 수도 있습니다.
1
동작을 반복하려면 루프이라고합니다. 델파이에는 루프에 대한 예약어가 3 개 있습니다 (for
, repeat
및 while
). 모두는 도움말 파일에 문서화되어 있습니다. 가장 중요한 주제는 structured statements입니다. 당신은 그들에 대해 읽는 것이 좋을 것입니다.
기존의 for
루프는 처리하려는 배열이나 목록이있는 경우에 가장 적합합니다. 아마도 파일 이름 목록 일 것입니다.
for i := 0 to High(FileNames) do begin ... end;
나이 :
for i := 0 to Pred(FileNames.Count) do begin ... end;
는 그런 다음 현재 반복의 파일 이름을 얻기 위해 루프 내 FileNames[i]
를 참조합니다 당신은이 같은 루프를 작성할 수 있습니다. 또한 파일 이름이 포함 된 것이 열거 자 또는 반복자을 사용할 때 사용할 새로운 스타일 for
루프가 있습니다.
for name in FileNames do begin ... end;
While
및 repeat
루프는 루프가 코드를 실행해야하는 횟수를 시작하기 전에 반드시 모를 때 사용됩니다 그럼 당신은 이런 식으로 루프를 작성합니다. 이것을 FindFirst
과 FindNext
Delphi 함수와 함께 사용할 수 있습니다. 예 :
if FindFirst('*.txt', faAnyFile, SearchResult) = 0 then try
repeat
// Do something with SearchResult.Name
until FindNext(SearchResult) <> 0;
finally
SysUtils.FindClose(SearchResult);
end;
관련 문제
- 1. Asp.NET에서 여러 요청을 처리하는 방법
- 2. 여러 대의 대리인을 처리하는 방법
- 3. 파이썬 zip 파일을 처리하는 방법
- 4. 대용량 MXML 파일을 처리하는 방법
- 5. 여러 파일을 처리하는 가장 완고한 방법은 무엇입니까
- 6. 여러 다른 파일을 처리하는 좋은 방법은 무엇입니까?
- 7. 여러 파일을 동시에 처리하는 방법은 무엇입니까?
- 8. Delphi - 작업이 실행되는 동안 WMDeviceChange 메시지를 듣고 처리하는 방법?
- 9. 여러 스크롤링 열을 처리하는 가장 좋은 방법
- 10. 클래스에서 여러 참조를 가장 잘 처리하는 방법
- 11. Python : 여러 클라이언트와 서버를 처리하는 방법
- 12. Rx에서 여러 이벤트를 처리하는 방법 (종속성 포함)
- 13. 여러 국가의 주 /도 UI를 처리하는 방법
- 14. 여러 페이지 REST 응답을 처리하는 방법
- 15. 여러 HTMLPurifier 구성을 효율적으로 처리하는 방법
- 16. 여러 제출을 처리하는 방법 서버 측
- 17. 동일한 데이터를 포함하는 여러 스키마를 처리하는 방법
- 18. Node.js로 여러 채팅 세션을 처리하는 방법
- 19. CakePHP에서 여러 주소로 고객을 처리하는 방법
- 20. Executor 서비스에서 여러 결과를 효율적으로 처리하는 방법
- 21. 여러 개의 열린 창을 처리하는 방법
- 22. SolrNet 초보자 - 여러 Where 절을 처리하는 방법
- 23. mvc3에서 여러 유효성 검사 요약을 처리하는 방법
- 24. 한 필드에 여러 입력 인스턴스를 처리하는 방법
- 25. 여러 ASIFormDataRequest - 아이폰 개발을 처리하는 방법
- 26. 웹 사이트에서 여러 언어를 처리하는 방법
- 27. Carrierwave : 동일한 유형의 여러 업로드를 처리하는 방법
- 28. 여러 페이지에 사용되는 양식을 처리하는 방법?
- 29. PHP 폼에서 여러 체크 박스를 처리하는 방법?
- 30. 하나의 ListSelectionEvent로 여러 개의 jLists를 처리하는 방법
감사합니다.하지만이 코드는 delphi 2007에서 작동합니다. –
알고있는 한. –
하지만 VCL 양식 응용 프로그램으로 사용하려합니다. –