How to rename multiple text files by content number? Use regex matching to batch rename TXT


TranslationEnglishFrançaisDeutschEspañol日本語한국어Update Time2026-07-08 06:36:16

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 multiple txt file names are messy or just temporary numbers, you can use information such as IDs, titles, or numbers in the file content to rename them. This article demonstrates the complete process through HeSoft Doc Batch Tool : import text files, use custom regular expressions to match the numbers after Book ID, and then overwrite the original file names with the matched results. No need to open and copy one by one, suitable for batch organizing txt, logs, exported documents, and numbered materials.

If you frequently organize text data, you've probably encountered this situation: a folder contains a batch of txt files with seemingly meaningless names like book1.txt, book2.txt, book3.txt. The content that truly distinguishes the files is actually inside the text, such as a Book ID, article number, order number, or other unique identifier written on the first line. Manually renaming a few files is acceptable, but as the number increases, opening each one, copying the number, closing the file, and renaming it becomes highly inefficient, repetitive labor.

This article introduces a more office-scenario-suited approach: using office software designed for batch file processing like “ HeSoft Doc Batch Tool ” . You can use the "Rename text files using file content" function, combined with wildcard regular expressions, to batch-extract specified numbers from text content and automatically set them as new txt filenames. In the example, we will batch-change temporary filenames like book1.txt to the numeric ID following "Book ID" found inside the text.

Applicable Scenarios: Extracting Numbers, Titles, or IDs from Text Content as Filenames

The core prerequisite for batch renaming txt files by content is that each text file contains a stable, recognizable piece of content suitable for a filename. As long as this content follows a certain pattern, it can be extracted using regular expressions.

Typical scenarios include:

  • E-book or database files: The file contains a Book ID, ISBN, title, or chapter number, but the filename is just a system-generated temporary name.
  • Business export texts: Each txt file contains an order number, contract number, or customer ID, requiring archiving by these numbers.
  • Log file organization: The log content includes device IDs, task IDs, or date batches that need to be renamed for easier searching.
  • Batch text collection: Collected result filenames are irregular, but the body text contains a unique identifier or source number.

Unlike standard filename find-and-replace, "Rename text files using file content" reads the internal content of the file first, then generates a new filename based on rules. This function is particularly suited for reducing repetitive work and represents core value in file organization scenarios for batch office software.

Effect Preview: Changing from book1.txt to numbered.txt

Before processing, the folder contains 5 text files named book1.txt through book5.txt. It's impossible to determine the actual content or number corresponding to each file from the filename alone.

image-Rename multiple text files by content,batch rename txt files,rename files by matching content with regex,batch file processing with office software

After opening one file, you can see the first line contains Book ID:4829173056. This indicates that the file already has a unique identifier suitable for a filename. What we need to do is extract the numbers following "Book ID:", not use the entire line of text as the filename.

image-Rename multiple text files by content,batch rename txt files,rename files by matching content with regex,batch file processing with office software

After batch processing, the filenames have become the numeric ID plus the txt extension, such as 1958436720.txt, 4829173056.txt, 6094728315.txt, etc. The processed filenames are clearer and more convenient for searching, sorting, and archiving by number.

image-Rename multiple text files by content,batch rename txt files,rename files by matching content with regex,batch file processing with office software

Operation Steps: Completing TXT Batch Rename in HeSoft Doc Batch Tool

Step 1: Find the Text File Content Rename Function

Open HeSoft Doc Batch Tool and select "File Name" in the left navigation. The right side will display a list of functions related to filename processing. According to the screenshot, the page includes "Find and replace keywords in file names," "Insert text into file names," "Add prefix and suffix to file names," and more. The function needed for this article is number 8: Rename text files using file content.

image-Rename multiple text files by content,batch rename txt files,rename files by matching content with regex,batch file processing with office software

The reason for choosing this function is clear: our goal is not to replace a word in the original filename or simply add prefixes/suffixes, but to read the txt file content and use the text matched within as the filename. After entering this function, the software will guide you through file selection, processing options, save location, and starting the process.

Step 2: Import the Text Files to Be Batch Processed

After entering "Rename text files using file content," you first arrive at the "Select records to process" step. The top of the interface provides two main entry points: "Add files" and "Import files from folder." If files are scattered in different locations, you can add them individually; if all files are in the same directory, using "Import files from folder" is more efficient.

image-Rename multiple text files by content,batch rename txt files,rename files by matching content with regex,batch file processing with office software

After importing, the list displays the file name, path, extension, creation time, and modification time. The example imports 5 txt files located in the D:\test directory. At this point, a quick check is recommended: first, is the number of files correct; second, are the extensions all txt; third, is the path the target folder. This avoids mistakenly processing other files.

After confirming correctness, click "Next" at the bottom. The expected result after this step is: the list of files to be processed is ready, and the software knows which text files to read content from next.

Step 3: Choose Text Matched by Custom Formula

The second step enters "Set processing options." In the "Search area," you can see options like "First line of text" and "Text matched by custom formula." The example selects Text matched by custom formula because we do not want to use the entire line "Book ID:4829173056" as the filename, but only extract the numbers following it.

image-Rename multiple text files by content,batch rename txt files,rename files by matching content with regex,batch file processing with office software

The advantage of choosing custom matching is greater precision. For example, the first line of text might contain fixed labels, colons, spaces, explanatory text, etc., which may not all be suitable for a filename. Using a regular expression allows you to extract only the truly needed part, resulting in a clean, standardized new filename.

Step 4: Fill in the Regular Expression to Extract Numbers Following Book ID

Enter the following in the "Regular Expression" input field:

(?<=Book ID:)\d+

This expression is specifically designed to match consecutive numbers appearing after "Book ID:". Its function can be understood as: first locate the fixed marker "Book ID:", then only take the numeric content following it. Taking the text in the screenshot as an example, the original content is:

Book ID:4829173056

The match result is:

4829173056

The filename generated this way will not include the text "Book ID" or the colon. This is important for Windows filenames, as special symbols like colons cannot appear directly. Using a regular expression to filter out unwanted characters beforehand can reduce issues with batch rename failures or non-standard naming.

Step 5: Set the Position for the Matched Content in the Filename

In the "Position" area, the example selects Overwrite the entire filename. This means the newly extracted number will directly replace the main body of the original filename. The original names like book1, book2 will not be retained, but the txt extension will be kept as the text file format.

If your work rules require retaining the original name, you can also choose "On the left side of the filename" or "On the right side of the filename" based on the interface options. For example, retaining the original name and appending the number suits scenarios where tracing the original sequence is needed. However, in this article's example, names like book1 have no actual meaning, so overwriting the entire filename aligns better with the organization goal.

Step 6: Continue to Set the Save Location and Execute Processing

After completing the processing options, click "Next". The subsequent interface flow includes "Set save location" and "Start processing". Since batch renaming affects multiple files at once, it is recommended to reconfirm three things before starting: Is the file list correct? Can the regular expression match the desired content? Does the save location conform to your organization habits?

If using a regex rule for the first time, it's advisable to test with a small sample of files first. After confirming the generated new filenames are correct, execute the batch processing on the whole folder. Once finished, open the target folder to see the filenames updated according to the numbers in the text content.

Why Regular Expressions are Suitable for Batch File Renaming

The advantage of wildcards or regular expressions lies in their ability to describe "a type of text pattern" rather than only processing fixed characters. For instance, in this article's files, each Book ID number is different, but they all share common characteristics: located after "Book ID:" and composed of consecutive digits. Regular expressions are exactly the tool for handling such patterns.

Without regular expressions, the manual method requires repeatedly performing these actions: open file, locate number, copy number, close file, rename, paste number, confirm extension. Handling 5 files might not be much, but for 500 files, the workload multiplies rapidly. Using batch office software, you only need to set the rules; the reading, matching, and renaming are handled by the software.

Frequently Asked Questions and Precautions

1. Must the Book ID's position be identical in every txt file?

It does not necessarily have to be in the exact same position, but the content format is best kept consistent. In this article's example, the Book ID appears on the first line, making the rule easier to match stably. If some files are written differently, for instance, "BookID:", "Book ID:", or "Book ID: 4829173056", you'll need to adjust the regular expression based on the actual format.

2. Why is it recommended to extract only the numbers, not the entire line?

Because the entire line may contain spaces, colons, or other characters unsuitable for filenames. Extracting only the numeric ID makes the filename cleaner and avoids system-restricted special symbols.

3. What if the matched numbers are duplicated?

If multiple files contain the same number in their content, filename conflicts may occur. It's best to confirm the uniqueness of Book IDs or business numbers before processing. For important files, consider backing them up first or validating results with a small batch of files.

4. Can this method only process txt files?

This article demonstrates the "Rename text files using file content" function, which primarily targets txt text files. For Word documents, common extensions include docx and doc; for PDF files, a corresponding file content rename function specific to that type is usually needed. In practice, choose the corresponding function entry in the software based on the file type.

5. What is the most commonly overlooked thing before batch processing?

The most commonly overlooked thing is rule testing. A regular expression can fail to match content if it's missing even a single space or symbol. It is recommended to start by selecting a small number of representative files for validation, and only process all files after confirming the results are correct.

Summary: Delegate Repetitive Renaming to Batch Office Software

Renaming multiple text files based on an internal content ID is essentially a typical batch office automation problem. The "Rename text files using file content" function provided by HeSoft Doc Batch Tool can centralize the steps of reading file content, regex matching, and filename generation into a single process. This article's example used (?<=Book ID:)\d+ to extract the number following Book ID, chose to overwrite the entire filename, and ultimately batch-changed temporary names into standardized numbered filenames.

If your folders also contain a large number of txt, log text, exported data, or numbered documents needing organization, you can refer to this article's workflow: first confirm the content pattern, import the files, set the regular expression, and finally batch process them. This significantly reduces the time spent on manual copy-paste and individual renaming, allowing you to dedicate your energy to more valuable content review, archiving, and analysis tasks.


KeywordRename multiple text files by content , batch rename txt files , rename files by matching content with regex , batch file processing with office software
Creation Time2026-07-08 06:36:00

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!