//这个算是为了纪念自己电脑被偷一个星期后的所做:丢失的东西可能永远都找不回来,而所有过去存在的灵感思维丢失了就很难找回来.虽然还有人还有很长时间的路可以走.也或许会设计出来更好更完善的程序.但是曾经的精力和过程呢,能轻易的忘却么?
//新增功能描述:主要在前一版本上新增并完成了替换当前目录下的所有目录文件夹中的文件名.
//操作方法:主要是选择目标存放文件夹,然后修改要替换的原始文件名中包含的字符串,最后点替换当前目录(整个目录)后缀就可以实现批量更新目标文件夹下所有指定字符串文件名成为新的文件名了.
//尚存在问题,比如替换JPG为GIF的时候,原始文件夹下的.HTML文件可能没有办法拷贝到新的文件夹中.经测试可能会在原始文件夹下存留个别文件,请使用者在替换完成后检查.因为暂时没有找到错误原因在哪里.
功能应用描述:
这个程序主要功能是批量替换,用途:
1.一个ASP站点下的所有后缀名为ASP的文件替换JSP站点下的所有JSP后缀名。然后修正代码函数成为一个JSP站点
2.一个ASP.NET中多层架构的时候编写相同的功能模块用。比如都有增删改查几个操作的CS文件,先用这个程序替换完所有的文件名,然后拷贝至对应的文件夹,然后在Dreamweaver中批量替换掉所有对应的窗体或者实体类名就能快速新增一个模块了。
3.主要能方便美工再PHOTOSHOP中导出切片时候格式错误,因为网咯更喜欢GIF的,批量替换掉就等美工有时间了可以直接覆盖掉一个文件夹下的所有图片文件。
//下载地址:项目文件在我上传的资源空间可以找到.VS2003和VS2005版本的。基本上函数变量一样.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace ListFileManager
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.FolderBrowserDialog FolderBrowserDialog1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
public string recentFolder="C:";
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.FolderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(104, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(216, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "c://222";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(104, 48);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(216, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Text = ".jpg";
//
// button1
//
this.button1.Location = new System.Drawing.Point(328, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 23);
this.button1.TabIndex = 3;
this.button1.Text = "设置存放目标地址";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listBox1
//
this.listBox1.HorizontalScrollbar = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(8, 120);
this.listBox1.Name = "listBox1";
this.listBox1.ScrollAlwaysVisible = true;
this.listBox1.Size = new System.Drawing.Size(560, 244);
this.listBox1.TabIndex = 9;
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 56);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 16);
this.label1.TabIndex = 4;
this.label1.Text = "原始字符串";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 88);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 16);
this.label2.TabIndex = 5;
this.label2.Text = "替换字符串";
//
// button2
//
this.button2.Location = new System.Drawing.Point(328, 48);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(112, 23);
this.button2.TabIndex = 4;
this.button2.Text = "替换当前目录后缀";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 16);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 16);
this.label3.TabIndex = 8;
this.label3.Text = "文件目录";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(104, 80);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(216, 21);
this.textBox3.TabIndex = 2;
this.textBox3.Text = ".gif";
//
// button3
//
this.button3.Location = new System.Drawing.Point(328, 80);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(112, 23);
this.button3.TabIndex = 5;
this.button3.Text = "替换当前目录文件";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(456, 16);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(112, 23);
this.button4.TabIndex = 6;
this.button4.Text = "退出当前应用程序";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(456, 48);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(112, 23);
this.button5.TabIndex = 7;
this.button5.Text = "替换整个目录后缀";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(456, 80);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(112, 23);
this.button6.TabIndex = 8;
this.button6.Text = "替换整个目录文件";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(576, 373);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.button2);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "C#批量重命名工具V2.0";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public static string OldPartFileName="";
public static string NewPartFileName="";
public static string TopSourceFolderName="";
public static string TopTargetFolderName = "";
public static string TempSourceFolderName = "";
public static string TempTargetFolderName = "";
public static int TotalRenameNumber=0;
public static int TempRenameNumber=0;
//按钮事件----------------------------------------------------------------------------------
private void button1_Click(object sender, System.EventArgs e)
{
try
{
FolderBrowserDialog myFolderBrowserDialog = new FolderBrowserDialog();
myFolderBrowserDialog.RootFolder = Environment.SpecialFolder.MyComputer;
if (myFolderBrowserDialog.ShowDialog() == DialogResult.OK)
{
string myFolderPath = myFolderBrowserDialog.SelectedPath;
recentFolder = myFolderPath;
this.textBox1.Text = myFolderPath;
}
}
catch (Exception ex)
{
MessageBox.Show("暂未选择任何文件夹" + ex.Message);
}
}
private void button2_Click(object sender, System.EventArgs e)
{
//不分大小写替换部分
string strOldFileName;
string strNewFileName;
string strOldPart = this.textBox1.Text.Trim();//重命名文件前的文件名等待替换字符串
string strNewPart = this.textBox2.Text.Trim();//重命名文件后的文件名替换字符串
string strFileFolder = this.textBox3.Text.Trim();//重命名文件后所在的目录
if (strOldPart == "")
{
MessageBox.Show("请填写替换前文件名部分");
return;
}
if (strNewPart == "")
{
MessageBox.Show("请填写替换后文件名部分");
return;
}
if (strFileFolder == "")
{
MessageBox.Show("请填写或选择替换对象所在目录");
return;
}
string strNewFilePath;
int TotalFiles = 0;
DateTime StartTime = DateTime.Now; //获取开始时间
DirectoryInfo di = new DirectoryInfo(strFileFolder);
FileInfo[] filelist = di.GetFiles("*.*");
int i = 0;
foreach (FileInfo fi in filelist)
{
strOldFileName = fi.Name;
strNewFileName = ReplaceFileName(fi.Name, strOldPart, strNewPart);
//strNewFileName=fi.Name.Replace(strOldPart.ToLower(),strNewPart.ToLower());
//strNewFileName=strNewFileName.Replace(strOldPart.ToUpper,strNewPart.ToUpper());
strNewFilePath = @strFileFolder + "//" + strNewFileName;
filelist[i].MoveTo(@strNewFilePath);
TotalFiles += 1;
this.listBox1.Items.Add("文件名:" + strOldFileName + "已重命名为" + strNewFileName);
i += 1;
}
DateTime EndTime = DateTime.Now;//获取结束时间
TimeSpan ts = EndTime - StartTime;
this.listBox1.Items.Add("总耗时:" + ts.Hours.ToString() + "时" + ts.Minutes.ToString() + "分" + ts.Seconds.ToString() + "秒");
}
private void button3_Click(object sender, System.EventArgs e)
{
string strOldFileName;
string strNewFileName;
string strOldPart = this.textBox1.Text.Trim();//重命名文件前的文件名等待替换字符串
string strNewPart = this.textBox2.Text.Trim();//重命名文件后的文件名替换字符串
string strNewFilePath;
string strFileFolder; //原始图片目录
int TotalFiles = 0;
DateTime StartTime = DateTime.Now; //获取开始时间
FolderBrowserDialog f1 = new FolderBrowserDialog(); //FORM控件
if (f1.ShowDialog() == DialogResult.OK)
{
strFileFolder = f1.SelectedPath;
DirectoryInfo di = new DirectoryInfo(strFileFolder);
FileInfo[] filelist = di.GetFiles("*.*");
int i = 0;
foreach (FileInfo fi in filelist)
{
strOldFileName = fi.Name;
strNewFileName = fi.Name.Replace(strOldPart, strNewPart);
strNewFilePath = @strFileFolder + "//" + strNewFileName;
filelist[i].MoveTo(@strNewFilePath);
TotalFiles += 1;
this.listBox1.Items.Add("文件名:" + strOldFileName + "已重命名为" + strNewFileName);
i += 1;
}
}
DateTime EndTime = DateTime.Now;//获取结束时间
TimeSpan ts = EndTime - StartTime;
this.listBox1.Items.Add("总耗时:" + ts.Hours.ToString() + "时" + ts.Minutes.ToString() + "分" + ts.Seconds.ToString() + "秒");
}
private void button4_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void button5_Click(object sender, System.EventArgs e)
{
//不分大小写替换部分
TopTargetFolderName = this.textBox1.Text.Trim();//重命名文件后所在的目录
OldPartFileName = this.textBox2.Text.Trim();//重命名文件前的文件名等待替换字符串
NewPartFileName = this.textBox3.Text.Trim();//重命名文件后的文件名替换字符串
if (OldPartFileName == "")
{
MessageBox.Show("请填写替换前文件名部分");
return;
}
if (NewPartFileName == "")
{
MessageBox.Show("请填写替换后文件名部分");
return;
}
if (TopTargetFolderName == "")
{
MessageBox.Show("请填写或选择替换对象所在目录");
return;
}
DateTime StartTime = DateTime.Now; //获取开始时间
FolderBrowserDialog f1 = new FolderBrowserDialog(); //FORM控件
if (f1.ShowDialog() == DialogResult.OK)
{
TopSourceFolderName = f1.SelectedPath;
RenameFolderFile(TopSourceFolderName);
}
DateTime EndTime = DateTime.Now;//获取结束时间
TimeSpan ts = EndTime - StartTime;
this.listBox1.Items.Add("总耗时:" + ts.Hours.ToString() + "时" + ts.Minutes.ToString() + "分" + ts.Seconds.ToString() + "秒");
}
private void button6_Click(object sender, System.EventArgs e)
{
//不分大小写替换部分
TopTargetFolderName = this.textBox1.Text.Trim();//重命名文件后所在的目录
OldPartFileName = this.textBox2.Text.Trim();//重命名文件前的文件名等待替换字符串
NewPartFileName = this.textBox3.Text.Trim();//重命名文件后的文件名替换字符串
if (OldPartFileName == "")
{
MessageBox.Show("请填写替换前文件名部分");
return;
}
if (NewPartFileName == "")
{
MessageBox.Show("请填写替换后文件名部分");
return;
}
if (TopTargetFolderName == "")
{
MessageBox.Show("请填写或选择替换对象所在目录");
return;
}
DateTime StartTime = DateTime.Now; //获取开始时间
FolderBrowserDialog f1 = new FolderBrowserDialog(); //FORM控件
if (f1.ShowDialog() == DialogResult.OK)
{
TopSourceFolderName = f1.SelectedPath;
RenameFolderFile(TopSourceFolderName);
}
DateTime EndTime = DateTime.Now;//获取结束时间
TimeSpan ts = EndTime - StartTime;
this.listBox1.Items.Add("总耗时:" + ts.Hours.ToString() + "时" + ts.Minutes.ToString() + "分" + ts.Seconds.ToString() + "秒");
}
//共用函数----------------------------------------------------------------------------------------------------
//重命名文件
public string ReplaceFileName(string strFileName, string strOldString, string strNewString)
{
string p = strOldString;
return Regex.Replace(strFileName, p, strNewString, RegexOptions.IgnoreCase);
}
//通过遍历来重命名目标文件夹下所有的子目录和文件
public void RenameFolderFile(string dir)
{
TempSourceFolderName = dir;
TempTargetFolderName = dir.Replace(TopSourceFolderName, TopTargetFolderName);
TempRenameNumber = 0;
string strOldFileName = "";
string strNewFileName = "";
//string strOldFilePath = "";
//string strNewFilePath = "";
if (Directory.Exists(TempSourceFolderName))
{
foreach (string d in Directory.GetFileSystemEntries(TempSourceFolderName))
{
if (File.Exists(d))
{
FileInfo fi = new FileInfo(d);
if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
fi.Attributes = FileAttributes.Normal;
strOldFileName = fi.Name;
strNewFileName = fi.Name.Replace(OldPartFileName, NewPartFileName);
//strOldFilePath = @TempSourceFolderName + "//" + strOldFileName;
//strNewFilePath = @TempTargetFolderName + "//" + strNewFileName;
MoveFile(strOldFileName, strNewFileName, @TempSourceFolderName, @TempTargetFolderName); //重命名文件并移动到目标地址
TotalRenameNumber += 1;
TempRenameNumber += 1;
this.listBox1.Items.Add("文件名:" + @TempSourceFolderName + strOldFileName + "已重命名为" + @TempTargetFolderName + strNewFileName);
}
else
{
RenameFolderFile(d);//递归删除子文件夹
}
}
if (Directory.Exists(TempSourceFolderName))
{
Directory.Delete(TempSourceFolderName);//删除已空文件夹
}
}
}
//移动文件到目的地址并删除原始文件
public void MoveFile(string OldFileName, string NewFileName, string OldDirectoryPath, string NewDirectoryPath)
{
try
{
//判断目标文件夹是否存在,如果不存在则创建新文件夹
DirectoryInfo di = new DirectoryInfo(NewDirectoryPath);
if (di.Exists == false) di.Create();
if (File.Exists(NewDirectoryPath+"//"+NewFileName))
File.Delete(NewDirectoryPath+"//"+NewFileName);
//移动文件到目标地址
File.Move(OldDirectoryPath+"//"+OldFileName,NewDirectoryPath+"//"+NewFileName);
//删除原始文件
File.Delete(OldDirectoryPath+"//"+OldFileName);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
//移动目录函数
public bool MoveDirectory(string NewDirectoryPath, string NewDirectoryName)
{
try
{
//如果目标目录不存在则创建新的文件夹
DirectoryInfo di = new DirectoryInfo(NewDirectoryPath);
if (di.Exists == false) di.Create();
//如果目标子目录不存在则创建目标子目录
DirectoryInfo dis = di.CreateSubdirectory(NewDirectoryPath + "//" + NewDirectoryName);
if (Directory.Exists(NewDirectoryPath + "//" + NewDirectoryName) == false)
di.MoveTo(NewDirectoryPath + "//" + NewDirectoryName);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}
}
//如果目录下的子文件夹和文件全部移动完成则删除掉当前目录和所有子目录
public bool DeleteDirectory(string OldDirectoryName, string OldDirectoryPath)
{
try
{
DirectoryInfo dir = new DirectoryInfo(OldDirectoryPath + "//" + OldDirectoryName);
//判断目录下是否存在文件,如果有则批量删除
FileInfo[] finfo = dir.GetFiles("*.*");
if (finfo.Length > 0)
{
foreach (FileInfo f in finfo)
{
this.listBox1.Items.Add("正在删除目录:" + OldDirectoryPath + "下的" + OldDirectoryName + "中的文件" + f.FullName);
f.Delete();
}
}
//判断目录下是否存在子目录,如果有则批量删除
DirectoryInfo[] dinfo = dir.GetDirectories("*");
if (dinfo.Length > 0)
{
foreach (DirectoryInfo d in dinfo)
{
this.listBox1.Items.Add("正在删除目录:" + OldDirectoryPath + "下的" + OldDirectoryName + "中的目录" + d.FullName);
d.Delete(true);
}
}
//删除当前目录
//this.listBox1.Items.Add("正在删除目录:" + OldDirectoryPath + "下的" + OldDirectoryName + "目录");
//dir.Delete(true);
return true;
}
catch (Exception ex)
{
Console.Write(ex.ToString());
return false;
}
}
//遍历删除目标文件夹下所有的子目录和文件
public void DeleteFolder(string dir)
{
if (Directory.Exists(dir))
{
foreach (string d in Directory.GetFileSystemEntries(dir))
{
if (File.Exists(d))
{
FileInfo fi = new FileInfo(d);
if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
fi.Attributes = FileAttributes.Normal;
File.Delete(d);//直接删除其中的文件
}
else
DeleteFolder(d);//递归删除子文件夹
}
Directory.Delete(dir);//删除已空文件夹
}
}
}
}