我在一个目录中有 500 个 pdf 文件。我想删除文件名的前五个字符并重命名它。
9 回答
用于重命名给定目录中文件列表的示例代码。在下面的示例中,c:\Projects\sample
是文件夹,其中列出的文件已重命名为 0.txt、1.txt、2.txt 等。
我希望这能解决你的问题
import java.io.File;
import java.io.IOException;
public class FileOps {
public static void main(String[] argv) throws IOException {
File folder = new File("\\Projects\\sample");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
File f = new File("c:\\Projects\\sample\\"+listOfFiles[i].getName());
f.renameTo(new File("c:\\Projects\\sample\\"+i+".txt"));
}
}
System.out.println("conversion is done");
}
}
这样的事情应该做(Windows版本):
import java.io.*;
public class RenameFile {
public static void main(String[] args) {
// change file names in 'Directory':
String absolutePath = "C:\\Dropbox\\java\\Directory";
File dir = new File(absolutePath);
File[] filesInDir = dir.listFiles();
int i = 0;
for(File file:filesInDir) {
i++;
String name = file.getName();
String newName = "my_file_" + i + ".pdf";
String newPath = absolutePath + "\\" + newName;
file.renameTo(new File(newPath));
System.out.println(name + " changed to " + newName);
}
} // close main()
} // close class
用于File.listFiles(...)
列出目录中的文件、String.substring(...)
形成新的文件名以及File.rename(...)
进行重命名。
但我建议您在开始重命名之前让您的应用程序检查它是否可以重命名所有文件而不会发生任何冲突。
但是@Pascal 的评论是正确的。Java 不是做这种事情的最简单的工具。
If you're on Windows you should use the command prompt or a .bat file. Windows supports wildcard renames natively at the OS level so it will be orders of magnitude faster than Java, which has to iterate over all the names and issue rename calls for each one.
如果您在 MacOS X 上并且想要从外部驱动器重命名文件夹和子文件夹中的所有文件,下面的代码将完成这项工作:
public class FileOps {
public static void main(String[] argv) throws IOException {
String path = "/Volumes/FAT32/direito_administrativo/";
File folder = new File(path);
changeFilesOfFolder(folder);
}
public static void changeFilesOfFolder(File folder) {
File[] listOfFiles = folder.listFiles();
if (listOfFiles != null) {
int count = 1;
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
File f = new File(folder.getPath() + "/" + listOfFiles[i].getName());
f.renameTo(new File(folder.getPath() + "/" + count + ".flv"));
count++;
} else if (listOfFiles[i].isDirectory()) {
changeFilesOfFolder(listOfFiles[i]);
}
}
} else {
System.out.println("Path without files");
}
}
}
对于这类工作,Java 是一个糟糕的选择。更好的选择是像 Groovy 这样的 JVM 脚本语言。如果你想追求这个选择
步骤1:
下载并安装 Groovy
第2步:
启动 groovy 控制台
第 3 步:
运行此脚本
def dirName = "/path/to/pdf/dir"
new File(dirName).eachFile() { file ->
def newName = file.getName()[5..-1]
File renamedFile = new File(dirName + "/" + newName)
file.renameTo(renamedFile)
println file.getName() + " -> " + renamedFile.getName()
}
我在这里假设所有文件都在目录中/path/to/pdf/dir
。如果其中一些在此目录的子目录中,则使用File.eachFileRecurse
而不是File.eachFile
.
这将更改您提到的文件夹的所有文件名:
for (int i = 0; i < folders.length; i++) {
File folder = new File("/home/praveenr/Desktop/TestImages/" + folders[i]);
File[] files2 = folder.listFiles();
int count = 1;
for (int j = 0; j <files2.length; j++,count++) {
System.out.println("Old File Name:" + files2[j].getName());
String newFileName = "/home/praveenr/Desktop/TestImages/" + folders[i]+"/file_"+count+"_original.jpg";
System.out.println("New FileName:" + newFileName);
files2[j].renameTo(new File(newFileName));
}
}
好吧,试试这个示例代码
import java.io.File;
public class RenameFile
{
public String callRename(String flname, String fromName, String toName)
{
try
{
File fe = new File(flname);
File allFile[] = fe.listFiles();
for(int a = 0; a < allFile.length; a++)
{
String presentName = (allFile[a].toString().replaceAll(fromName, toName));
allFile[a].renameTo(new File(presentName));
}
return allFile + " files renamed successfully.!!!";
}
catch(Exception ae)
{
return(ae.getMessage());
}
}
public static void main(String[] args)
{
RenameFile rf = new RenameFile();
System.out.println("Java rename files in directory : ");
String lastResult = rf.callRename("yourpathname", "from", "to");
System.out.println(lastResult);
}
}
根据您的要求,使用 java 可以实现如下所示,
//path to folder location
String filePath = "C:\\CMS\\TEST";
//create direcotory TEST
File fileDir = new File(filePath);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
//create two file named 12345MyFile1.pdf, 12345MyFile2.pdf
File file1 = new File(filePath, "\\12345MyFile1.pdf");
file1.createNewFile();
File file2 = new File(filePath, "\\12345MyFile2.pdf");
file2.createNewFile();
//rename the file
File file = new File(filePath);
String[] fileList = file.list();//get file list in the folder
for (String existFileName : fileList) {
String newFileName = existFileName.substring(5);//remove first 5 characters
File sourceFile = new File(filePath + File.separator + existFileName);
File destFile = new File(filePath + File.separator + newFileName);
if (sourceFile.renameTo(destFile)) {
System.out.println("File renamed successfully from: " + existFileName + " to: " + newFileName);
} else {
System.out.println("Failed to rename file");
}
}