dwg ファイルでいっぱいのフォルダーがあるので、ファイルの最新バージョンを見つけるか、ファイルにバージョンがない場合はそれをディレクトリにコピーするだけです。たとえば、次の 3 つのファイルがあります。
ABBIE 08-10 #6-09H4 最終 06-12-2012.dwg
ABBIE 08-10 #6-09H4 最終 06-12-2012_1.dwg
ABBIE 08-10 #6-09H4 最終 06-12-2012_2.dwg
違いは、あるファイルには があり_1
、別のファイルには がある_2
ことに注意してください。したがって、ここの最新のファイルは_2
. 最新のファイルを保持し、ディレクトリにコピーする必要があります。一部のファイルには異なるバージョンがないため、コピーできます。多くの場合、それらは同じであるため、ファイルの作成日または変更日に焦点を当てることはできず、ファイル名自体を確認するだけです。これを行うには、以下に投稿する方法よりも効率的な方法があると確信しています。
DirectoryInfo myDir = new DirectoryInfo(@"H:\Temp\Test");
var Files = myDir.GetFiles("*.dwg");
string[] fileList = Directory.GetFiles(@"H:\Temp\Test", "*FINAL*", SearchOption.AllDirectories);
ArrayList list = new ArrayList();
ArrayList WithUnderscores = new ArrayList();
string nameNOunderscores = "";
for (int i = 0; i < fileList.Length; i++)
{
//Try to get just the filename..
string filename = fileList[i].Split('.')[0];
int position = filename.LastIndexOf('\\');
filename = filename.Substring(position + 1);
filename = filename.Split('_')[0];
foreach (FileInfo allfiles in Files)
{
var withoutunderscore = allfiles.Name.Split('_')[0];
withoutunderscore = withoutunderscore.Split('.')[0];
if (withoutunderscore.Equals(filename))
{
nameNOunderscores = filename;
list.Add(allfiles.Name);
}
}
//If there is a number after the _ then capture it in an ArrayList
if (list.Count > 0)
{
foreach (string nam in list)
{
if (nam.Contains("_"))
{
//need regex to grab numeric value after _
var match = new Regex("_(?<number>[0-9]+)").Match(nam);
if (match.Success)
{
var value = match.Groups["number"].Value;
var number = Int32.Parse(value);
WithUnderscores.Add(number);
}
}
}
int removedcount = 0;
//Whats the max value?
if (WithUnderscores.Count > 0)
{
var maxval = GetMaxValue(WithUnderscores);
Int32 intmax = Convert.ToInt32(maxval);
foreach (FileInfo deletefile in Files)
{
string shorten = deletefile.Name.Split('.')[0];
shorten = shorten.Split('_')[0];
if (shorten == nameNOunderscores && deletefile.Name != nameNOunderscores + "_" + intmax + ".dwg")
{
//Keep track of count of Files that are no good to us so we can iterate to next set of files
removedcount = removedcount + 1;
}
else
{
//Copy the "Good" file to a seperate directory
File.Copy(@"H:\Temp\Test\" + deletefile.Name, @"H:\Temp\AllFinals\" + deletefile.Name, true);
}
}
WithUnderscores.Clear();
list.Clear();
}
i = i + removedcount;
}
else
{
//This File had no versions so it is good to be copied to the "Good" directory
File.Copy(@"H:\Temp\SH_Plats\" + filename, @"H:\Temp\AllFinals" + filename, true);
i = i + 1;
}
}