public ChangeFileName(CancellationTokenSource _tokenSource, ICloneable _threadParameter) : base(_tokenSource, _threadParameter) { if (this.ThreadParameter != null) { param = (ChangeFileNameParameter)this.ThreadParameter; } //amount = 0; }
public ChangeFileNameParameter GetChangeFileNameParameter() { ChangeFileNameParameter rtn = new ChangeFileNameParameter(); rtn.SetOriginalRootPath(this.txtOriginalDir.Text.Trim()); rtn.SetOutputRootPath(this.txtOutputDir.Text.Trim()); return(rtn); }
/// <summary> /// recursive call by self /// </summary> /// <returns></returns> public override T RunSubThread(ICloneable thparam) { //judage if threading is cancelled. this.IsTaskCanceled(); bool IsNameChanged = true; ChangeFileNameParameter param; if (thparam != null) { param = (ChangeFileNameParameter)thparam; } else { return(default(T)); } //Directory Change DirectoryInfo outputFold = new DirectoryInfo(param.OutputRootPath); string newOutputPath = ConvertToValidName(outputFold.Name); IsNameChanged = !(newOutputPath == outputFold.Name); if (IsNameChanged) { outputFold = new DirectoryInfo(param.OutputRootPath.Substring(0, param.OutputRootPath.Length - outputFold.Name.Length) + newOutputPath); } //outputFold. if (!System.IO.Directory.Exists(outputFold.FullName)) { try { outputFold.Create(); } catch { LoggerHelper.Warn("Program cann't create fold:" + outputFold.FullName + "\r\n"); return(default(T)); } if (!System.IO.Directory.Exists(outputFold.FullName)) { LoggerHelper.Warn("Program cann't create fold:" + outputFold.FullName + "\r\n"); return(default(T)); } } //if (IsValidName(outputFold.Name)) //{ // successamount++; // SuccessRecorder.Record(FormatHistory(successamount,EventSet.DirectoryEvent, IsNameChanged, param.OriginalRootPath, outputFold.FullName, "directory event")); //} //else //{ // failamount++; // FailRecorder.Record(FormatHistory(failamount, EventSet.DirectoryEvent, IsNameChanged, param.OriginalRootPath, outputFold.FullName, "directory event")); //} successamount++; SuccessRecorder.Record(FormatHistory(successamount, EventSet.DirectoryEvent, IsNameChanged, param.OriginalRootPath, outputFold.FullName, "directory event")); PrintProcess(failamount, successamount); //File Name Change DirectoryInfo originalFold = new DirectoryInfo(param.OriginalRootPath); FileInfo[] OriginalFileList = originalFold.GetFiles(); foreach (var file in OriginalFileList) { string originalFileName = file.Name; string newFileName = ConvertToValidName(originalFileName); IsNameChanged = !(originalFileName == newFileName); //rename file if (!System.IO.File.Exists(outputFold.FullName + "\\" + newFileName)) { try { System.IO.File.Copy(file.FullName, outputFold.FullName + "\\" + newFileName, true); } catch (UnauthorizedAccessException ex) { LoggerHelper.Warn("File[" + file.FullName + "] cann't be renamed\r\n"); //System.IO.File.Copy(file.FullName, outputFold.FullName + "\\" + newFileName, true); } } else { // FileInfo f = new FileInfo(outputFold.FullName + "\\" + newFileName); if (f.Length != file.Length) { //It's not the same file if (f.IsReadOnly) { f.IsReadOnly = false; } try { System.IO.File.Copy(file.FullName, outputFold.FullName + "\\" + newFileName, true); } catch (UnauthorizedAccessException ex) { LoggerHelper.Warn("File[" + file.FullName + "] cann't be renamed\r\n"); //System.IO.File.Copy(file.FullName, outputFold.FullName + "\\" + newFileName, true); } } } //if (IsValidName(newFileName)) //{ //} //else //{ // failamount++; // FailRecorder.Record(FormatHistory(failamount, EventSet.FileEvent, IsNameChanged, file.FullName, outputFold.FullName + "\\" + newFileName, file.DirectoryName)); //} successamount++; SuccessRecorder.Record(FormatHistory(successamount, EventSet.FileEvent, IsNameChanged, file.FullName, outputFold.FullName + "\\" + newFileName, file.DirectoryName)); PrintProcess(failamount, successamount); } //recursive Sub Directory foreach (DirectoryInfo dir in originalFold.GetDirectories()) { ChangeFileNameParameter paramSub = (ChangeFileNameParameter)param.Clone(); paramSub.SetOriginalRootPath(dir.FullName); paramSub.SetOutputRootPath(outputFold.FullName + "\\" + dir.Name); RunSubThread(paramSub); } return(default(T)); }