您的位置:首页 > 其它

国利小工具,批量修改文件名

2012-02-22 15:45 190 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CPN
{
public partial class Form1 : Form
{
protected int fCount = 0;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = this.folderBrowserDialog1.SelectedPath;
}
}

private void button2_Click(object sender, EventArgs e)
{
try
{

if (string.IsNullOrEmpty(this.textBox1.Text.Trim()))
{
MessageBox.Show("对不起,路径存在错误!", "错误", MessageBoxButtons.OK);
return;
}
DirectoryInfo dif = new DirectoryInfo(this.textBox1.Text.Trim());
if (!dif.Exists)
{
MessageBox.Show("对不起,路径存在错误!", "错误", MessageBoxButtons.OK);
return;
}
else if (!string.IsNullOrEmpty(this.textBox3.Text.Trim()))
{
DirectoryInfo dif1 = new DirectoryInfo(this.textBox3.Text.Trim());
if (!dif1.Exists)
{
MessageBox.Show("对不起,路径存在错误!", "错误", MessageBoxButtons.OK);
return;
}
}
else if (string.IsNullOrEmpty(this.textBox2.Text.Trim()))
{
MessageBox.Show("对不起,项目名称不能为空!", "错误", MessageBoxButtons.OK);
return;
}
else
{
fCount = 0;
DealWithFile(this.textBox1.Text.Trim());
this.label5.Text = fCount.ToString();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "错误", MessageBoxButtons.OK);
return;
}

}
private void DealWithFile(string path)
{
DirectoryInfo dif = new DirectoryInfo(path);
DirectoryInfo[] s_dif=dif.GetDirectories();
FileInfo[] s_fif = dif.GetFiles();
if (s_fif.Length > 0)
{
string fName = "", n_fName = "";
for (int i = 0; i < s_fif.Length; i++)
{
fName = s_fif[i].Name;
if (fName.IndexOf("_") > 0)
{
//13711739184#_GDGLSA0012873853_20120111194045.wav
//项目名_20120111194045_013711739184.wav
string[] s_fName = fName.Replace("#", "").Substring(0, fName.Length - 5).Split('_');
n_fName = this.textBox2.Text.Trim() + "_" + s_fName[2] + "_" + s_fName[0] + ".wav";
if (string.IsNullOrEmpty(this.textBox3.Text.Trim()))
{
//原用文件上改
File.Move(s_fif[i].FullName, path + "\\" + n_fName);
}
else
{
if (!Directory.Exists(s_fif[i].DirectoryName.Replace(this.textBox1.Text.Trim(), this.textBox3.Text.Trim())))
Directory.CreateDirectory(s_fif[i].DirectoryName.Replace(this.textBox1.Text.Trim(), this.textBox3.Text.Trim()));
File.Copy(s_fif[i].FullName, s_fif[i].DirectoryName.Replace(this.textBox1.Text.Trim(), this.textBox3.Text.Trim()) + "\\" + n_fName, true);
}
fCount += 1;
}
}
}
if (s_dif.Length > 0)
{
for (int i = 0; i < s_dif.Length; i++)
{
DealWithFile(path+"\\"+s_dif[i].Name);
}
}

}

private void button3_Click(object sender, EventArgs e)
{
if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox3.Text = this.folderBrowserDialog1.SelectedPath;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: