2016-05-11 14 views
-1

私は多くのフォルダを持つディレクトリを持っています。名前は常に昇順の数字です。例ディレクトリ内の最後のディレクトリを名前で注文する

c:\folders\

は最後のフォルダ()を取得するために内部で彼らが想定ので

1 
2 
3 
4 
5 
6 
7 

、私はこれを行うだろう:

string[] folders = Directory.GetDirectories("c:\folders\", "*", 
       SearchOption.AllDirectories) 

そして、文字列の最後の要素を取ります。

しかし、このディレクトリには5000以上のフォルダがあるため、getdirectoriesは実行に約1分かかります。私はこれをやっているのはなぜ

私はソースディレクトリとターゲットディレクトリを持っています。私はすべての新しいフォルダとその内容をソースからターゲットにコピーしています(はい、バックアップ)。

string[] directories = Directory.GetDirectories(source, "*", 
        SearchOption.AllDirectories); 

foreach (string dirPath in directories) 
    Directory.CreateDirectory(dirPath.Replace(source, target)); 

foreach (string newPath in Directory.GetFiles(source, "*.*", 
    SearchOption.AllDirectories)) 
{ 
    if (!File.Exists(newPath.Replace(source, target)) && !newPath.Contains("\\Temp\\")) 
    { 
     File.Copy(newPath, newPath.Replace(source, target), false); 
    } 
} 

私は最後のフォルダを取得した後、私はディレクトリを作成し、ソースディレクトリにそれの後に来るだけで、フォルダ内のファイルをコピーする必要があります:

これは私のコードは、現在何をするかです。


どのようにして最高の名前(番号)のフォルダをより迅速に取得できますか?私は、最後に挿入されたフォルダを追跡するためにTXTファイルを作成することができましたが、変更可能な余分なファイルを持っている素晴らしいテクニックではありません。

ああ、ソースには、たとえばのような文字のフォルダがいくつか含まれています。などです。私はそれらのフォルダを無視し、番号のみの名前を持つものだけを取得したいと思います。

質問が多すぎますが、私は誰にも仕事を依頼しているわけではありません。アイデアがほしいと思っています。

答えて

0

多くの場合、私はDirectory.EnumerateDirectories(string path)が列挙にはるかに良いと聞いています。

var path = @"C:\test"; 
var highestNumberedDirectory = Directory.EnumerateDirectories(path) 
            // Gets just the folder names without the full path 
            .Select(d => ParseNumber(d.Split('\\').Last())) 
            // Orders the results by number. If you don't convert to int first then you'll start seeing some 
            // ordering you might not expect. 1, 11, 2, 22, etc. 
            .OrderBy(d=>d) 
            // Gets the highest numbered result, ignoring nulls. 
            .LastOrDefault(d => d != null); 

private int? ParseNumber(string potentialNumber) { 
    int result; 
    if (int.TryParse(potentialNumber, out result)) { 
     return result; 
    } 

    return null; 
} 

ディレクトリ構造は次のようになります。

1 
2 
3 
10 
11 
12 
20 
21 
22 
bad 
__ok 

と、それは22を返しますが、完全なパスが必要な場合は、単純に元のパスに

+0

列挙ディレクトリは.NET 4.0のみです。私は2.0を使用していることを忘れた – Phiter

+0

ああ、確かに物事を変更します:) – KSib

+0

私は今朝のいくつかの重い研究を行い、依然として希望の結果を得ることができませんでした。 – Phiter

0

それを追加することができますちょうどたIComparerを実装リスト内の最後の項目を比較する Stringcomparer

輸入システム。コレクション

公開クラス Stringcomparerは: 公開機能は、(列の)整数 実装したIComparerとして(ByValのは、文字列、文字列としてByVal yのように、X)コンペア(列の)したIComparerを実装.Compare

x = Split(x, "\")(Split(x, "\").Length - 1) 
    y = Split(y, "\")(Split(y, "\").Length - 1) 
    Dim ix As Int16 
    Dim iy As Int16 

    If Not Integer.TryParse(x, ix) Then 

     ix = -1 

    End If 

    If Not Integer.TryParse(y, iy) Then 

     iy = -1 

    End If 



    If ix > iy Then 


     Return 1 

    End If 

    If ix < iy Then 


     Return -1 

    End If 

    ' Return 0 
End Function 

エンドクラス

し、コードで

Dim FilesArray As String() 
    FilesArray = Directory.GetDirectories(path) 


    Dim ListofStrFile As New List(Of String) 
    ListofStrFile.AddRange(FilesArray) 

    ' take instance of Stringcomparer clases it to sort method 'method 
    Dim dc As New Stringcomparer 


    ListofStrFile.Sort(dc) 
関連する問題