在尝试了这么久之后没有得到正确的解决方案,所以在我的同事的帮助下通过 java 进行了尝试......感谢他。
如果有人需要,也愿意分享。
import java.io.File;
/**
* This class is search with start file character sequence and replace the file name with new character.
* @author nitin.choube
*
*/
public class SearchAndReplaceFileName
{
public static void main(String[] args)
{
//Parent file path from start searching files
File dir = new File("D:\\WS\\Upload");
// replace character string
final String replaceChar="XYZ";
// replace character string with
final String replaceCharWtih="ALB";
// file extension
final String fileExtension=".htm";
if(dir.isDirectory()){
File[] children = dir.listFiles();
iterateFile(children,replaceChar,replaceCharWtih,fileExtension);
}
}
/**
* This method is allow to search and replace file name.
* @param children
* @param replaceChar
* @param replaceCharWtih
* @param fileExtension
*/
public static void iterateFile(File[] children,String replaceChar,String replaceCharWtih,String fileExtension){
try {
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
File file = children[i];
System.out.println("Getting all files in " + file.getCanonicalPath() + " including those in subdirectories");
if(file.isDirectory()){
File[] child = file.listFiles();
iterateFile(child,replaceChar,replaceCharWtih,fileExtension);
}else if(file.isFile()){
String extension = file.getName().substring(file.getName().lastIndexOf("."));
System.out.println("extracted file name is "+file.getName()+" and extension is ="+extension);
if(extension.equals(fileExtension)){
String fileName=file.getName().substring(0,file.getName().lastIndexOf(fileExtension));
if(fileName.startsWith(replaceChar)){
String newFileName=fileName.replace(replaceChar,replaceCharWtih);
file.renameTo(new File(file.getCanonicalPath().substring(0,file.getCanonicalPath().lastIndexOf("\\"))+"\\"+newFileName+fileExtension));
}else if(fileName.contains("_"+replaceChar+"_")){
String newFileName=fileName.replace(replaceChar,replaceCharWtih);
file.renameTo(new File(file.getCanonicalPath().substring(0,file.getCanonicalPath().lastIndexOf("\\"))+"\\"+newFileName+fileExtension));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}