2012-03-13 2 views
2

나는 약 500 시간을 보냈다. MSDN 문서를 읽으면 여전히 원하는대로 작동하지 않는다. 나는이 같은 파일 이름으로FileSystemInfo [] 이름으로 정렬

정렬 할 수 있습니다

01.png 
02.png 
03.png 
04.png 

을 즉 모두 동일한 파일 길이.

두 번째 파일 길이가 긴 파일이 모두 지옥에갑니다. 순서의 예를 들어

:

1.png 
2.png 
3.png 
4.png 
5.png 
10.png 
11.png 

그것은 읽기 :이 원하지 않는

1.png, 2.png then 10.png, 11.png 

.

내 코드 :

그것은 특히 파일 길이의 문제가 아니다
DirectoryInfo di = new DirectoryInfo(directoryLoc); 
FileSystemInfo[] files = di.GetFileSystemInfos("*." + fileExtension); 
Array.Sort<FileSystemInfo>(files, new Comparison<FileSystemInfo>(compareFiles)); 

foreach (FileInfo fri in files) 
{ 
    fri.MoveTo(directoryLoc + "\\" + prefix + "{" + operationNumber.ToString() + "}" + (i - 1).ToString("D10") + 
     "." + fileExtension); 

    i--; 
    x++; 
    progressPB.Value = (x/fileCount) * 100; 
} 

// compare by file name 
int compareFiles(FileSystemInfo a, FileSystemInfo b) 
{ 
    // return a.LastWriteTime.CompareTo(b.LastWriteTime); 
    return a.Name.CompareTo(b.Name); 
} 
+0

시나리오에서 파일 이름 패턴을 변경할 수 있습니까? E.G. 1.png에서 01.png까지? –

+1

시도해보십시오. http://stackoverflow.com/questions/1601834/c-implementation-of-or-alternative-to-strcmplogicalw-in-shlwapi-dll,'StrCmpLogicalW'는 정렬의 "마법"을 수행하는 Windows API입니다 "논리적 인"방식으로 파일 이름. – xanatos

답변

3

- 그것은 사전 식 순서로 비교되는 이름의 문제이다.

확장자가없는 이름을 가져 와서 정수로 구문 분석하고 두 이름을 그렇게 비교하는 것처럼 들리는 경우가 있습니다. 실패 할 경우 사전 식 순서로 되돌릴 수 있습니다.

물론 "debug1.png, debug2.png, ... debug10.png"가있는 경우에는 작동하지 않습니다.이 경우 더 복잡한 알고리즘이 필요합니다.

0

코드가 정확하고 예상대로 작동합니다. 정렬 작업은 숫자가 아닌 알파벳순으로 수행됩니다.

예를 들어, 문자열 "1", "10", "2"는 알파벳 순서입니다. 대신 파일 이름이 항상 ".png"에 더하여 숫자 일 뿐이라는 것을 알고 계시면 숫자로 정렬 할 수 있습니다. 예를 들어,이 같은 :

int compareFiles(FileSystemInfo a, FileSystemInfo b)   
{    
    // Given an input 10.png, parses the filename as integer to return 10 
    int first = int.Parse(Path.GetFileNameWithoutExtension(a.Name)); 
    int second = int.Parse(Path.GetFileNameWithoutExtension(b.Name)); 

    // Performs the comparison on the integer part of the filename 
    return first.CompareTo(second); 
} 
3

당신은 당신이 그 (것)들을 으로 분류합니다 (내가 있으리라 믿고있어)에도 불구하고, 문자열로 이름을 비교하고 있습니다. "10"전에 오는 곳

잘 알려진 문제가 당신이 알고있는 경우 (10)의 첫 번째 문자 (1)

(9)의 첫 번째 문자 이하이기 때문에 "9"한 파일의 모든 것 이름이 지정된 이름으로 구성되어 있으면 사용자 정의 정렬 루틴을 수정하여 이름을 정수로 변환하고 적절히 정렬 할 수 있습니다.

0

동일한 문제가 발생했지만 직접 목록을 정렬하는 대신 6 자리 '0'으로 채워진 키를 사용하여 파일 이름을 변경했습니다.당신은 파일 이름을 변경할 수없는 경우, 당신은 알파 종류를 다루는 자신의 정렬 루틴을 구현해야 할거야,

000001.jpg 
000002.jpg 
000003.jpg 
... 
000010.jpg 

을하지만 :

내 목록은 이제 다음과 같습니다.

0

주문을 수정하기 위해 linq와 정규식을 사용하는 것이 어떻습니까?

var orderedFileSysInfos = 
    new DirectoryInfo(directoryloc) 
    .GetFileSystemInfos("*." + fileExtension) 
    //regex below grabs the first bunch of consecutive digits in file name 
    //you might want something different 
    .Select(fsi => new{fsi, match = Regex.Match(fsi.Name, @"\d+")}) 
    //filter away names without digits 
    .Where(x => x.match.Success) 
    //parse the digits to int 
    .Select(x => new {x.fsi, order = int.Parse(x.match.Value)}) 
    //use this value to perform ordering 
    .OrderBy(x => x.order) 
    //select original FileSystemInfo 
    .Select(x => x.fsi) 
    //.ToArray() //maybe? 
관련 문제