Many TXT text files, after being downloaded or generated, only have temporary names like book1 or book2, while the truly valuable numbers, titles, or business fields are inside the file content. This article uses extracting the Book ID from the first line of text as an example to explain how to use HeSoft Doc Batch Tool to match numeric identifiers from the content of multiple text files through wildcards/regular expressions and batch overwrite the original file names, achieving standardized naming from book1.txt to 4829173056.txt, reducing the repetitive work of opening, copying, pasting, and renaming files one by one.
When organizing e-book files, scraping text with crawlers, or managing logs or archive texts exported from business systems, a common issue often arises: filenames have no practical meaning, often just sequential names like book1.txt, book2.txt, book3.txt. However, upon opening the file, the first line or a fixed location in the body might contain the identifier that should truly serve as the filename, such as Book ID:4829173056. If there are only a few files, manually opening them, copying the identifier, closing the file, and renaming it is acceptable. But with dozens, hundreds, or even more text files, this repetitive operation is not only time-consuming but also prone to copying errors, omissions, or overwriting mistakes.
This article aims to solve this specific problem: using the batch processing capabilities of office software to extract required fields from TXT text content via regular expressions, and then batch-writing these fields into filenames. In the example, we will extract the Book ID number from the body of multiple .txt text files and batch-rename the original names like book1.txt, book2.txt to numbered filenames such as 4829173056.txt, 1958436720.txt. The operating tool, as shown in the screenshot, is HeSoft Doc Batch Tool . It is positioned as a batch file processing software for office scenarios, suitable for handling repetitive tasks related to file naming, document sorting, text extraction, and various file types like Word, PDF, Excel, and PowerPoint.
Applicable Scenarios: When is it suitable to batch rename TXT files using file content?
This method is highly suitable for batch renaming whenever there are fields with stable formats in the file body. Especially in the following office scenarios, using wildcards or regular expressions for batch renaming of text files is very efficient.
First, organizing e-books or article texts. The body of some TXT files contains information like book IDs, article IDs, chapter numbers, or titles, but the filenames are just temporary names generated during download. Using regex to extract Book ID, title, or number allows for quickly establishing a uniform naming convention, making subsequent retrieval easier.
Second, managing system export texts, logs, or reports. Filenames exported by many business systems might be serial numbers, timestamps, or meaningless names, while the file content contains key fields like customer number, order number, contract number, or device number. Batch-changing these fields to filenames can reduce manual sorting and search time.
Third, organizing files after crawler collection or automatic generation. Collection programs often generate files like 1.txt, 2.txt, book1.txt, but the body contains more accurate IDs or titles. Using a batch rename tool can serve as a post-collection cleanup step, improving the quality of file archiving.
Fourth, archive management and data filing. When the file body contains a unique identifier, using it as the filename reduces the risk of duplicate names and facilitates future matching with databases, Excel spreadsheets, or other systems.
The focus of this tutorial is not simply "batch replacing filename keywords," but "generating filenames based on file content." Handling such a requirement manually typically involves five steps: opening the file, locating the field, copying the content, closing the file, and renaming the file. Using an office batch processing tool, these repetitive actions can be consolidated into a single rule setup.
Preview of Results: TXT files and body structure before processing
Before processing, the folder contains multiple TXT text files named book1.txt to book5.txt. These names only indicate sequence and do not directly reveal the actual ID or content corresponding to each file.

Opening one of the text files reveals that the information truly needed for naming is located in the body. The first line of the sample file is Book ID:4829173056, and the red arrow points to the numeric ID that needs to be extracted. The article title and body text follow, but this renaming task only requires extracting the consecutive digits after "Book ID:".

In other words, the rule target is very clear: find the number immediately following "Book ID:" in each TXT file's content, and use this string of digits to overwrite the original filename while retaining the .txt extension. For a regular expression, this can be achieved with a "lookbehind" assertion, for example, (?<=\bBook\b \bID\b:)\d+, which means matching a segment of digits that is immediately preceded by "Book ID:".
Preview of Results: Filenames become the IDs from the body after processing
After processing, the original filenames like book1.txt, book2.txt have been changed to the numeric IDs extracted from their respective bodies. The screenshot shows that the filenames have become formats like 1958436720.txt, 4829173056.txt, 6094728315.txt, 7305619482.txt, 8640295173.txt.

The value of this result lies in the fact that the filename itself now carries key index information, eliminating the need to open each file to check the ID. For subsequent tasks like retrieval by ID, system import, matching with an Excel list, sending to colleagues, or backup archiving, this is much clearer than temporary names like book1.
Operation Steps: Using HeSoft Doc Batch Tool to extract filenames from text content
The complete operation is described below according to the sequence of screenshots. To avoid operational errors, it is recommended to first copy a test folder and confirm the rule is correct before processing official files. Especially when using "Overwrite the entire filename," the rule matching result will directly determine the new filename.
Step 1: Enter the "File Name" category and select to rename text files using file content
After opening HeSoft Doc Batch Tool , you can see multiple office file processing categories on the left, such as File Name, Folder Name, File Sorting, Word Tools, Excel Tools, PowerPoint Tools, PDF Tools, Text Tools, etc. This article needs to process filenames, so select the File Name category on the left.
Among the file name related functions, find and click "Rename text files using file content". In the screenshot, this function is item 8, and its description indicates it is for batch using certain text from a text file's content as that file's filename, which perfectly matches our requirement.

The purpose of this step is to enter the batch renaming workflow specifically for TXT text files. Different from ordinary filename find-and-replace, this function reads the internal content of text files and then generates a new filename based on the set matching rule.
Step 2: Add the TXT files to be processed and confirm the file list is correct
After entering the function page, the top of the interface displays the current function name "Rename text files using file content". The first step is "Select records to process". You can manually select multiple TXT files using the "Add Files" button at the top, or use "Import files from folder" to import files from a specific folder all at once. The screenshot shows that 5 text files have been imported, with the list displaying information like sequence number, name, path, extension, creation time, and modification time.

In this list, check three key points: first, whether the number of files is correct, as shown by the record count of 5 at the bottom of the screenshot; second, whether the extension is txt; third, whether the path is the intended folder for processing, such as the files in the screenshot located in the D:\test\ directory. If you find that unneeded files have been imported, you can use the delete icon on the right side of each row to remove them; if you need to re-select, you can also use the "Clear" button at the top to empty the current list and then add files again.
After confirming the list is correct, click "Next" at the bottom to enter the processing options settings. At this point, the software will use these files as the objects for the upcoming batch reading and renaming.
Step 3: Set the search area to text matched by a custom formula
The second interface is "Set processing options". In the "Search Area" section, you can see options, including "First line of text" and "Text matched by a custom formula". In this example, "Text matched by a custom formula" is selected because we only want to extract the number after "Book ID:", not use the entire line "Book ID:4829173056" as the filename.

The purpose of this step is to tell the software not to simply take the complete text of the first line, but to use a regular expression for precise matching. For TXT files with a fixed structure, this method is more reliable than manual truncation and more suitable for batch processing.
Step 4: Enter the regular expression to extract the number after Book ID
Fill in the rule in the "Regular Expression" input box: (?<=\bBook\b \bID\b:)\d+. This expression can be understood in two parts: (?<=\bBook\b \bID\b:) means asserting that the position is immediately preceded by "Book ID:", but without including "Book ID:" itself in the result; \d+ means matching one or more digits. Therefore, when "Book ID:4829173056" appears in the text, the final extraction result is "4829173056".
If the field format in your text is slightly different, you can adjust it according to the actual content. For example, if the field is ID:12345, the expression needs to be changed to match the numbers after "ID:"; if there is a space in the middle, like "Book ID: 4829173056", the expression should also handle the space accordingly. This article's example is based only on the format shown in the screenshot, and direct application to texts with completely different formats is not recommended.
Step 5: Choose to overwrite the entire filename and proceed with the subsequent workflow
In the "Position" area, "Overwrite the entire filename" is selected in the screenshot. This means the software will use the text extracted by the regular expression to replace the original filename body. For example, if the original filename is book2.txt and the extraction result is 4829173056, it will become 4829173056.txt after processing. The extension remains as the TXT file extension here, facilitating continued opening as a text file.
If "On the left of the filename" or "On the right of the filename" is chosen, it is more suitable for appending the extracted content as a prefix or suffix to the original filename. However, the goal of this tutorial is to completely replace the original temporary name with the body's ID number, so selecting "Overwrite the entire filename" better meets the requirement. After completing the setup, click "Next", and follow the interface flow to continue setting the save location and start processing. The flow bar at the top of the screenshot shows subsequent steps are "Set save location" and "Start processing," indicating the software uses a step-by-step confirmation approach, which helps in checking the rules before batch execution.
FAQ and Precautions
1. Why does the regular expression only extract numbers and not include the "Book ID" text?
Because the expression uses a "lookbehind" assertion, like (?<=\bBook\b \bID\b:), it only specifies that the matched content must be preceded by "Book ID:" but does not include this fixed text in the match result. Only the digits matched by \d+ are output to the filename. This avoids filenames like Book ID4829173056.txt or including characters like colons that are unsuitable for filenames.
2. What if some TXT files do not have a "Book ID"?
If a file has no content matching the rule, the software may not be able to generate a target name for that file. It is recommended to spot-check several texts before processing to confirm the field format is consistent; you can also test with a small number of files after batch importing. For texts with inconsistent formats, you can first unify the content format or adjust the regular expression according to the actual situation.
3. Will there be a filename conflict during batch renaming?
If the Book IDs extracted from two text files' bodies are exactly the same, the problem of duplicate target filenames may occur. The uniqueness of the ID should be confirmed before batch processing. For scenarios where uniqueness is uncertain, consider first appending the ID to the left or right of the original filename, and after confirming correctness, proceed with a more thorough renaming plan.
4. Why is it recommended to copy the folder before operating?
Batch processing files is highly efficient, but if a rule is written incorrectly, it can quickly affect a large number of files. Especially operations like "Overwrite the entire filename" will directly replace the original name. For safety, it is recommended to first copy a test directory, verify that the extraction results, save location, and naming rules are all correct, and then process the official data.
5. Is this method only applicable to TXT files?
The tutorial demonstrates "Rename text files using file content," so the focus is on .txt text files. The software interface also shows tool categories for Word, PDF, Excel, and related entries for renaming Word files, PDF files using file content, but the reading methods and applicable functions differ for different formats. The corresponding tool should be selected based on the actual file type.
Summary: Turning file content into filenames for more efficient batch TXT filing
Through the example in this article, it can be seen that when the real identifiers of many text files are hidden in the body, manually opening and renaming one by one is not an efficient approach. With the "Rename text files using file content" feature of HeSoft Doc Batch Tool , you can first import TXT files in batches, then use the regular expression (?<=\bBook\b \bID\b:)\d+ to extract the ID from the body, and finally overwrite the original filename with the extraction result.
This method is particularly suitable for office scenarios such as organizing e-book IDs, archiving textual data, cleaning system-exported files, and standardizing log file naming. It transforms repetitive, mechanical, and error-prone operations into a single rule setup and batch execution, saving time and improving the accuracy of file management. It is recommended that you prepare a few sample files to test the rules before formal processing, and proceed with batch renaming in the software for the entire set of TXT files only after confirming the effect matches expectations.