File renaming implementation

Source: Internet
Author: User

 

Knowledge involved: string functions, File and Directory functions, Environment and some interface classes

The core code is as follows: very simple

// Clear logs
This. listBoxLog. Items. Clear ();
// Obtain all file names in the current path
String [] files = Directory. GetFiles (Environment. CurrentDirectory );
Foreach (String filename in files)
{
// The Last "\"
Int lastpath = filename. LastIndexOf ("\\");
// The Last "."
Int lastdot = filename. LastIndexOf (".");
// File Name Length
Int length = lastdot-lastpath-1;
// File directory string xx \
String beginpart = filename. Substring (0, lastpath + 1 );
// File name
String namenoext = filename. Substring (lastpath + 1, length );
// Extension
String ext = filename. Substring (lastdot );

If (length <3)
{
// Fill in three characters to form a new file name
String namenew;
If (length = 1)
Namenew = "00" + namenoext;
Else
Namenew = "0" + namenoext;
String fullnewname = beginpart + namenew + ext;

// Rename
File. Move (filename, fullnewname );

// Log
This. listBoxLog. Items. Add (namenoext + "--->" + namenew );
This. listBoxLog. SelectedIndex = this. listBoxLog. Items. Count-1;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.