2016-08-26 5 views
0

Inno Setup에서 지정된 파일 확장명을 가진 디렉토리의 파일 수를 계산하고 싶습니다. 나는 다음과 같은 코드를 작성했다.Inno Setup 지정된 파일 확장자를 가진 디렉토리의 파일 수를 확인하십시오.

감사합니다,

function AmountOfFilesInDir(const DirName, Extension: String): Integer; 
var 
    FilesFound: Integer; 
    ShouldBeCountedUppercase, ShouldBeCountedLowercase: Boolean; 
    FindRec: TFindRec; 
begin 
    FilesFound := 0; 
    if FindFirst(DirName, FindRec) then begin 
    try 
     repeat 
     if (StringChangeEx(FindRec.Name, Lowercase(Extension), Lowercase(Extension), True) = 0) then 
      ShouldBeCountedLowercase := False 
     else 
      ShouldBeCountedLowercase := True; 
     if (StringChangeEx(FindRec.Name, Uppercase(Extension), Uppercase(Extension), True) = 0) then 
      ShouldBeCountedUppercase := False 
     else 
      ShouldBeCountedUppercase := True; 
     if (FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0) 
      and ((ShouldBeCountedUppercase = True) or (ShouldBeCountedLowercase = True)) then begin 
      FilesFound := FilesFound + 1; 
     end; 
     until not FindNext(FindRec); 
    finally 
     FindClose(FindRec); 
    end; 
    end; 
    Result := FilesFound; 
end; 

과 사용 예제가 될 수는 :

Log(IntToStr(AmountOfFilesInDir(ExpandConstant('{sys}\*'), '.exe'))); 

나는 비트 길이 보이는 것이 더 전문 보이게하기 위해이 기능의 코드 라인을 줄일 것을 좋아합니다. 이 기능을 실패하지 않고 어떻게 할 수 있는지 알아야합니다.

미리 감사드립니다.

답변

1

FindFirst function 자체에 와일드 카드 (확장)를 기반으로 파일을 선택할 수 있습니다

function AmountOfFiles(PathWithMask: string): Integer; 
var 
    FindRec: TFindRec; 
begin 
    Result := 0; 
    if FindFirst(PathWithMask, FindRec) then 
    begin 
    try 
     repeat 
     Inc(Result); 
     until not FindNext(FindRec); 
    finally 
     FindClose(FindRec); 
    end; 
    end; 
end; 

이 좋아 사용

Log(IntToStr(AmountOfFiles(ExpandConstant('{sys}\*.exe')))); 
+0

이 감사합니다 ........! .... 당신은 그것을 짧게 단축했습니다 ......... – GTAVLover

관련 문제