2013-02-07 18 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

相关问题