A simple batch replacement advertising gadget~
import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** *Automatically scan all files under the folder and remove the advertisement field in the file name */ public class UpdateFileName { public static int num = 1; //The specific location of the original promotion file, such as L: testdev Scan Code.png public static String AD0pathfrom = "1"; //The specific location of the original promotion file, such as L: testdev Scan Code.png public static String AD1pathfrom = "1"; //The specific location of the original promotion file, such as L: testdev source of material - 94 design.url public static String AD2pathfrom = "1"; public static String AD3pathfrom = "1"; /** *Program entry * * @param args */ public static void main(String[] args) { System. out. println ("------------ Welcome to use the batch replacement promotion advertising tool ------------"); System.out.println ("Main function: folders are numbered incrementally according to 1, and customized advertising files are placed, up to 4"); System. out. println ("The format of the input path: such as (L: testdev scanning code. png)"); System. out. println ("The input path format: such as (L: testdev more free materials. url)"); System. out. println ("Paste the same advertisement if there are not 4~"); System.out.println("-----------------By:Lcry------------------------"); Scanner sc = new Scanner(System.in); List<String> list = new ArrayList<>(); System. out. println ("Please enter the root directory of the folder to be replaced, such as L: testdev"); String resurl = sc.next(); for (int i = 1; i <= 4; i++) { System. out. println ("Please enter the"+i+"advertising path:"); String next = sc.next(); list.add(next); } AD0pathfrom = list.get(0); AD1pathfrom = list.get(1); AD2pathfrom = list.get(2); AD3pathfrom = list.get(3); System.out.println ("Please enter the serial number of the starting number: such as 1"); int scint = sc.nextInt(); num = scint; recursiveFiles(resurl); System. out. println ("Execution Completed"); } /** *Copy files * *@ param source source file address *@ param dest target file address * @param bufferSize */ public static void copy(String source, String dest, int bufferSize) { InputStream in = null; OutputStream out = null; try { in = new FileInputStream(new File(source)); out = new FileOutputStream(new File(dest)); byte[] buffer = new byte[bufferSize]; int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** *Traverse all folders and files * *@ param path The path of the folder to traverse is as follows: L: testdev */ private static void recursiveFiles(String path) { String AD0pathto = "".equals(AD0pathfrom) ? null : AD0pathfrom.substring(AD0pathfrom.lastIndexOf("\\") + 1); String AD1pathto = "".equals(AD1pathfrom) ? null : AD1pathfrom.substring(AD1pathfrom.lastIndexOf("\\") + 1); String AD2pathto = "".equals(AD2pathfrom) ? null : AD2pathfrom.substring(AD2pathfrom.lastIndexOf("\\") + 1); String AD3pathto = "".equals(AD3pathfrom) ? null : AD3pathfrom.substring(AD3pathfrom.lastIndexOf("\\") + 1); //Create File Object File file = new File(path); //Access file/folder File files[] = file.listFiles(); //If the object is empty, return directly if (files == null) { return; } //Files under directory if (files.length == 0) { System. out. println (path+"There are no files in this folder"); } //Judgment of existing file traversal for (File f : files) { //Determine whether it is a folder if (f.isDirectory()) { //If it is a folder, put it into the promotion picture //System. out. print ("Folder:"); System.out.println(f.getAbsolutePath()); copy(AD0pathfrom, f.getAbsolutePath() + "\\" + AD0pathto, 10240); copy(AD1pathfrom, f.getAbsolutePath() + "\\" + AD1pathto, 10240); copy(AD2pathfrom, f.getAbsolutePath() + "\\" + AD2pathto, 10240); copy(AD3pathfrom, f.getAbsolutePath() + "\\" + AD3pathto, 10240); //Continue traversing the folder recursiveFiles(f.getAbsolutePath()); //Modify directory String dirDirectoryPath = f.getAbsolutePath();// Directory Path File finalDirectoryName = new File(dirDirectoryPath +"---"+youxusum()+"-xxx--"); f.renameTo(finalDirectoryName); }Else if (f. isFile()) {//Judge whether it is a file //If the folder is renamed in batch, it will not be changed if it is an advertisement: if ( (AD0pathto != null && f.getName().contains(AD0pathto)) || (AD2pathto != null && f.getName().contains(AD2pathto)) || (AD3pathto != null && f.getName().contains(AD3pathto)) || (AD1pathto != null && f.getName().contains(AD1pathto)) ) { //If it is found that it is an advertisement, then no random sequence will be added } else { // String dirPath = f.getAbsolutePath();// Directory Path // String toFileName = addsuffix(dirPath); // File finalName = new File(toFileName); // f.renameTo(finalName); } } else { System. out. print ("unknown error file"); } } } /** *Replace the folder name and add the idword auto generated sequence to the files in each folder *File+random ID */ public static String addsuffix(String srcfilename) { //Get Front of Folder String prefixname = srcfilename.substring(0, srcfilename.lastIndexOf(".")); //Get End of Folder String suffixname = srcfilename.substring(srcfilename.lastIndexOf(".")); //Please import the IdWorker class if necessary // IdWorker idWorker = new IdWorker(1, 1); // return prefixname + (idWorker.nextId() + "") + suffixname; return prefixname + suffixname; } /** *Generate ordered sequence */ public static int youxusum() { int num = UpdateFileName.num; ++UpdateFileName.num; return num; } }