2012-04-26 15 views
0

Directory.GetFilesを使用して、コピーされるファイルを検索しています。私はコピーを使用することができるようにファイルのパスを見つける必要がありますが、パスを見つける方法はわかりません。 ファイルを繰り返し処理しますが、ファイルのソースパスが必要なためコピーまたは移動できません。ディレクトリツリーの配列からのパス

string[] files = Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories); 

System.Console.WriteLine("Files Found"); 

// Display all the files. 
foreach (string file in files) 
{ 
    string extension = Path.GetExtension(file); 
    string thenameofdoom = Path.GetFileNameWithoutExtension(file); 
    string filename = Path.GetFileName(file); 

    bool b = false; 
    string newlocation = (@"\\TEST12CVG\Public\Posts\Temporaryjunk\"); 

    if (extension == ".pst" || 
    extension == ".tec" || 
    extension == ".pas" || 
    extension == ".snc" || 
    extension == ".cst") 
    { 
    b = true; 
    } 

    if (thenameofdoom == "Plasma" || 
    thenameofdoom == "Oxygas" || 
    thenameofdoom == "plasma" || 
    thenameofdoom == "oxygas" || 
    thenameofdoom == "Oxyfuel" || 
    thenameofdoom == "oxyfuel") 
    { 
    b = false; 
    } 

    if (b == true) 
    { 
    File.Copy(file, newlocation + thenameofdoom); 
    System.Console.WriteLine("Success: " + filename); 
    b = false; 
    } 
} 
+0

それはより多くを持っていますが、私は必要だったものを入れて、私はそれを必要とします私が働いている私の仕事のために、私は必要ではなく、後でより早くです。 – shred1894

+0

'Path.GetDirectoryName(filename)'を意味しますか? – mellamokb

+0

ファイルが入っているフォルダだけでなく、パス全体が必要です。 – shred1894

答えて

1

Path.GetFullPath作品が、また、それは多くのファイルヘルパーメソッドに付属しているようFileInfoを使用することを検討してください:

これは私が持っているものです。私はあなたが拡張子をフィルタリングしていることに気づいたが、

私は()...キャッチを試みる扱う多くのエラーを使用することができます(これと同様の方法を使用しますが、それは良いスタートだ

EDITそれらを必要とし、コードへの更新は次のようにコールしている

class BackupOptions 
{ 
    public IEnumerable<string> ExtensionsToAllow { get; set; } 
    public IEnumerable<string> ExtensionsToIgnore { get; set; } 
    public IEnumerable<string> NamesToIgnore { get; set; } 
    public bool CaseInsensitive { get; set; } 

    public BackupOptions() 
    { 
    ExtensionsToAllow = new string[] { }; 
    ExtensionsToIgnore = new string[] { }; 
    NamesToIgnore = new string[] { }; 
    } 
} 

static void Backup(string sourcePath, string destinationPath, BackupOptions options = null) 
{ 

    if (options == null) 
    optionns = new BackupOptions(); 

    string[] files = Directory.GetFiles(sourcePath, ".", SearchOption.AllDirectories); 
    StringComparison comp = options.CaseInsensitive ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture; 

    foreach (var file in files) 
    { 
    FileInfo info = new FileInfo(file); 

    if (options.ExtensionsToAllow.Count() > 0 && 
     !options.ExtensionsToAllow.Any(allow => info.Extension.Equals(allow, comp))) 
     continue; 

    if (options.ExtensionsToIgnore.Any(ignore => info.Extension.Equals(ignore, comp))) 
     continue; 

    if (options.NamesToIgnore.Any(ignore => info.Name.Equals(ignore, comp))) 
     continue; 

    try 
    { 
     File.Copy(info.FullName, destinationPath + "\\" + info.Name); 
    } 
    catch (Exception ex) 
    { 
     // report/handle error 
    } 
    } 
} 

することができます:。

var options = new BackupOptions 
{ 
    ExtensionsToAllow = new string[] { ".pst", ".tec", ".pas", ".snc", ".cst" }, 
    NamesToIgnore = new string[] { "Plasma", "Oxygas", "Oxyfuel" }, 
    CaseInsensitive = true 
}; 

Backup("D:\\temp", "D:\\backup", options); 
+0

ファイルはサーバ上にあり、別のサーバに置かれます。エリックが私に働いてくれたと思います(これは例外ではありません。私はソートアウトする必要がありますアクセス許可の問題があります。本当にエラー処理が必要なこと以外はありません。 – shred1894

+0

@ shred1894 Path.GetDirectoryName(file)は、私が提供したコードよりも優れていません。 UNCパスでの作業、つまり '\\ servername \ ... 'はコードに関係なく、このコードも同様に動作します。実行可能な任意のソリューションでは、プロセスには、ローカルファイルまたはリモートファイルに関係なく、適切な読み取りと書き込みの権限が必要です。 – payo

+0

@ shred1894私が 'FileInfo'を使用しているのは、非常に簡単にパスと名前と拡張子を取得するための多くの優れたヘルパーメソッドがあるためです。 – payo