Some time ago I had a lot of tutorials on the Internet, and the file names all started with this character, "come with me to learn XXX", which was very confusing, because the file name is too long, it is too troublesome to manually change the file names when browsing the folder. If there are more than 100 files, let the computer automatically help me change the file name, use C # To write a simple tool.
Column directory Function Private void listdirectory (string path)
{
Directoryinfo dir = new directoryinfo (PATH );
Fileinfo [] files = dir. getfiles ();
String extension = getextension ();
Filelist. Items. Clear ();
Foreach (fileinfo file in files)
{
If (extension! = String. Empty)
{
If (extension. indexof (file. extension. toupper () >=- 1 & file. extension! = String. Empty)
{
Filelist. Items. Add (file. Name );
}
}
Else
{
Filelist. Items. Add (file. Name );
}
}
}
Get Extension Private string getextension ()
{
Stringbuilder sb = new stringbuilder (50 );
Foreach (Control in this. groupbox1.controls)
{
If (control. Tag. tostring (). Equals ("tagextension "))
{
If (control as checkbox). Checked)
{
SB. append (control. Text + ";");
}
}
}
Return sb. tostring ();
}
Rename Method Private void btnrename_click (Object sender, eventargs E)
{
If (MessageBox. Show ("this operation is irreversible. Are you sure you want to continue? "," Ask ", messageboxbuttons. yesno, messageboxicon. Question) = dialogresult. No)
Return;
Fileinfo file;
String prename = this.txt prename. Text. Trim ();
String pre = prename;
Int prelen = (INT) This. prelength. value;
Try
{
For (INT I = 0; I <filelist. Items. Count; I ++)
{
File = new fileinfo (FBD. selectedpath + "\" + filelist. items [I]. tostring ());
If (this. chkauto. Checked) Pre = I. tostring () + "." + prename;
If (filelist. items [I]. tostring (). length> = prelen)
{
File. moveTo (FBD. selectedpath + "\" + pre + filelist. items [I]. tostring (). substring (prelen ));
}
Else
{
File. moveTo (FBD. selectedpath + "\" + pre + filelist. items [I]. tostring ());
}
}
MessageBox. Show ("renamed", "prompt", messageboxbuttons. OK, messageboxicon. information );
Listdirectory (FBD. selectedpath );
}
Catch
{
MessageBox. Show ("Renaming error", "prompt", messageboxbuttons. OK, messageboxicon. Error );
}
}
}
Renamw.l.rar