Batch rename txt files with text content: regular expression to extract Book ID to generate file name


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

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 folder contains a large number of txt text files, but the file names are only temporary names like book1, book2, subsequent retrieval, archiving, and delivery become inefficient. This article demonstrates how to use HeSoft Doc Batch Tool , through the "Rename text files using file content" feature, using regular expressions to extract the number after Book ID from the text body and batch generate new txt file names, avoiding opening, copying, pasting, and renaming one by one.

When organizing e-book materials, article materials, log files, exported data, or batch-generated txt documents, a common situation arises: filenames have no business meaning, such as book1.txt, book2.txt, book3.txt, but the file content contains truly valuable IDs, titles, customer numbers, order numbers, or material IDs. Manual handling requires opening each text file one by one, finding the target content, copying it out, and then returning to the folder to rename it. With a large number of files, this is not only time-consuming, but also prone to copying errors, missed changes, or overwriting files.

This article addresses the problem of "batch renaming txt files based on text file content." In the example, the first line of each txt file content contains information like Book ID:4829173056. We want to extract the number after Book ID: and use it as the new filename, resulting in files like 4829173056.txt which are easier to search and manage. The operation uses a wildcard/regex matching approach, suitable for office scenarios requiring batch file processing to reduce repetitive labor.

The software used in this tutorial is the one shown in the screenshot, " HeSoft Doc Batch Tool ". It is positioned as a batch file processing tool within office software, focusing not on editing individual documents but on centralizing repetitive file organization, file renaming, and document processing actions into one workflow, thereby improving daily office efficiency.

Applicable Scenarios: Which text files are suitable for content-based batch renaming

"Rename text files using file content" is suitable for processing txt files with relatively stable content structures. As long as each file contains an identifiable field, it can be extracted as a filename using rules. For example, if the file body contains information like Book ID, Article ID, order number, customer number, contract number, device number, or log serial number, this method can be considered.

From a practical office perspective, it is particularly suitable for the following scenarios:

  • Batch-downloaded or exported txt materials, where the original filenames are just serial numbers, but the body contains a unique ID.
  • E-books, articles, and material packages need to be archived by book number, title number, or internal code.
  • System-exported log texts need renaming by serial number, task number, or device number.
  • A large number of text files need to be delivered to colleagues or clients, with filenames directly reflecting the content.
  • The original filenames are unsearchable, requiring files to be opened first to know their content, affecting lookup efficiency.

Note that this example processes txt text files. If it's a Word document, it might involve formats like doc or docx; if it's a PDF file, the corresponding "Rename Word files using file content" or "Rename PDF files using file content" functions typically apply. Different file types have different reading methods, and choosing the corresponding tool can reduce recognition errors.

Effect Preview: Filenames without business meaning before processing, become IDs directly after processing

Before Processing: The folder only contains temporary names like book1, book2

From the file list before processing, we can see there are 5 txt files in the current folder, named book1.txt, book2.txt, book3.txt, book4.txt, book5.txt. These names only indicate the file order and cannot directly determine which book, record, or number each file corresponds to. If you need to find the file corresponding to a specific Book ID later, you must open each one to confirm.

image-Batch rename TXT files,rename files based on text content,regular expression renaming,wildcard file renaming,HeSoft Doc Batch Tool

Opening one of the text files, we can see the first line of the body contains Book ID:4829173056. The number pointed to by the red arrow in the screenshot is the content we want to extract as the filename. The body also contains a title and article content afterward, but for this renaming, only the number after Book ID: in the first line is needed.

image-Batch rename TXT files,rename files based on text content,regular expression renaming,wildcard file renaming,HeSoft Doc Batch Tool

After Processing: Filenames are batch replaced with the digital IDs from the body

After batch processing completes, the original filenames like book1.txt, book2.txt have been changed to 1958436720.txt, 4829173056.txt, 6094728315.txt, 7305619482.txt, 8640295173.txt. This means the software extracted the corresponding Book ID number from each txt file's content and wrote it into the filename position, while the extension remains txt.

image-Batch rename TXT files,rename files based on text content,regular expression renaming,wildcard file renaming,HeSoft Doc Batch Tool

The benefit of this naming convention is clear: the filename itself expresses key content, allowing direct searching by number in the folder later, and it is more suitable for archiving, uploading, comparison, and batch delivery. Compared to manually opening and renaming each file individually, batch processing significantly reduces repetitive operations.

Operation Steps: Extract Book ID using Regex and Batch Rename txt Files

Step 1: Enter the "File Name" tool and select the text file renaming function

After opening HeSoft Doc Batch Tool , select the "File Name" category in the left navigation bar. This category centrally houses functions related to batch filename processing, such as find/replace filename, insert text, add prefix/suffix, delete filename text, etc. Since we need to generate filenames based on file content this time, select "8. Rename text files using file content" on the page.

image-Batch rename TXT files,rename files based on text content,regular expression renaming,wildcard file renaming,HeSoft Doc Batch Tool

The purpose of this step is to enter the content extraction and renaming workflow specifically for txt text files. The screenshot shows the function card is selected, indicating we are not simply replacing characters in the original filename, but reading the text file content and using specified text from the content as the new filename.

Step 2: Add the txt files to be processed

After entering the function, the interface top displays the current function name "Rename text files using file content," and shows the process flow: "Select records to process, Set processing options, Set save location, Start processing." In the first step, you can select single or multiple txt files via "Add Files," or batch import files from a folder via "Import files from folder."

image-Batch rename TXT files,rename files based on text content,regular expression renaming,wildcard file renaming,HeSoft Doc Batch Tool

The screenshot shows 5 records have been imported, with the table listing information like serial number, name, path, extension, creation time, and modification time. Here, you can first check if the files are complete, e.g., confirming book1.txt to book5.txt are all listed, the path is the expected D drive test directory, and the extensions are all txt. If files are added by mistake, you can remove them via the delete button in the action column.

After confirming the file list is correct, click "Next" at the bottom to enter processing option settings. The expected result of this action is: the software retains the current pending file list and proceeds to the content matching rule configuration page.

Step 3: Choose custom formula matching text, and enter the regex

On the "Set processing options" page, you need to tell the software: from which area of the text file and using which rules to extract the new filename. In the screenshot, the "Search area" provides options like "First line text" and "Text matched by custom formula." This example selects "Text matched by custom formula" because we only want to extract the number after Book ID:, not use the entire line "Book ID:4829173056" as the filename.

image-Batch rename TXT files,rename files based on text content,regular expression renaming,wildcard file renaming,HeSoft Doc Batch Tool

In the "Regex" input box, the example filled in is:

(?<=Book ID:)\d+

The meaning of this regex can be understood in parts:

  • Book ID: is the fixed prefix in the body, used to locate the position where the target number appears.
  • (?<=Book ID:) indicates matching a position immediately preceded by Book ID:, but not including Book ID: itself in the result.
  • \d+ means matching one or more digits.

Therefore, when Book ID:4829173056 appears in the text, only 4829173056 is extracted, not including Book ID:. This is exactly the filename body we want.

Further down the same page is the "Position" setting; the screenshot shows "Overwrite entire filename" is selected. This means the extracted content will replace the original filename body. For example, if the original filename was book2.txt and the extracted number is 4829173056, the result after processing becomes 4829173056.txt. This avoids appending unnecessary characters before or after the original name, keeping the filename clean and uniform.

Step 4: Set the save location and start processing

After completing the matching rule, click "Next". Following the interface flow, you will then enter the "Set save location" and "Start processing" stages. The save location determines where the processing results are written. It is recommended to choose an appropriate method based on actual needs: if you want to keep the original files for verification, save to a new folder; if you are confident in the rules and want to directly organize the current directory, choose the corresponding save location as prompted by the interface.

Before clicking start processing, it's advisable to double-check three points: first, whether the number of imported files is correct; second, whether the regex can match the target content in each file; third, whether duplicate filenames might occur after renaming. Execute the processing after confirming these are correct. After processing is complete, you can check the results in the target folder, where filenames will have changed to the numeric IDs extracted from the txt content.

Regex Writing Guide: How to adapt to your own field rules

The expression used in this example is suitable for the format Book ID:number. If your text fields differ, you can adjust based on the actual content. For instance, if the text has ID:123456, consider using (?<=ID:)\d+; if it's Order No:20260506001, you can rewrite it based on the fixed prefix to a rule matching the order number.

If there might be spaces after the field, for example, Book ID: 4829173056, you need to account for the space in the expression. Regex is quite sensitive to spaces, case, and punctuation, so it's recommended to test with a few files first, and process in bulk only after confirming the extraction results meet expectations.

From the perspective of office software usage, the value of wildcards and regex lies in handing over "fixed patterns" to the tool. As long as the file content structure is consistent, there is no need for manual, individual judgment. For hundreds or thousands of txt files, this method can compress hours of repetitive operation into minutes.

Common Issues and Precautions

1. Why didn't the filename change after processing?

A common reason is that the regex did not match any content. For example, if the body actually says BookID: but the expression uses Book ID: with a space, the match will fail. It is recommended to open a few sample files to confirm the field format is completely consistent.

2. Can I extract only the first line?

The "Search area" in the screenshot can select "First line text" or "Text matched by custom formula". If the entire line is the desired filename, you can choose first line text; if the first line also contains prefixes, explanatory text, or multiple fields, it's recommended to use a custom formula to extract only the needed part via regex.

3. Will the txt extension be lost?

From the processing results, the filename body was replaced with a digital ID, while the extension remained txt. This helps keep the file type unchanged, and it can still be opened normally with a text editor after processing.

4. What if two files extract the same ID?

Before batch renaming, try to confirm the extracted field is unique. If the same Book ID appears in the content of multiple files, it might cause a filename conflict. It's recommended to test with a few files first, or back up the original files before formal processing.

5. Is it suitable for files like doc, docx, PDF?

This tutorial demonstrates text files (txt). If you want to process Word documents (like doc, docx) or PDF files, you should choose the corresponding Word file or PDF file content renaming function within the software. Different formats have different content reading methods, so choosing the correct entry point is safer.

Summary: Turning file content into filenames reduces massive repetitive organizing work

Using the "Rename text files using file content" function of HeSoft Doc Batch Tool , the operations that originally required manual opening, finding, copying, and renaming can be transformed into a single batch processing workflow. In this article's example, the software uses the regex (?<=Book ID:)\d+ to extract the number after Book ID from each txt file and uses it as the new filename, achieving the batch conversion from meaningless names like book1.txt to numeric ID filenames.

If you are organizing a large number of text files and the file content already contains numbers, titles, or other fields that could serve as filenames, it's recommended to first select a few samples to test the matching rule, then import files in bulk for processing. This ensures naming accuracy while significantly improving office efficiency, making file archiving, searching, and delivery clearer and more controllable.


Keyword:Batch rename TXT files , rename files based on text content , regular expression renaming , wildcard file renaming , HeSoft Doc Batch Tool
Creation Time:2026-07-07 06:37:52

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!