Batch Rename TXT Text Files by Content: Use Regular Expressions to Extract Book ID as Filename


Translation:EnglishFrançaisDeutschEspañol日本語한국어,Update Time:2026-07-09 06:37:20

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 a large number of TXT text files have temporary names like book1, book2, and the truly valuable numbers, titles, or business IDs are saved in the file content, manually opening, copying, pasting, and renaming them is very time-consuming. This article uses HeSoft Doc Batch Tool as an example to demonstrate how to use the "Rename Text Files Using File Content" feature, extracting the number after Book ID from the text content using regular expressions and batch overwriting the original file names, which is suitable for scenarios such as data archiving, e-book organization, and log number management.

When organizing a large number of TXT text files, many people encounter a similar problem: a folder contains files with names that have no practical meaning, such as book1.txt, book2.txt, book3.txt. However, upon opening the files, you discover that each text file contains a number that should actually serve as the file name, such as "Book ID:4829173056". If you only have a few files, manually opening them, copying the number, closing the file, and renaming it is still manageable. But once you have dozens, hundreds, or even more TXT files, this repetitive operation is not only time-consuming but also prone to copy errors, omissions, or accidentally deleting the file extension.

This article addresses the office scenario of "how to batch rename many text files based on their content." We will use the batch file processing capability of the office software " HeSoft Doc Batch Tool " to match specified text from TXT content using wildcards/regular expressions, and then use the matching results as the new file names. In the example, we will extract the number following "Book ID:" in the first line of each text file and batch rename the original names like book1.txt, book2.txt to more identifiable names like 1958436720.txt, 4829173056.txt.

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

Batch renaming by file content is suitable for situations where "the file name is unimportant, but the file content contains a key identifier." Common scenarios include:

  • E-books, articles, and material texts that contain Book ID, Article ID, serial numbers, or titles, which need to be used as the TXT file name.
  • Exported text files like logs, orders, customer data, or contract summaries, where the file name is a system default number, but the file body contains a more accurate business number.
  • TXT files from batch downloads or generation, with temporary names like book1, book2, data001, which need to be archived according to a unique ID in the content.
  • Needing to use regular expressions to extract fixed-format text, such as "Book ID:number," "Order No.:number," "Serial No.:letters+numbers," etc.

The common characteristic of these tasks is: clear rules, a large number of files, and repetitive manual operations. As a batch processing software designed for office scenarios, HeSoft Doc Batch Tool is suitable for entrusting this kind of repetitive labor to a tool, reducing the time cost of opening files one by one and manually renaming them.

Effect Preview: File Name Changes Before and After Processing

Before Processing: File names are only temporary names like book1, book2

From the file list before processing, you can see that there are 5 TXT files in the folder, named book1.txt, book2.txt, book3.txt, book4.txt, and book5.txt. These file names only indicate a sequence and do not directly show the actual Book ID or business number corresponding to each text.

image-Batch rename TXT,rename by file content,rename text files with regular expressions

Opening one of these text files, you can see that the first line of the body contains "Book ID:4829173056". This indicates that the content truly suitable for the file name is not in the original file name but within the text content. The red arrow in the screenshot points to the key number that needs to be extracted.

image-Batch rename TXT,rename by file content,rename text files with regular expressions

After Processing: File names are batch changed to the Book ID numbers from the text content

After batch processing is complete, the original file names like book1.txt, book2.txt have been changed to the digital numbers extracted from the file content, such as 1958436720.txt, 4829173056.txt, 6094728315.txt, 7305619482.txt, and 8640295173.txt. This way, you can directly identify the number corresponding to each TXT file from its name without opening it.

image-Batch rename TXT,rename by file content,rename text files with regular expressions

This processing method is particularly suitable for subsequent retrieval, sorting, archiving, and batch uploading. Compared to manually copying and renaming one by one, the software completes matching, extraction, and renaming in one go, significantly reducing repetitive work.

Operation Steps: Using Regular Expressions to Extract TXT Content as File Names

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

After opening HeSoft Doc Batch Tool , you can see different tool categories on the left side. This article deals with file name-related tasks, so select the "File Name" category on the left. In the main function area, find "8. Rename text files using file content." This function's description is to batch-use certain text in the content of text files as the file name, which perfectly matches this requirement.

image-Batch rename TXT,rename by file content,rename text files with regular expressions

The purpose of entering this function is to tell the software: this time, it is not about ordinary find-and-replace for file names, nor adding prefixes or suffixes to file names, but about extracting text from TXT file content and using the extraction result to rename the file.

Step 2: Add the TXT Files for Batch Processing

After entering the function page, the top of the interface shows the current function is "Rename text files using file content." In the first step, "Select records to process," you can import single or multiple TXT files via "Add Files," or import text files from a folder at once via "Import Files from Folder."

image-Batch rename TXT,rename by file content,rename text files with regular expressions

The screenshot shows 5 imported records, with the table listing information like sequence number, name, path, extension, creation time, and modification time. Here you can check if the files are completely imported, for example, if book1.txt through book5.txt are all in the list; if you have imported files that do not need processing, you can also remove them using the operation column on the right. After confirming the file list is correct, click "Next" at the bottom to enter the processing options setup.

Step 3: Set the Search Area, Choose Text Matched by Custom Formula

In the second step, "Set processing options," you must first determine which part of the text file to extract content from. The interface provides options like "First line of text" and "Text matched by custom formula." In this example, although the Book ID appears in the first line, we want to precisely extract the number following "Book ID:", not the entire line as the file name, so we choose "Text matched by custom formula."

image-Batch rename TXT,rename by file content,rename text files with regular expressions

The purpose of this setting is to let the software search for segments matching the rule in the text content based on the entered regular expression and only use the matched part for the file name. For TXT files containing fixed prefixes, numbers, titles, or other structured content, this method is more flexible than directly using an entire line of text.

Step 4: Fill in the Regular Expression to Extract the Number After Book ID

In the "Regular Expression" input box, the example filled in is:

(?<=Book ID:)\d+

The meaning of this expression can be simply understood as: find the continuous numbers located after "Book ID:". Among them, (?<=Book ID:) specifies that the numbers must be preceded by "Book ID:", and \d+ means matching one or more digits. Ultimately, the software will only use the number part as the file name, without including "Book ID:" in the file name.

For example, if the first line of text is "Book ID:4829173056", the result matched by this regular expression is "4829173056". After processing, the file name will become 4829173056.txt. Since the extension remains txt, the file type will not change.

Step 5: Set the New File Name Position to Overwrite the Entire File Name

Further down on the same settings page, you can see the "Position" option, including "Overwrite the entire file name," "To the left of the file name," and "To the right of the file name." This example requires completely replacing the original names like book1, book2 with the Book ID, so choose "Overwrite the entire file name."

If you choose "To the left of the file name" or "To the right of the file name," the matched number will be inserted on either side of the original file name, which is more suitable when you want to keep the original name and add identification information. The goal of this article is to directly use the number from the TXT content as the file name, so the "Overwrite the entire file name" setting should be maintained.

Step 6: Continue to Next Step and Start Processing

After completing the regular expression and position settings, click "Next." The interface process also includes the "Set save location" and "Start processing" phases. Choose the save method based on your actual needs, and then proceed to the start processing step. After processing is complete, check the target folder for the results, and you will see that the original files like book1.txt have been batch renamed to the extracted Book ID numbers.

It is recommended to test a small number of samples for the accuracy of the regular expression match before formally processing a large number of files. Confirm that the output file names are as expected before importing the full folder for batch execution; this is a more secure approach.

Common Questions and Precautions

1. Why use regular expressions instead of directly using the first line of text?

If the first line only contains a pure number, then using the first line of text directly is also fine. But in this example, the first line is "Book ID:4829173056", where "Book ID:" is just descriptive text, and what we really want as the file name is the number following it. Using a regular expression allows precise extraction of the target segment, avoiding unnecessary prefixes in the file name.

2. Does the Book ID in the regular expression need to be exactly the same as in the text content?

Generally, it needs to be consistent, including spaces, capitalization, and colon format. For instance, if the text says "Book ID:4829173056", the expression should correspond to "Book ID:". If some files are written with a Chinese colon like "Book ID:", or "BookID:" without a space in between, the matching rule needs to be adjusted.

3. What if no number is matched in a file's content?

If a certain TXT file does not contain content that matches the rule, the software cannot extract a new file name based on that rule. Therefore, before batch processing, confirm that the sample formats are consistent, or filter out text files with abnormal formats first. For files from complex sources, it is advisable to spot-check a few samples of their content first.

4. Is a backup needed before batch renaming?

When batch modifying file names is involved, it is recommended to keep a backup of the original files, especially when using regular expressions to process important materials for the first time. Batch processing software can greatly improve efficiency, but the premise is that the rules are set correctly. A backup can reduce the recovery cost brought by operational errors.

5. Is this method only applicable to TXT files?

This article demonstrates the "Rename text files using file content" function, which is applicable to text file scenarios. The software interface also shows similar function entries for renaming Word files and PDF files by content, but different file types should use the corresponding tools; for example, do not directly apply the TXT tool to doc, docx, pdf, and other documents.

Summary: Hand Over Repetitive Renaming to Batch Processing Tools

With HeSoft Doc Batch Tool , the repetitive workflow of "open TXT, find Book ID, copy number, manually rename" can be transformed into an automated operation of importing files at once, filling in a regular expression, and executing in batch. For dozens or hundreds of text files, this approach not only saves time but also reduces errors in manual copying and naming.

If your TXT files contain fixed-format numbers, titles, order numbers, or other key fields, and you wish to use this content as file names, it is recommended to prepare a few sample texts, confirm the regular expression match is correct, and then use the "Rename text files using file content" function for batch processing. This allows you to complete file archiving more efficiently, making file names truly serve the purposes of searching and management.


Keyword:Batch rename TXT , rename by file content , rename text files with regular expressions
Creation Time:2026-07-09 06:37:05

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!