
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Rename Multiple Files Using Java
Following is the code to rename multiple files using Java −
Example
import java.io.File; import java.io.IOException; public class Demo{ public static void main(String[] argv) throws IOException{ String path_to_folder = "path\to\folder\where\multiple\files\are\present"; File my_folder = new File(path_to_folder); File[] array_file = my_folder.listFiles(); for (int i = 0; i < array_file.length; i++){ if (array_file[i].isFile()){ File my_file = new File(path_to_folder + "\" + array_file[i].getName()); String long_file_name = array_file[i].getName(); String[] my_token = long_file_name.split("\s"); String new_file = my_token[1]; System.out.println(long_file_name); System.out.print(new_file); my_file.renameTo(new File(path_to_folder + "\" + new_file + ".pdf")); } } } }
Output
The files in the folder will be renamed to .pdf
A class named Demo contains the main fucntion, where the apth to the folder containing multiple files is defined. A new folder is created in the path mentioned.
The list of files is obtained using the 'listFiles' function. The file of array is iterated over, and if a file is encountered, a new file path is created and the name of the file is obtained and it is split. The files are renamed to .pdf. The names of files are shortened byobtaining the substring that begins after the first space in the 'long_file_name'.
Advertisements