只需为我正在运营呼叫中心的朋友编写以下算法,他想格式化他的文件名并根据月份和日期移动目录。但是呼叫中心有超过 350 万个文件,而且该程序似乎工作了 12 个小时,只处理了不到 20gb
那么有没有办法优化下面的算法,
class Program
{
// How much deep to scan. (of course you can also pass it to the method)
const int HowDeepToScan = 20;
static void Main(string[] args)
{
ProcessDir(@"E:\Hard Disk 2\", 1);
Console.WriteLine("Islem Bitmistir");
Console.ReadLine();
}
public static void ProcessDir(string sourceDir, int recursionLvl)
{
if (recursionLvl <= HowDeepToScan)
{
ChangeDirectories(sourceDir);
// Recurse into subdirectories of this directory.
string[] subdirEntries = Directory.GetDirectories(sourceDir);
foreach (string subdir in subdirEntries)
// Do not iterate through reparse points
if ((File.GetAttributes(subdir) &
FileAttributes.ReparsePoint) !=
FileAttributes.ReparsePoint)
ProcessDir(subdir+@"\",recursionLvl + 1);
}
}
public static void ChangeDirectories(string givenPath)
{
DataTable resultSet = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
SqlConnection callCenterConnection = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CallCenter;Data Source=.");
//Directory of mp3s
string sourceDir = givenPath;
//Get the files inside that directory
string[] fileEntries = Directory.GetFiles(sourceDir);
callCenterConnection.Open();
//Iterate through those files
foreach (string fullFileName in fileEntries)
{
//Get the file name without path and extension
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullFileName);
adapter = new SqlDataAdapter("SELECT TOP 1 ID,Time,Tel,AgentID FROM ResultTable WHERE ID=" + fileNameWithoutExtension, callCenterConnection);
adapter.Fill(resultSet);
}
adapter.Dispose();
if (resultSet.Rows.Count != 0)
{
foreach (DataRow dr in resultSet.Rows)
{
DateTime fileDate = Convert.ToDateTime(dr["Time"]);
if (!File.Exists(@"E:\Ses Dosyalari" + @"\" + fileDate.Year + @"\" + fileDate.Month + @"\" + Convert.ToString(dr["Time"]).Replace(":", ".") + " - " + Convert.ToString(dr["Tel"]) + " - " + Convert.ToString(dr["AgentID"]) + ".mp3"))
{
File.Move(sourceDir + Convert.ToString(dr["ID"]) + ".mp3", @"E:\Ses Dosyalari" + @"\" + fileDate.Year + @"\" + fileDate.Month + @"\" + Convert.ToString(dr["Time"]).Replace(":", ".") + " - " + Convert.ToString(dr["Tel"]) + " - " + Convert.ToString(dr["AgentID"]) + ".mp3");
cmd = new SqlCommand("UPDATE ResultTable SET Used = 1 WHERE ID="+Convert.ToString(dr["ID"]), callCenterConnection);
cmd.ExecuteNonQuery();
}
}
}
cmd.Dispose();
callCenterConnection.Close();
resultSet.Clear();
resultSet.Dispose();
}
}