Modify file names in batches in JAVA.

Source: Internet
Author: User

Modify file names in batches in JAVA.

Today, I watched the JAVA video of Chuanzhi podcast. Every time I watched it, I tried to find a specific video for review. I found that the name of each downloaded video is very long, so that all the parts that can be differentiated are ignored. After learning JAVA, I suddenly wondered if I could remove the same parts before the file name, in this way, the part about the video content can be displayed. Okay. Try it.

After thinking about the idea, first find the directory of the file to be modified, get the file name of the file to be modified, then get the file name of the part not to be deleted, and rename the file.

As I was a beginner, I did not know much about it in many places, so I checked a piece of code from the Internet.

  

Import java. io. File;
Import java. io. IOException;

/**
* If the file name is "C123_the title of article.pdf", we need to change these files
* "The title of articles" format.
*
*
*/
Public class FileRename {
Public static void main (String [] args) throws IOException {
Java. io. File file = new java. io. File ("G:/Test ");
String dirPath = file. getAbsolutePath (); // directory path
If (file. isDirectory ()){
File [] files = file. listFiles (); // obtain the File list in this directory

Long starttime = System. currentTimeMillis ();

For (File fileFrom: files ){
String fromFile = fileFrom. getName (); // file name
String toFileName;
Int index;
Index = fromFile. indexOf ("_");
FromFile = fromFile. substring (index + 1); // The String following _

If (index! =-1) // prevent some file names from missing _
{
ToFileName = dirPath + "\" + fromFile;
File toFile = new File (toFileName );
If (fileFrom. exists ()&&! ToFile. exists ()){
// Start renaming
FileFrom. renameTo (toFile );
}
}

}

Long endtime = System. currentTimeMillis ();

System. out. println ("Time:" + new Long (endtime-starttime); // Time consumed
}
}
}

Modify the directory path of the file, modify the part of the file name to be deleted, and then check the usage of indexOf, returns the first occurrence of a substring in a string object. . For example, if I want to delete the file name section before "Number", I need to change the parameter in the function to "Number", and the start position starts from the start part. The substring function is used to intercept strings. Substring (int beginindex, int endindex) refers to the string from the specified beginindex to the end of the endindex-1.

After modification, run and modify the file name. A little excited, haha.

However, there is another small problem. In this way, only the file name in a folder is modified. It seems that a loop is required, and there are a lot of folders to be modified.

···

···

OK. Add a loop. All modifications are completed. Dinner ···


Java reads and changes the file name in batches

You just need to make some modifications on your own.
Import java. io .*;
Import java. SQL .*;
Public class RenameFile
{
File parentFile;
Public RenameFile (File parentFile)
{
This. parentFile = parentFile;
ReadDataBase (parentFile. list ());
}
Private void readDataBase (String [] list) // The method for reading the database in this function may be different from yours.
{
For (String oldName: list)
{
Try
{
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
Connection conn = DriverManager. getConnection ("jdbc: odbc: DSN", "", ""); // DSN is the data source name.
Statement st = conn. createStatement ();
ResultSet rs = st.exe cuteQuery ("Select * from TABLENAME where FILENAME =" + oldName); // TABLENAME indicates the name of the table in the database, and FILENAME indicates the name of the field in the file name.
While (rs. next ())
{
String newName = rs. getString ("NUMBER"); // NUMBER indicates the student ID field. getString must be changed to getInt or another type based on your data type.
Rename (oldName, newName );
}
}
Catch (Exception e)
{
E. printStackTrace ();
}
}
}
Private void rename (String oldName, String newName)
{
Try
{
File source = new File (parentFile. toString () + "\" + oldName );
File target = new File (source. getParent () + "\" + newName );
Boolean res = source. renameTo (target );
}
Catch (Exception e)
{
E. printStackTrace ();
}
}
Public static void main (String [] args)
{
String path = "path"; // change path to the folder path.
File file = new File (path );
New RenameFile (file );
}
}

_______________________________
For (String oldName: lis... for the rest>

Kneeling Program Design: write a program to change the file name in batches (it is best to use JAVA, C language, C ++)

Public class ReNameFile {

Public static String path = "Enter the disk or folder path you want to rename ";

Public static void main (String [] args ){

ReName (path );

}

Public static void reName (String filePath ){

File rootFile = new File (filePath );
If (rootFile. isDirectory ()){
File files [] = rootFile. listFiles ();
If (files! = Null & files. length> 0 ){
For (int I = 0; I <files. length; I ++ ){
File f = files [I];
If (f. isDirectory ()){
ReName (f. getAbsolutePath ());
} Else {
F. renameTo (new File ("Enter the name you want to rename"); // remember to enter the path
}
}
}

} Else {
RootFile. renameTo (new File ("Enter the name you want to rename"); // remember to enter the path
}

}

}

If you do not understand, continue to ask questions.

Related Article

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.