Many resource-type txt files generate meaningless names like book1.txt, book2.txt when exported, but the file body often contains a Book ID, serial number, or sequence number. This article describes how to use HeSoft Doc Batch Tool , through the "Rename text files using file content" feature and regular expressions, to extract the numbers following the Book ID from each txt file and batch overwrite the original filenames. The tutorial includes before-and-after effects, operation steps, regex rule explanations, and notes, suitable for office scenarios requiring efficient organization of a large number of text files.
In daily office work and data management, txt files often come from batch exports, system downloads, scraping and organizing, or content conversion. The exported filenames might be book1.txt, book2.txt, book3.txt, which look neat but do not express the file content. The truly useful information is often hidden inside the file. For example, the first line reads Book ID:4829173056. If you need to archive these files by Book ID, manual processing would be very tedious.
This article will demonstrate a more suitable approach for batch processing: using the office software HeSoft Doc Batch Tool to extract specified numbers from text file content via regular expressions, and then use the extracted results as new filenames. The focus of this method is not simply renaming, but "batch renaming txt files based on their content", suitable for processing large volumes of similarly structured text documents.
Applicable Scenarios: When File Content is More Suitable as a Name Than the Original Filename
The original names of some files are just temporary numbers and are not suitable for long-term storage. For example, system-generated book1.txt, book2.txt only reflect the file creation order, not the book ID, document number, or business serial number. If you later need to search by number or upload the files to another system, temporary filenames will incur management costs.
When a stable field exists inside a text file, you can consider renaming the file using its body content. Typical examples include: each txt file has a Book ID on the first line; each log file has a device number at the beginning; each exported record contains an order number; each customer data text has a customer ID; each instruction document contains a product code. As long as the formats of these fields are relatively uniform, batch matching can be performed using regular expressions.
The example in this tutorial is the number after Book ID, but you can extend this idea to other text fields. Compared to copying and pasting one by one, batch file processing tools can centralize repetitive actions into a single rule setting, which is especially suitable for office staff, data managers, operators, and content editors.
Effect Preview: From book-named to ID-named
Before Processing: Multiple txt filenames lack identifying information
From the screenshot before processing, you can see there are five text files in the folder, named book1.txt, book2.txt, book3.txt, book4.txt, book5.txt. Although these filenames are short, it is impossible to determine which Book ID each file corresponds to.

Opening one of the text files, you can see the first line of the file content contains Book ID:4829173056. The red arrow marks the numeric part that needs to be extracted. Our goal is to change the filename from book2 to 4829173056.

After Processing: Filenames directly use the numbers from the text
After processing is complete, the original temporary names are replaced by the numeric IDs from the body text. The file list becomes 1958436720.txt, 4829173056.txt, 6094728315.txt, 7305619482.txt, 8640295173.txt. This way, the filename itself reflects the key information, making subsequent searching, sorting, or archiving by ID more convenient.

From the before-and-after comparison, you can see that the batch renaming did not change the file extension; they are still txt files. What changed is the main filename, which is the part before the extension.
Operation Steps: Setting up Batch Renaming in HeSoft Doc Batch Tool
Step 1: Find the Text Content Renaming function under the File Name tool
After launching HeSoft Doc Batch Tool , there is a "File Name" category in the left navigation. After clicking this category, the right side will display multiple functions related to batch filename processing, such as find and replace keywords in filenames, insert text into filenames, add prefix and suffix to filenames, etc.
The item to use this time is the 8th one, "Rename text files using file content". The function card in the screenshot is already selected, and the tooltip explains that this function can batch-use certain text from text file content as the file's name.

The expected result of this step is to enter the specialized text file content renaming workflow, rather than using the ordinary filename replacement function. This is because ordinary replacement can only handle existing filenames and cannot read the Book ID from the txt body.
Step 2: Import the txt files to be renamed
After entering the function, the top of the page shows the processing workflow: Select records to process, Set processing options, Set save location, Start processing. First, you need to add the target files to the list.
You can click the Add Files button at the top of the page to select txt files individually or in batches; if all files are in the same directory, you can also use Import files from folder. After importing, the table will display information such as file name, path, extension, creation time, and modification time.

The screenshot shows 5 records have been imported, namely book1.txt through book5.txt. After confirming these are all the files to be processed, click Next at the bottom. If you find that wrong files were imported, you can remove the corresponding records via the delete icon in each row's operation column, or use the clear button on the interface to re-import.
Step 3: Specify the text to extract using a regular expression
On the "Set processing options" page, you first need to determine the search area. In the screenshot, Text matched by custom formula is selected. This means the software will not simply use the entire first line of text, but will search for the target content according to the regular expression we fill in.
The content filled in the regular expression input box is:
(?<=Book ID:)\d+

This rule can be understood by breaking it down: Book ID: is the fixed prefix appearing in the text; (?<=Book ID:) indicates that the match position must be after this prefix; \d+ means matching consecutive digits. Therefore, when Book ID:4829173056 appears in the text, the software will extract 4829173056 as the match result.
In the "Position" setting, choose Overwrite the entire filename. This way, the extracted Book ID will directly replace the original names like book1, book2. If you choose to insert to the left or right, the result would be appending content based on the original filename, rather than completely changing it to the ID name. The goal of this example is to make the filename equal to the number, so choosing overwrite the entire filename is more appropriate.
Step 4: Continue to the next step and execute batch processing
After setting the matching rule, click Next. Following the interface workflow, the subsequent steps will involve "Set save location" and "Start processing". Before officially starting, it is recommended to reconfirm whether the regular expression matches the text format, especially checking if the colon, space, and letter case are consistent with the file content.
After starting the process, the software will sequentially read the text files in the imported list, find the matched Book ID number, and write it into the filename. Once processing is complete, open the original folder to see the filenames have been batch updated.
Rule Expansion: What if Your Field is Not Book ID
This example uses Book ID: as the locating text. If your files use other field names, you can replace the fixed part in the regular expression. For example, if the text contains 订单号:20260506001, you can think about using "订单号:" as the prefix; if the text is ID=ABC123, you need to match the characters after the equals sign.
It should be noted that different fields might contain more than just numbers. If the number includes letters, you can adjust the digit matching part to a rule suitable for letters and numbers. This article will not expand on complex regex syntax; the core idea is: first find the marker that exists stably in every file, then match the content after the marker that will truly serve as the filename.
Frequently Asked Questions and Notes
What to do when content cannot be matched
If the processing results are not as expected, first check if the text file truly contains Book ID:. Then check if the colon is directly followed by digits. If the actual text is Book ID: 4829173056, with an extra space in the middle, you will need to adjust the rule.
Can I just take the first line of text
From the screenshot, you can see the search area also has a "First line text" option, but this example chose text matched by a custom formula. The reason is the first line contains both Book ID: and the number, but we only need the number part. Using a regular expression allows for more precise extraction of the required segment.
Is a backup needed before processing
Batch renaming is an operation that modifies filenames in batches. Although highly efficient, if the rules are set incorrectly, it can affect multiple files at once. It is recommended to copy a sample or backup the folder before processing important data, test with a small number of files first, and then process all files.
What is the impact of duplicate numbers
If two txt files have the exact same Book ID, this could cause a filename duplication problem. In practical work, Book IDs, order numbers, and archive numbers should usually be unique. You can do a spot check or use other methods before processing to confirm that the numbers are not repeated.
Summary: Leave the repetitive copying and renaming to the batch processing tool
Through the steps in this article, you can batch-extract Book ID numbers from multiple txt text files and automatically overwrite the original filenames. The entire process only requires selecting the function, importing files, filling in a regular expression, setting to overwrite the filename, and starting process—no need to open files one by one to copy the content.
For users who frequently need to organize text files, log files, and data files, HeSoft Doc Batch Tool provides a typical office automation capability: converting a large number of repetitive, error-prone manual operations into rule-based batch processing. It is recommended that you first verify the rules with a few sample files, and after confirming they are correct, batch-process the complete directory. This ensures accuracy while significantly improving file organization efficiency.