---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
前言:不能Run(),一切都白搭。这个方法将实现这个程序所介绍的功能:文件批量重命名
public override bool Run()
{
// 如果所有输入正确,执行操作
if (AllGreen())
{
Message.Text = string.Format("*消息:任务进行中.....");
string[] strs = Directory.GetFiles(DirectoryPath.Text, SpecifiedType.Text); //获取指定目录下指定类型的文件
string st = SpecifiedType.Text; //文件类型
string[] fileNames = CustomName.Text.Split(new string[] { brackets }, StringSplitOptions.None); // 分割文件名格式
if (fileNames.Length != 2) // 如果分割后的文件名数量不为2,抛出异常
{
string exceptionMessage = string.Format("fileNames.Length == {0}", fileNames.Length);
throw new ApplicationException(exceptionMessage);
}
string places = new string('0', n2); //表示序号位数
List<int> indexOfAlreadyRename = new List<int>(); //表示已经重命名好的文件名的Index
for (int i = n1; i < strs.Length + n1; i++) //冒泡对比,筛选出已经重命名好的文件名,并匹配到strs对应的位置
{
for (int j = 0; j < strs.Length; j++)
{
if (fileNames[0] + i.ToString(places) + fileNames[1] == Path.GetFileNameWithoutExtension(strs[j]))
{
string temp = strs[i - n1];
strs[i - n1] = strs[j];
strs[j] = temp;
indexOfAlreadyRename.Add(i - n1);
break;
}
}
}
string[] newStrs = new string[strs.Length]; //用于存储重命名后的文件名
int indexOfList = 0; //indexOfAlreadyRename的索引
for (int i = 0; i < strs.Length; i++) //开始重命名
{
if (indexOfAlreadyRename.Count > 0 && i == indexOfAlreadyRename[indexOfList])
{
if (indexOfList < indexOfAlreadyRename.Count - 1)
{
indexOfList++;
}
continue;
}
newStrs[i] = Path.GetDirectoryName(strs[i]); //先拿到路径的目录信息
newStrs[i] =
Path.Combine(newStrs[i], fileNames[0] + (i + n1).ToString(places) + fileNames[1] + st.Substring(st.IndexOf('.'))); //合并目录名和用户定义的文件名
File.Move(strs[i], newStrs[i]); // 重命名
//Message.Text = string.Format("*任务进度:{0}", (i + 1) / strs.Length);
}
Message.Text = string.Format("*消息:任务完成。成功重命名{0}个文件。", strs.Length);
return true;
}
else
{
if (DirectoryPath.Text == null)
{
Message.Visibility = true;
Message.Color = ColorString.TextColor.Red;
Message.Text = "*提醒:①不能为空!";
}
else if (SpecifiedType.Text == null)
{
Message.Visibility = true;
Message.Color = ColorString.TextColor.Red;
Message.Text = "*提醒:②不能为空!";
}
else if (CustomName.Text == null)
{
Message.Visibility = true;
Message.Color = ColorString.TextColor.Red;
Message.Text = "*提醒:③不能为空!";
}
return false;
}
}
/// <summary>
/// 检查所有输入是否完全正确。如果检查到输入错误,弹出消息提醒用户
/// </summary>
/// <returns>全部输入是否完全正确</returns>
public override bool AllGreen()
{
if (DirectoryPath.Color == ColorString.TextColor.Black && DirectoryPath.Text != "" && DirectoryPath.Text != null &&
SpecifiedType.Color == ColorString.TextColor.Black && SpecifiedType.Text != "" && SpecifiedType.Text != null &&
CustomName.Color == ColorString.TextColor.Black && CustomName.Text != "" && CustomName.Text != null)
{
Message.Color = ColorString.TextColor.Orange;
Message.Text = "*消息:准备就绪!";
Message.Visibility = true;
return true;
}
else
{
if (DirectoryPath.Color == ColorString.TextColor.Red)
{
CheckTheDirectory();
}
else if (SpecifiedType.Color == ColorString.TextColor.Red)
{
CheckSpecifiedType();
}
else if (CustomName.Color == ColorString.TextColor.Red)
{
CheckCustomName();
}
else
{
Message.Visibility = false;
}
return false;
}
}
因为已经重命名好的文件不需要再重新重命名,只需要把它交换到在strs中对应的位置,在重命名的时候根据
if (indexOfAlreadyRename.Count > 0 && i == indexOfAlreadyRename[indexOfList])
{
if (indexOfList < indexOfAlreadyRename.Count - 1)
{
indexOfList++;
}
continue;
}
判断strs[i]是否需要重命名即可。
例子:
最后PNG格式的文件没有被重命名。
有趣的地方:
注意乔布斯和那个星星,现在我保留原来的命名格式,只修改序号从2开始
你一定会惊讶地发现,除了乔布斯的序号从001变成了014,其他图片并没有重命名!
当然我不敢说这个特性一定是好的,但这确实很有趣,不是吗?
思考:实际上在做这个小项目的时候,有超过30%的知识是我以前没学过的,其中最难的是解决数据绑定中类型转换的问题和容错算法,它们差点让我放弃,那时我很焦虑。但我最终决定静下心,结合以往学习的经验,在网上不断搜索资料,反复调试,直到解决问题。
在这次经历中,我学会了遇到困难要保持冷静的头脑,结合过去的经验和冷静的思考,最后答案就会浮现出来。
好了,软件的介绍暂告一段落,这是源代码的下载链接One Click源代码,里面的一些被注释的代码是我砍掉的功能,如果你感兴趣,可以研究一下。
如果你想与我交流.net的学习,请联系我QQ:2961117975
---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
详细请查看:<a href="http://www.itheima.com" target="blank">www.itheima.com</a>