C#文件批量重命名和多文件夹文件统一命名合并
会了几行代码,就想着能取之于民,用之于民。程序做大了服务大多数人,做小了服务专业领域的人,工作中抬头不见低头见,生活中需要用的没人会说,也没人会想到,就是想到了,yandex一个。用着太麻烦,没必要再去学习正则表达式,没必要去找人开发一个。
小孩买了个火火兔,在家长那是个播放器,资源网上都是,对于有强迫症的完美主义者,你可以往下看,没有的你把所有资源都放一块听着也一样。会电脑的一个个找,一个个比较,哪个一样删除哪个,留一个总的,不行了就everything吗;不会的,找客服,客服会直接给一个最多的,要是听腻了,我再给你一个链接,有重复的不见得就能听出来。
有强迫症的完美主义者来了,用windows电脑在网上下了几个品牌的压缩包,火火兔、jojo、讲故事等等等等,是一个品牌的也分系列,再苦不能苦教育,就是要把所有的儿歌整合到一个文件夹里,最好是不重复,反正播放器能记住播放到第几个了,再多mp3也不会每次从头放。
一、你看看这个是不是你说的功能
1.1 文件名为啥都要带序号
文件名有个格式,不说复杂的,001.小宝贝快快睡.mp3,002.虫儿飞.mp3,003.世上只有妈妈好.mp3········,三位数够用了吧。中文名称的文件排序是按拼音排序的(个别是按创建时间),内存卡都能自己更换歌曲了,没人规定“爱我你就抱抱我”要放在第一个,也没人规定每次“生日快乐歌”听完就是“世上只有妈妈好”,还有随机播放模式呢。不管咋样,我用电脑看文件的时候不能像看百家姓一样看看谁家字头多,谁家字头少吧,又不是排辈分,得打乱,具体怎么打乱看着办,总之就是歌曲的名称不能有规律的放一块,破坏艺术,影响情操。
爱因斯坦的相对论也提过,时间有次序,万物的本质没有规律。翻译过来就是,第一个文件可以是这个,第二个可以是那个,随机都是相对于第一个,第二个来说的,随机的相对就是序号:1,2,3·······。补零是为了防止1,10,11,100,101,2,20,23·······这样的姓一样,再按名字排序的乱序,倘若是有显示屏的小朋友,那她的数学老师可就不乐意这样的排序了,纯粹误人子弟的序号嘛,要是这还不如不要。
1.2 怎么把两个文件夹的文件合并,关键是有序号啊
整理好了两个文件夹的文件了
一个A:
另一个B:
这可就考验眼力了,细心的小朋友歌都不听了,就问你一共有几首歌。这也难不倒,伸出双手最多十个,完了。——别整那三岁小孩的脑筋急转弯,要整就整5岁的。
这两个文件夹有这么几个俗话说:A文件夹里有重复的,序号没重复。B文件夹里有A里有的,但是序号不对。B里还有个“我不是小公主”。
最后就要一个文件夹,十首歌。
二、准备起飞,来个草稿
标题都写C#了,直接开VS
2.1 批量重命名文件
文件夹里的所有名称格式都是三位数补零序号+.+文件名(包含后缀名),去除重复就在这干完了
2.2 合并文件夹
三、要开始秀了
代码部分不包含随机算法
3.1 拖拽获得路径,读取所有文件,计算新名称,重命名操作
没啥难度,稀罕就稀罕个拖拽获得路径
界面:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace tempXC0816
{
public partial class Form1 : Form
{
string strPath = "";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[] dir = Directory.GetFiles(strPath);
dataGridView1.Rows.Clear();
for (int i = 0; i < dir.Length; i++)
{
dataGridView1.Rows.Add(dir[i], "", "");
dataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
Random rand = new Random();
List<string> list = new List<string>();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string str = dataGridView1.Rows[i].Cells[0].Value.ToString();
str = str.Remove(0, str.LastIndexOf('\\') + 1);
string head = str.Substring(0, str.IndexOf('.'));
string data = str.Remove(0, str.IndexOf('.') + 1);
data = data.Substring(0, data.LastIndexOf('.'));
//if (data.Contains(":"))
//{
// data = data.Split(':')[0] + "(" + data.Split(':')[1] + ")";
//}
//head = rand.Next(1, 159).ToString();
string[] arr = data.Split(' ');
//while (list.Contains(head))
//{
// head = rand.Next(1, 159).ToString();
//}
list.Add(head);
data = arr[0] + "("+arr[arr.Length - 1]+")";
str = head.PadLeft(3, '0') + "." + data + ".mp3";
dataGridView1.Rows[i].Cells[1].Value = str;
dataGridView1.Rows[i].Cells[2].Value = strPath + "\\" + str;
}
}
private void button3_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string n1 = dataGridView1.Rows[i].Cells[0].Value.ToString();
string n2 = dataGridView1.Rows[i].Cells[2].Value.ToString();
//一定要打断点测试后循环
File.Move(n1, n2);
}
MessageBox.Show("完成");
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
//如果拖进来的是文件类型
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] paths = e.Data.GetData(DataFormats.FileDrop) as string[];
//得到拖进来的路径
string path = paths[0];
//路径字符串长度不为空
if (path.Length > 1)
{
//判断是文件夹吗
FileInfo fil = new FileInfo(path);
if (fil.Attributes == FileAttributes.Directory)//文件夹
{
//鼠标图标链接
e.Effect = DragDropEffects.Link;
}
else//文件
{
//鼠标图标禁止
e.Effect = DragDropEffects.None;
}
}
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
strPath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
MessageBox.Show(strPath);
}
}
}
3.2 扫描两个文件夹,提取实际文件名,比较差异文件,复制文件,追加序号命名
先分别拖拽两个文件夹到对应的选项卡,文件夹名称无所谓,分清楚谁是A谁是B就行,A作为合并后的文件夹
A组选项卡界面:
B组选项卡界面:
判断重复的核心在于:
List<string> listA;
if (!listA.Contains(str))
{
}
下面不是伪代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace tempXC0816
{
public partial class FormHuoHuoTu : Form
{
List<string> listA;
Dictionary<string, int> listB;
public FormHuoHuoTu()
{
InitializeComponent();
}
private void btnScan_Click(object sender, EventArgs e)
{
string[] dir1 = Directory.GetFiles(textBox1.Text);
string[] dir2 = Directory.GetFiles(textBox2.Text);
dataGridView1.Rows.Clear();
dataGridView2.Rows.Clear();
for (int i = 0; i < dir1.Length; i++)
{
dataGridView1.Rows.Add(dir1[i]);
dataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString();
}
for (int i = 0; i < dir2.Length; i++)
{
dataGridView2.Rows.Add(dir2[i]);
dataGridView2.Rows[i].HeaderCell.Value = (i + 1).ToString();
}
}
private void btnCalc_Click(object sender, EventArgs e)
{
listA = new List<string>();
listB = new Dictionary<string, int>();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string str = dataGridView1.Rows[i].Cells[0].Value.ToString();
str = str.Remove(0, str.LastIndexOf('\\') + 1);
//str = str.Split('.')[0] + ".mp4";
dataGridView1.Rows[i].Cells[1].Value = str;
str = str.Remove(0, str.IndexOf('.') + 1);
str = str.Substring(0, str.LastIndexOf('.'));
string name = str;
name = name.Trim();
dataGridView1.Rows[i].Cells[2].Value = name;
if (listA.Contains(name))
{
dataGridView1.Rows[i].Cells[4].Value = "重复"+ (listA.IndexOf(name)+1);
}
else
{
listA.Add(name);
}
}
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
string str = dataGridView2.Rows[i].Cells[0].Value.ToString();
str = str.Remove(0, str.LastIndexOf('\\') + 1);
//str = str.Split('.')[0] + ".mp4";
dataGridView2.Rows[i].Cells[1].Value = str;
str = str.Remove(0, str.IndexOf('.') + 1);
str = str.Substring(0, str.LastIndexOf('.'));
string name = str;
name = name.Trim();
dataGridView2.Rows[i].Cells[2].Value = name;
if (listB.Keys.Contains(name))
{
dataGridView2.Rows[i].Cells[4].Value = "重复";
}
else
{
listB.Add(name, i);
}
}
int startIndex = dataGridView1.Rows.Count+1;
string path = textBox1.Text + "\\";
for (int i = 0; i < listB.Count; i++)
{
string str = listB.ElementAt(i).Key;
bool have = false;
for (int j = 0; j < listA.Count; j++)
{
string targe = listA.ElementAt(j).ToUpper();
if (targe==str.ToUpper())
{
have = true;
break;
}
}
if (!have)
{
int row = listB.ElementAt(i).Value;
dataGridView2.Rows[row].DefaultCellStyle.BackColor = Color.Red;
string newName = startIndex.ToString("d3") + "." + str + ".mp3";
dataGridView2.Rows[row].Cells[3].Value = path + newName;
startIndex++;
}
//return;
//if (!listA.Contains(str))
//{
// int row = listB.ElementAt(i).Value;
// dataGridView2.Rows[row].DefaultCellStyle.BackColor = Color.Red;
// string newName = startIndex.ToString("d3") + "." + str + ".mp3";
// dataGridView2.Rows[row].Cells[3].Value = path+newName;
// startIndex++;
//}
}
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
dataGridView1.Rows.Clear();
dataGridView2.Rows.Clear();
if (sender.Equals(dataGridView1))
{
textBox1.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
}
else
{
textBox2.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
}
//MessageBox.Show(strPath);
}
private void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
//如果拖进来的是文件类型
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] paths = e.Data.GetData(DataFormats.FileDrop) as string[];
//得到拖进来的路径
string path = paths[0];
//路径字符串长度不为空
if (path.Length > 1)
{
//判断是文件夹吗
FileInfo fil = new FileInfo(path);
if (fil.Attributes == FileAttributes.Directory)//文件夹
{
//鼠标图标链接
e.Effect = DragDropEffects.Link;
}
else//文件
{
//鼠标图标禁止
e.Effect = DragDropEffects.None;
}
}
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
private void btnExport_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
if (dataGridView2.Rows[i].Cells[3].Value == null) continue;
string newPath = dataGridView2.Rows[i].Cells[3].Value.ToString();
string oldPath= dataGridView2.Rows[i].Cells[0].Value.ToString();
try
{
File.Copy(oldPath, newPath);
}
catch (Exception)
{
throw;
}
}
MessageBox.Show("完成!");
}
private void btnRename_Click(object sender, EventArgs e)
{
Form1 f = new Form1();
f.ShowDialog();
}
}
}
五、下载
https://download.csdn.net/download/u012619677/87759163
六、最终能修改的权
重命名的个性名称多种多样,合并扫描的文件夹内名称格式也多种多样,只要对你有用,大可留言,趁着C#还热
gshuaijun@163.com