How to use regular expressions to extract TXT content and batch rename files


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

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 are still saved with temporary names like book1 or book2, manually opening each file, copying the identifier, and renaming it is highly inefficient. Using HeSoft Doc Batch Tool as an example, this demonstrates how to use the "Rename Text Files Using File Contents" feature to extract Book ID from text content via regular expressions and batch overwrite them as new file names. This is suitable for scenarios such as data archiving, e-book organization, log ID management, and standardizing naming conventions before data delivery.

When organizing a large amount of text data, the true identifying information for many files is not in the filename, but written within the file content. For example, what you see in a folder might be book1.txt, book2.txt, book3.txt, but upon opening them, you discover the first line contains a unique identifier like Book ID:4829173056. If you only have a few files, manually opening them, copying the ID, returning to the folder, and renaming is barely manageable; but when the number of files grows to dozens, hundreds, or more, this repetitive operation is not only time-consuming but also prone to copying errors, omissions, or overwrites.

This article aims to solve this typical pain point: using wildcard regular expressions to batch-extract content from many TXT text files and use that content to rename the corresponding text files. Using the office software HeSoft Doc Batch Tool shown in the screenshots as an example, it explains how to use the "Rename text files using file content" feature to batch-convert the Book ID within the text into filenames. The core value of this method is: set rules once, and the software automatically reads the content of each file to generate a new filename, significantly reducing repetitive work.

Applicable Scenarios: Which Files Are Suitable for Content-Based Batch Renaming

"Rename based on file content" is suitable for scenarios where filenames are irregular but a stable identifier exists within the file itself. As long as each text file contains a piece of content that can be matched by a rule, you can consider using regular expressions for batch renaming.

Common scenarios include:

  • E-book or article material organization: Filenames are book1.txt, book2.txt, but the text body contains unique information like Book ID, article number, or chapter number.
  • Data delivery file archiving: TXT, log, csv, and other text files contain order numbers, customer IDs, or batch numbers that need to be used as filenames.
  • Standardizing log or report files: The beginning of a file contains dates, device IDs, or task numbers, which one wishes to batch-change into more searchable names.
  • Secondary organization of downloaded files: Filenames are meaningless after downloading, but the file content includes a title, ID, or source information that can be used to generate a standard filename.

In this example, the filenames before processing are book1.txt through book5.txt, which don't reveal the actual Book ID corresponding to each file. The goal is to extract the number following Book ID: from each TXT file's content and use it as the new filename, for example, changing book2.txt to 4829173056.txt.

Preview of Results: Relationship Between Pre-Processing Filenames and Content

First, look at the file list before processing. The folder contains 5 TXT files named book1.txt, book2.txt, book3.txt, book4.txt, book5.txt. This type of naming is more like temporary serial numbers, making it impossible to directly determine the file content and inconvenient for subsequent retrieval, comparison, and archiving.

image-Batch TXT renaming,regular expression file renaming,text file content to file name,batch file processing

Opening one of these text files reveals the first line contains Book ID:4829173056. The red arrow in the screenshot points to the ID that needs to be extracted. In other words, the content truly suitable as a filename is inside the file, not in the current filename.

image-Batch TXT renaming,regular expression file renaming,text file content to file name,batch file processing

If handled manually, you would need to repeat the process of "Open file — Copy the digits after Book ID — Close file — Rename file" for each file. The repetition isn't obvious with 5 files, but becomes very apparent with larger quantities. After using HeSoft Doc Batch Tool , you can batch-match these IDs with a single regular expression, allowing the software to automatically complete the reading and renaming.

Post-Processing Results: TXT Files Batch-Renamed to Book ID Numbers

After processing is complete, the original temporary names like book1.txt and book2.txt have become numerical filenames, such as 1958436720.txt, 4829173056.txt, 6094728315.txt, 7305619482.txt, and 8640295173.txt. As you can see, the file extension remains .txt; it's the main part of the filename that has changed.

image-Batch TXT renaming,regular expression file renaming,text file content to file name,batch file processing

This naming convention is more suitable for subsequent searching and systematic management. For example, as long as you know the Book ID, you can directly locate the corresponding TXT file in the folder. If later steps involve importing to a database, uploading to a system, or comparing with a spreadsheet list, it will also be more reliable than temporary names like book1 or book2.

Operation Steps: Using Regular Expressions for Batch TXT Content Extraction and Renaming

The following steps, following the screenshots' order, fully explain the operation process from selecting the feature to setting the regular expression. Since the goal of this article is batch processing text files, the focus is on using "a certain piece of text from the file content" as the new filename.

Step 1: Enter the "File Name" category and select the text file content renaming feature

After opening HeSoft Doc Batch Tool , select File Name from the feature categories on the left. The center area will display various features related to batch filename processing, such as find-and-replace filename keywords, inserting text, adding prefixes or suffixes, etc.

The feature to use this time is item 8: Rename text files using file content. The description in the screenshot indicates that this feature is used for "batch use certain text from the content of a text file as the filename for that file." This perfectly matches the scenario in this article: extracting the Book ID from the TXT body, then renaming the file with this Book ID.

image-Batch TXT renaming,regular expression file renaming,text file content to file name,batch file processing

The purpose of selecting this feature is to enter a batch renaming process specifically designed for text files like TXT. Unlike standard filename find-and-replace, it doesn't modify the existing filename; instead, it reads the file content to generate a new name.

Step 2: Add the TXT files to be processed

After entering the feature, the top of the interface displays the current feature name as Rename text files using file content, and you enter Step 1, "Select records to process." Here, you can use the interface's Add File or Import Files from Folder options to add the TXT files needing processing to the list.

image-Batch TXT renaming,regular expression file renaming,text file content to file name,batch file processing

In the screenshot, 5 files have been added, named book1.txt, book2.txt, book3.txt, book4.txt, and book5.txt. The list also shows path, extension, creation time, modification time, etc., making it easy to confirm the right files are selected. The operation column on the right allows removing individual files, and the clear button at the top empties the entire list.

The expected result of this step is: all TXT files to participate in the batch rename are present in the list with the correct record count. Once confirmed, click Next Step at the bottom to enter the processing option settings.

Step 3: Set the search area, choosing custom formula to match text

Entering Step 2 "Set Processing Options," you first need to determine from which part of the file content the software should extract text. The "Search Area" in the screenshot provides options, including First line of text and Text matched by custom formula.

In this example, Text matched by custom formula is selected. The reason is that although the Book ID appears on the first line, what we really want is not the entire line "Book ID:4829173056," but only the numeric ID after the colon. If we directly used the first line's text, it might bring "Book ID:" into the filename, which doesn't meet the goal. Using a custom formula, i.e., a regular expression, allows for more precise extraction of just the numeric part.

image-Batch TXT renaming,regular expression file renaming,text file content to file name,batch file processing

The operational purpose of this step is to tell the software: don't simply take the entire line as the filename; instead, match the specified content according to the expression set below.

Step 4: Enter the regular expression to extract only the digits after Book ID

The regular expression in the screenshot is:

(?<=Book ID:)\d+

This expression can be understood in two parts:

  • (?<=Book ID:): Indicates the position must be immediately preceded by "Book ID:". It's a lookbehind assertion in regular expressions, used to limit the matching condition.
  • \d+: Indicates matching one or more digits.

The combined meaning is: find a string of digits in the text that is immediately preceded by "Book ID:". Taking the content Book ID:4829173056 in the screenshot as an example, what is ultimately extracted is not the whole segment of text, but 4829173056.

If the number format in your text differs, you can adjust it accordingly. For instance, if the prefix is "ID=", the regex can be changed to match the digits after ID=; if the ID includes letters and numbers, you'd need to expand the match rule from digits to an alphanumeric combination. This article doesn't cover all regex patterns, but the core idea is the same: locate using a stable prefix, then extract the part that truly needs to be the filename.

Step 5: Select the filename position to overwrite the entire filename

In the "Position" area, the screenshot shows Overwrite the entire filename is selected. This means the extracted text will serve as the new filename main body, rather than being inserted on the left or right of the original filename.

The goal of this example is to completely replace temporary names like book1, book2 with the Book ID, so overwriting the entire filename should be chosen. If you only wanted to prepend the number to the original filename, you could use the interface's option to choose "On the left of the filename"; if you wanted to keep the original filename and append the number, you could choose "On the right of the filename". However, judging from the final result in the screenshot, this processing uses overwriting the entire filename while preserving the .txt extension.

After setting this up, click Next Step at the bottom to proceed to the subsequent save location and start processing flow. The step bar at the top of the interface shows there are still "Set Save Location" and "Start Processing" afterwards; follow the software prompts to complete them.

Regular Expression Setup Explanation: Why Not Use Standard Wildcards

Many users are accustomed to using wildcards like asterisk * or question mark ? to represent uncertain content. However, when the task is "precisely extracting a segment of text from file content," regular expressions are more suitable. Wildcards are typically good for simple filename matching, like matching all .txt files; whereas regular expressions can express more precise conditions, such as "only the consecutive digits following Book ID."

The (?<=Book ID:)\d+ used in this article is a typical example of a content extraction rule. It won't include line titles, body paragraphs, or spaces as parts of the filename, fetching only the numeric ID that meets the condition. This reduces cleanup work after batch renaming and avoids unnecessary characters in filenames.

Common Questions and Considerations

1. Does each TXT file need to contain matchable content?

It is recommended to check sample files before batch processing to ensure each file contains content similar to Book ID:Number. If some files have no match result, the software might not generate the expected new filename; the exact behavior needs to be based on actual processing results. To reduce risk, it is recommended to test the rule with a small number of files first.

2. What if there are spaces before or after the Book ID?

The format in the screenshot shows no space between Book ID, the colon, and the number. If your text is written as "Book ID: 4829173056" with a space after the colon, the regular expression needs to be adjusted accordingly. Before batch processing files, confirm whether the ID format is unified.

3. Will it affect the .txt extension?

From the post-processing screenshot, the files still maintain the .txt extension; it's the main part of the filename that changes. That is, after renaming, they are still text files and can be opened normally with a text editor.

4. What if the extracted numbers are duplicated?

When batch renaming, try to use unique identifiers like order numbers, Book IDs, or device IDs as filenames. If multiple files have the same extracted content, it might cause naming conflicts. It is recommended to confirm the uniqueness of IDs before processing, or test on a small number of files first.

5. Does it only apply to TXT files?

This feature's name is clearly "Rename text files using file content," so this article uses TXT as an example. For Word documents like .docx or .doc, you can also see similar features in the software interface like "Rename Word files using file content"; for PDFs, there is "Rename PDF files using file content." The corresponding feature should be selected for different file types and not mixed up.

Summary: Leave Repetitive Renaming to Batch Processing Tools

When filenames are meaningless but the file content contains stable identifiers, batch renaming using regular expressions is a highly efficient approach. This article demonstrated how to select "Rename text files using file content" in HeSoft Doc Batch Tool , import multiple TXT files, and use (?<=Book ID:)\d+ to extract the digits following Book ID, ultimately batch-changing temporary names like book1.txt and book2.txt into identification number filenames.

For office scenarios, this type of batch file processing capability can significantly reduce repetitive work, particularly suitable for tasks like data archiving, text data organization, and standardizing naming before batch delivery. It is recommended that before officially processing a large number of files, you first select a few sample TXT files for testing to confirm the regular expression can accurately extract the target content, and then execute the task in batch.


Keyword:Batch TXT renaming , regular expression file renaming , text file content to file name , batch file processing
Creation Time:2026-07-10 06:34:33

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!