如何使用c#在多个文件夹中搜索文件名中具有特定子字符串的文件?
我想知道如何修改我下面的代码,以便根据文件名确定是否复制了文件。例如,文件名可能类似于“Smith,#10 FINAL 08-20-2010.dwg”,如果文件已重新提交,则其将类似于“Smith,#10 FINAL 08-20-2010#u 1.dwg”或“Smith,#10 FINAL 08-20-2010#u 2.dwg”等。。。请注意_1、_2等。我需要能够确定该文件是否有_1、_2、_3等,并仅复制具有最大结束值的文件(假设它有一个)。我只是不知道如何确定这一点。非常感谢如何使用c#在多个文件夹中搜索文件名中具有特定子字符串的文件?,c#,linq,C#,Linq,我想知道如何修改我下面的代码,以便根据文件名确定是否复制了文件。例如,文件名可能类似于“Smith,#10 FINAL 08-20-2010.dwg”,如果文件已重新提交,则其将类似于“Smith,#10 FINAL 08-20-2010#u 1.dwg”或“Smith,#10 FINAL 08-20-2010#u 2.dwg”等。。。请注意_1、_2等。我需要能够确定该文件是否有_1、_2、_3等,并仅复制具有最大结束值的文件(假设它有一个)。我只是不知道如何确定这一点。非常感谢 //Now
//Now lets start searching the entire ARCHIVE folder/subfolders for a DWG that has
//this Well_Name and Actual_Date with FINAL in File Name...
DirectoryInfo myDir = new DirectoryInfo(@"C:\G-M");
//Collect the Final, Approved DWGs only...
var files = myDir.GetFileSystemInfos().Where(f => f.Name.Contains("FINAL") || f.Name.Contains(drow["Well_Name"].ToString()) || f.Name.Contains(drow["Actual_Date"].ToString()));
//Copy to directory...
foreach (FileInfo file in files)
{
//Lets loop through and check to see that the File has greatest value if it contains _1, _2, etc..
System.IO.File.Copy(file.FullName, @"C:\FINAL" + file, true);
}
为此使用正则表达式 因此: 编辑:一个想法是:
System.Text.RegularExpressions.Regex myFileRegex = new Regex(@"_(\d+)\.dwg", RegexOptions.IgnoreCase);
SortedDictionary<string, int> fileFromNumber= new SortedDictionary<int, string>();
foreach (string filePath in Direcoty.GetFiles(@"you directory path")
{
Match match = myFileRegex.Match(fileName);
if (match.Groups.Count > 1)
{
fileFromNumber.Add(Convert.ToInt32(match.Groups[1].Value), filePath);
}
}
string newestFile = fileFromNumber[fileFromNumber.Count - 1];//the last element
System.Text.RegularExpressions.Regex myFileRegex=new Regex(@“\ud+\.dwg”,RegexOptions.IgnoreCase);
SortedDictionary fileFromNumber=新的SortedDictionary();
foreach(Direcoty.GetFiles(@“您的目录路径”)中的字符串文件路径)
{
Match Match=myFileRegex.Match(文件名);
如果(match.Groups.Count>1)
{
fileFromNumber.Add(Convert.ToInt32(match.Groups[1].Value),filePath);
}
}
字符串newestFile=fileFromNumber[fileFromNumber.Count-1];//最后一个元素
此代码应提供您所需的一切:
void Main()
{
// this is your base scenario
IEnumerable<FileInfo> fileInfos = new List<FileInfo>{new FileInfo("a_1"), new FileInfo("c"), new FileInfo("a"), new FileInfo("b"), new FileInfo("b_1000")};
// now use the new class
IEnumerable<ComparableFileInfo> cFileInfos = fileInfos.Select(x => new ComparableFileInfo(x));
var nameGroups = cFileInfos.GroupBy(x => x.BaseName).Select(group => new {group.Key, MaxFile = group.Max()});
foreach (var g in nameGroups.OrderBy(x => x.Key))
{
Console.WriteLine(g.MaxFile.OrigFileInfo.FullName);
}
}
class ComparableFileInfo : IComparable{
public FileInfo OrigFileInfo; // omit getters and setters for brevity
public int FileVersion;
public string BaseName;
public ComparableFileInfo(FileInfo fileInfo){
this.OrigFileInfo = fileInfo;
int splitIdx = fileInfo.Name.LastIndexOf("_");
this.BaseName = splitIdx < 0 ? fileInfo.Name : fileInfo.Name.Substring(0, splitIdx);
string version = fileInfo.Name.Substring(splitIdx+1, fileInfo.Name.Length - splitIdx -1);
int versionCast;
this.FileVersion = Int32.TryParse(version, out versionCast) ? versionCast : 0;
}
public int CompareTo(object obj)
{
if (obj is ComparableFileInfo)
{
ComparableFileInfo f2 = (ComparableFileInfo)obj;
return this.FileVersion.CompareTo(f2.FileVersion);
}
else
throw new ArgumentException("Object is not a ComparableFileInfo");
}
}
void Main()
{
//这是您的基本场景
IEnumerable fileInfos=新列表{new FileInfo(“a_1”)、new FileInfo(“c”)、new FileInfo(“a”)、new FileInfo(“b”)、new FileInfo(“b_1000”)};
//现在使用新类
IEnumerable cFileInfos=fileInfos.Select(x=>newcompariablefileinfo(x));
var nameGroups=cFileInfos.GroupBy(x=>x.BaseName).Select(group=>new{group.Key,MaxFile=group.Max());
foreach(nameGroups.OrderBy(x=>x.Key)中的变量g)
{
Console.WriteLine(g.MaxFile.OrigFileInfo.FullName);
}
}
类可比较文件信息:i可比较{
public FileInfo OrigFileInfo;//为了简洁起见,省略getter和setter
公共int文件版本;
公共字符串基名称;
公共可比文件信息(文件信息文件信息){
this.OrigFileInfo=fileInfo;
int splitIdx=fileInfo.Name.LastIndexOf(“”);
this.BaseName=splitIdx<0?fileInfo.Name:fileInfo.Name.Substring(0,splitIdx);
string version=fileInfo.Name.Substring(splitIdx+1,fileInfo.Name.Length-splitIdx-1);
int versionCast;
this.FileVersion=Int32.TryParse(version,out versionCast)?versionCast:0;
}
公共整数比较(对象对象对象)
{
if(obj为可比文件信息)
{
可比文件信息f2=(可比文件信息)obj;
返回此.FileVersion.CompareTo(f2.FileVersion);
}
其他的
抛出新ArgumentException(“对象不是可比较的FileInfo”);
}
}
输出为:
C:\Users\xxx\Desktop\a\u 1
C:\Users\xxx\Desktop\b\u 1000
C:\Users\xxx\Desktop\C@cudahead-Hi cudahead,我看到您的代码中使用了一个名为“names”的字符串数组。我有一个名为dtResult的数据表,所以我应该将其转换为一个列表,或者您建议我如何实现您的代码?@Josh您可以这样做:
var names=dtResult.AsEnumerable()。选择(x=>x.Field(“yourColumn”));
我在第let version=Convert.ToInt32行(name.Substring(splitIdx+1,name.Length-splitIdx-1))中得到“输入字符串格式不正确”,我刚刚意识到,您也可以在名称末尾不带“\ux”,对不对(如果没有版本)?@cudahead-实际上如果它包含“\ux”接近尾端时,它将始终是一个数字,例如(“\u 1”或“\u 2”或“\u 3”等)。因此,为了反映有时文件名可能只是类似于Harry#10 2-08-2001.dwg,如果该文件有其他版本,那么您可能会有类似于Harry#10 2-09-2002#1.dwg或Harry#10 2-09-2002#2.dwg等的文件。因此,不,如果没有版本,那么您将只会看到类似于Harry#10 2-09-2002.dwgHi Jalal的文件名,我发布的代码我的问题是在一个数据表的foreach循环中。我在每一行中循环,每一行都包含大部分文件名(例如“Smit,#10 FINAL 08-20-2010.dwg”)。它显然没有考虑到“#1”或“#2”等,如果它们存在的话。我如何将它们合并到代码中?
void Main()
{
// this is your base scenario
IEnumerable<FileInfo> fileInfos = new List<FileInfo>{new FileInfo("a_1"), new FileInfo("c"), new FileInfo("a"), new FileInfo("b"), new FileInfo("b_1000")};
// now use the new class
IEnumerable<ComparableFileInfo> cFileInfos = fileInfos.Select(x => new ComparableFileInfo(x));
var nameGroups = cFileInfos.GroupBy(x => x.BaseName).Select(group => new {group.Key, MaxFile = group.Max()});
foreach (var g in nameGroups.OrderBy(x => x.Key))
{
Console.WriteLine(g.MaxFile.OrigFileInfo.FullName);
}
}
class ComparableFileInfo : IComparable{
public FileInfo OrigFileInfo; // omit getters and setters for brevity
public int FileVersion;
public string BaseName;
public ComparableFileInfo(FileInfo fileInfo){
this.OrigFileInfo = fileInfo;
int splitIdx = fileInfo.Name.LastIndexOf("_");
this.BaseName = splitIdx < 0 ? fileInfo.Name : fileInfo.Name.Substring(0, splitIdx);
string version = fileInfo.Name.Substring(splitIdx+1, fileInfo.Name.Length - splitIdx -1);
int versionCast;
this.FileVersion = Int32.TryParse(version, out versionCast) ? versionCast : 0;
}
public int CompareTo(object obj)
{
if (obj is ComparableFileInfo)
{
ComparableFileInfo f2 = (ComparableFileInfo)obj;
return this.FileVersion.CompareTo(f2.FileVersion);
}
else
throw new ArgumentException("Object is not a ComparableFileInfo");
}
}