2013-02-07 19 views
0

我有一些文件看起來像這樣:重命名一個文件,如果子之間的文件名,如果結果是一個數字

prtx010.prtx010.0199.785884.351.05042413

prtx010.prtx010.0199.123456.351.05042413

prtx010.prtx010.0199.122566.351.05042413

prtx010.prtx010.0199.this.351.05042413

prtx010.prtx010.0199.s omething.351.05042413

現在,我想串那些文件,這樣我得到下面的結果

(這是左21右-12 )

事情是,我只想在這些文件之間的子字符串之間的位置,只有他們是數字和6位數字NG。

任何想法如何完成這個感激地收到。

目前,這是我的,但它的所有個子串的文件:

//Rename all files 
DirectoryInfo di = new DirectoryInfo(@"\\prod\abc"); //location of files 
DirectoryInfo di2 = new DirectoryInfo(@"\\prod\abc\");//where they are going 
string lstrSearchPattern = "prtx010.prtx010.0199."; 
foreach (FileInfo fi in di.GetFiles(lstrSearchPattern + "*")) 

{ 
    string newName = fi.Name.Substring(lstrSearchPattern.Length, 6); 
    fi.MoveTo(di2 + newName); 

    //do something with the results 
} 
di = null; 

回答

0

然後newName將包含六個字符你感興趣例如,它可能包含122566,或。它可能包含this.3。您可以使用正則表達式來確定它是否是數字:

if (Regex.Matches(newName, @"^\d{6}$")) 
{ 
    // The string is numeric. 
} 

其實,這是不完全正確,因爲期後的圖案可能是123456abc。您要查找的是您的前綴字符串,後跟一個句點,六位數字和另一個句點。所以你想要的是:

Regex re = new Regex(@"$prtx010\.prtx010\.0199\.(\d{6})\.)"); 
Match m = re.Match(fi.Name); 
if (m.Success) 
{ 
    // The string is 6 digits 
    string newName = m.Groups[1].Value; 
} 
+0

嗨吉姆,非常感謝您花時間看我的問題。我從來沒有想過使用正則表達式,它是非常有意義的。 。 。 。除非我不完全確定如何在代碼中使用您的示例來完成我想要的操作? – user2050866

+0

@ user2050866:我向您展示瞭如何從文件名中可靠地獲取6位數的字符串。我以爲這就是你要求的。如果這不是你想要做的,那麼你需要在你的問題中提供一個更好的解釋。 –

+0

嗨,是的,對不起。你的代碼就是我想要實現的。 。 。 。我的問題是,我不知道如何使它工作?也就是說,將你的代碼應用到我的目錄中?希望有點更有意義嗎? – user2050866

相關問題