I downloaded a lot of videos online before. After decompressing them, I found that there was a long list of URLs in front of each file, which made me unable to see clearly what the name of each video was.
I searched for some batch renaming methods online, but none of them were what I wanted. Since that's the case, I'd just write one in Java by myself. I tested it and it should be fine, and I will share it now.
First upload the code:
import java.io.File;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;/*** Rename rule class* @author jack*/class ReplacementChain{private Map<String,String> map;public ReplacementChain() {this.map = new HashMap<String, String>();}public Map<String, String> getMap() {return map;}// Add new replacement rule (string replacement) public ReplacementChain addRegulation(String oldStr , String newStr){this.map.put(oldStr, newStr);return this;}}/*** Rename class* @author Jack*/public class Rename {/*** Batch rename* @param path* @param replacementChain*/public static void multiRename(String path,ReplacementChain replacementChain){File file = new File(path);boolean isDirectory = file.isDirectory();/** If it is not a folder, return * */if(!isDirectory){System.out.println(path + "not a folder!");return;}String[] files = file.list();File f = null;String filename = "";String oldFileName = ""; //Previous name/** Loop through all files* */for(String fileName: files){oldFileName = fileName;Map<String, String> map = replacementChain.getMap();for (Entry<String, String> entry : map.entrySet()) { fileName = fileName.replace(entry.getKey(), entry.getValue());} f = new File(path + "//" + oldFileName); //The output address is consistent with the original path f.renameTo(new File(path + "//" + fileName));}System.out.println("Congratulations, the batch renaming was successful! ");}public static void main(String[] args) {}}How to use:
If I now have a folder with several txt files inside, all with a very long prefix and suffix.
Now I want to remove all their prefixes;
The first step is to create a new instance of the ReplacementChain class in the main method, which is a rule class. The main thing is to set some replacement rules.
ReplacementChain replacementChain = new ReplacementChain();
Step 2: Add new replacement rules
The full name of the first file:
[I am a very long prefix] ~~~~~ Novel 001 (I am Mengmeng Little Tail).txt
We hope to remove [I am a very long prefix] ~~~~ and (I am Mengmeng’s little tail), and just add two replacement rules to replaceChain.
replacementChain.addRegulation("【I am a very long prefix】~~~~~", "").addRegulation("(I am Mengmeng's little tail), "");
addRegulation supports chain calls.
Step 3: Call the batch renaming method
Rename.multiRename("F://test folder", replacementChain);
Pass two parameters. The first is the folder path where the file that needs to be processed in batches, and the second is the replacementChain object.
run
If the console is printed out: Congratulations, the batch renaming is successful!
Then it means success.
Those files I've locally have been renamed in batches:
The above is the Java file batch renaming and personal test available (lite version) introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!