How to batch rename TXT files based on Book IDs in their content? Regular expression number extraction tutorial


Translation:EnglishFrançaisDeutschEspañol日本語한국어,Update Time:2026-07-08 06:33:49

Disclaimer: All images, text, and video content on the website are for reference only and may not be the latest, correct, or accurate. In case of any dispute, please refer to the actual experience effect!

When dealing with a large number of txt files where the filenames are just temporary names like book1 or book2, and the truly valuable numbers are inside the file content, manually opening, copying, and renaming each one is extremely inefficient. Using HeSoft Doc Batch Tool as an example, this article demonstrates how to use the "Rename Text Files Using File Content" feature to extract the numbers after the Book ID using regular expressions and batch rename these txt files with those numbers as the new filenames.

When organizing e-book materials, log texts, collection results, or exported interface files, a common problem often arises: the file names in the folder have no practical meaning, such as book1.txt, book2.txt, book3.txt. It is only after opening the files that you discover the actual numbers, titles, or business fields needed for archiving are written inside the text content. The manual approach typically involves opening each txt file one by one, copying the ID from the content, and then returning to the folder to rename it. This is acceptable when there are only a few files, but with dozens or hundreds of text files, this repetitive operation is not only time-consuming but also prone to copying errors, missed renames, or mistyped file names.

This article aims to solve the problem of "how to batch rename many text files using their content with wildcards or regular expressions." In the example, the first line of each text file contains content similar to Book ID:4829173056. We want to extract the number after "Book ID:" and batch rename the original book1.txt, book2.txt to file names like 4829173056.txt, 6094728315.txt. The operation uses the office software positioning of HeSoft Doc Batch Tool . Its core value is batch processing files to reduce repetitive work, suitable for handling batch tasks in office scenarios involving txt, documents, PDFs, spreadsheets, etc.

Applicable Scenarios: Which Text Files Are Suitable for Batch Renaming by Content

This method is particularly suitable for scenarios where "the file name is not important, but the file content has a stable identifier." For example, batch-downloaded novel chapters, e-book introductions, product data, order texts, contract summaries, log files, exported txt records, etc. As long as each text file contains a recognizable field, such as Book ID, Order No, Number, Title, Name, Date, etc., you can consider using regular expressions to extract this content as the new file name.

Unlike ordinary batch renaming, renaming by file content is not about simply replacing characters in the file name. Instead, it reads the content of each text file first, finds the target text according to specified rules, and then writes the matching result back to the file name. The advantage of regular expressions lies in their ability to describe rules like "match the numbers following a specific fixed text," "match a certain title line," or "match the content inside the first set of parentheses," making it more stable than manual copying and more flexible than ordinary keyword replacement.

Effect Preview: File Names and Text Content Before Processing

Before processing, the text file names in the folder are book1.txt to book5.txt. Such file names only indicate sequence and cannot directly show the book number or business number corresponding to each file. If you later need to retrieve, archive, or import them into a system based on the number, these file names are not user-friendly enough.

image-Batch rename TXT files using text content,rename files based on text content,extract filenames with regular expressions

Opening one of the txt files reveals that the first line contains Book ID:4829173056. The part indicated by the red arrow shows that what is truly suitable as the file name is the number after Book ID, not the original file name book1.txt. Since each file has a similar structure, we can use the same regular expression to extract the numbers in batch.

image-Batch rename TXT files using text content,rename files based on text content,extract filenames with regular expressions

Effect Preview: File Names Become the Numbers from the Content After Processing

After processing, the original file names like book1.txt, book2.txt are batch renamed to the numeric IDs from the text content, such as 1958436720.txt, 4829173056.txt, 6094728315.txt, 7305619482.txt, 8640295173.txt. You can see that the main part of the file name has been replaced with the Book ID number extracted from the text content, while the extension remains .txt, making them easy to continue opening and managing as text files.

image-Batch rename TXT files using text content,rename files based on text content,extract filenames with regular expressions

The benefits of this processing result are very direct: the folder list itself becomes an index, eliminating the need to open files to confirm numbers; subsequent sorting, searching, sending, or importing into systems by number becomes much more convenient. For office workers who need to repeatedly organize text materials, this type of batch processing can significantly reduce repetitive labor.

Operation Step 1: Enter the "Rename Text Files Using File Content" Function

Open HeSoft Doc Batch Tool and select the "File Name" category in the left navigation. The main interface will list multiple functions related to file name processing, such as finding and replacing file name keywords, inserting text, adding prefixes and suffixes, etc. The function to be used this time is the 8th one: "Rename Text Files Using File Content." The description for this function is to batch use certain text from the content of text files as the file name for that file, which perfectly matches the requirement of extracting the Book ID from txt content in this example.

image-Batch rename TXT files using text content,rename files based on text content,extract filenames with regular expressions

After clicking this function, the software enters a wizard-style operation page. The advantage of the wizard is that it breaks down complex tasks into several steps: first select files, then set extraction rules, then set the save location, and finally start processing. For users unfamiliar with regular expressions, as long as the field structure is stable, simply fill in the steps to complete the batch rename.

Operation Step 2: Add the txt Text Files to Be Processed

After entering the function page, the first step is to "Select records to be processed." The top right of the interface provides two entry points: "Add Files" and "Import Files from Folder." If you are only processing a small number of specific files, you can click "Add Files"; if the entire folder contains txt files that need renaming, you can use "Import Files from Folder," which is more suitable for batch operations.

image-Batch rename TXT files using text content,rename files based on text content,extract filenames with regular expressions

After the files are added, the list will display information such as sequence number, name, path, extension, creation time, and modification time. In the example, 5 text files have been imported, all with .txt extensions, located in the D:\test directory. It is recommended to check the list at this point to confirm that no files that should not be processed were added by mistake. If you find a file should not be processed, you can remove it using the delete icon in the operation column; if all selections are wrong, you can use "Clear" on the interface. After confirming the file list is correct, click "Next" at the bottom to enter the processing options settings.

Operation Step 3: Use Regular Expression to Extract the Number After Book ID

The second step is "Set processing options," which is the most critical part of this batch rename. The "Search Area" in the interface provides choices like "First line of text" and "Text matched by custom formula." Since the Book ID in the example files is located on the first line, and we want to use regex to precisely extract the number after the colon, we select "Text matched by custom formula."

image-Batch rename TXT files using text content,rename files based on text content,extract filenames with regular expressions

In the "Regular Expression" input field, fill in: (?<=Book ID:)\d+. This expression can be understood by breaking it down: (?<=Book ID:) indicates that the position being matched must be preceded by the fixed text "Book ID:", but this fixed text itself is not included in the result; \d+ means match one or more digits. Therefore, when the text "Book ID:4829173056" appears in the file content, the software extracts "4829173056" as the result, not the entire line containing "Book ID".

In the "Position" area, select "Overwrite entire file name." The purpose of this setting is to replace the main part of the original file name with the regex match result. Looking at the post-processing effect, the file name becomes the numeric ID plus the original extension, such as 4829173056.txt. For this example, this is exactly the result we need. If your actual requirement is to add the extracted content before or after the original file name, you can also choose "To the left of file name" or "To the right of file name" according to the position options in the interface, but this tutorial uses overwriting the original file name as an example.

Operation Step 4: Continue the Wizard and Start Batch Processing

After setting the regular expression and file name position, click "Next." The process prompt at the top of the interface shows that "Set save location" and "Start processing" are the subsequent steps. Here, it is recommended to choose a suitable save method based on your work habits: if these are formal files, it's best to back up the original directory before processing; if you are testing a rule, it's advisable to first verify the expression with a small number of files before expanding to the entire folder.

After entering the start processing stage, the software will read the text content one by one according to the list, use the regular expression to find the matching text, and write the matched Book ID number into the file name. After processing is complete, return to the folder to see that the file names have been changed in batch. Compared to manually opening, copying, pasting, and renaming, wizard-based batch processing can compress a large number of repetitive actions into a single configuration.

Common Problems and Precautions

1. Why didn't the regular expression match any content? Please check if the text does indeed contain "Book ID:", paying attention to whether the colon is an English colon or a Chinese colon, and if the case is consistent. If the actual content is BookID: or Book ID:, the expression needs to be adjusted accordingly.

2. What if the file content is not on the first line? If the number is not fixed on the first line, you can choose an appropriate method based on the search area options provided in the software interface and match the target text using a custom regex. The key principle is to make the expression match unique and stable content as much as possible.

3. What if the same number is extracted from multiple files? Before batch renaming, you should try to confirm the uniqueness of the extraction results. If the same Book ID appears in the content of multiple files, it may cause a naming conflict risk. It is recommended to perform a sample check first or back up the files before formal processing.

4. Can doc, docx, or PDF files be processed? This article demonstrates "Rename Text Files Using File Content," which is applicable to txt text files. In the interface, you can also see related functions like using file content to rename Word files, PDF files, etc. If your files are doc, docx, or PDF, you should choose the corresponding function, not the text file function.

Summary: Using Regex to Batch Rename txt Files Can Significantly Reduce Repetitive Work

When the file content already contains usable numbers, titles, or business fields, continuing to manually copy and rename each one individually is not efficient. Using the "Rename Text Files Using File Content" function of HeSoft Doc Batch Tool , you can first batch import txt files, then use the regular expression (?<=Book ID:)\d+ to extract the number after Book ID, and finally overwrite the original file name with the match result. The entire process is clear and reusable, especially suitable for organizing large volumes of text materials in office scenarios. It is recommended that you test the rule with 3 to 5 files first, confirm the processing results meet expectations, and then execute the batch processing on the complete folder.


Keyword:Batch rename TXT files using text content , rename files based on text content , extract filenames with regular expressions
Creation Time:2026-07-08 06:33:34

Disclaimer: All images, text, and video content on the website are for reference only and may not be the latest, correct, or accurate. In case of any dispute, please refer to the actual experience effect!

Related Articles

Don't see the feature you want?

Provide us with your feedback, and after evaluation, we will implement it for free!