Bulk renaming of txt filenames to the numbers in the text: using wildcard regex to extract content and rename


TranslationEnglishFrançaisDeutschEspañol日本語한국어Update Time2026-07-07 06:38:24

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!

The original names of many txt files are just sequential numbers like book1 and book2, but the actual Book ID that can be used for archiving is within the file content. This article uses HeSoft Doc Batch Tool as an example to explain how to import multiple txt files using the "Rename Text Files Using File Content" feature, extract the numbers after the Book ID with regular expressions, and batch replace the original file names, ensuring the file names match the content IDs. It's suitable for data organization, ebook archiving, and log file management.

In daily office work and data management, file names often determine the efficiency of later searches. If a folder is full of names like book1.txt, book2.txt, book3.txt, they may seem sequential in the short term, but during long-term maintenance, it's difficult to determine what content each file corresponds to. Especially when there are many text files, manually opening each to check the content, then copying the identifier back to the file explorer to rename it, becomes a very typical repetitive task.

A more reasonable approach is to have office software read the content of a txt file, automatically extract key fields from the body text according to fixed rules, and then write them into file names in batches. The scenario demonstrated in this article is to extract the Book ID number from the first line of the text body and use it to replace the original sequential book file name. Through wildcard/regex matching, you can accurately locate the number after "Book ID:", achieving batch renaming of text content into file names.

The tool used in this article is " HeSoft Doc Batch Tool " displayed in the screenshot. It is a batch document processing software for office scenarios, suitable for handling repetitive tasks like batch file renaming, folder organization, and tools for Word, Excel, PDF, and text files. For users who need to process a large number of txt, doc, docx, pdf, and other files at once, choosing the right batch processing function can significantly reduce manual operation time.

Applicable Scenario: The Body Contains an Identifier, but the File Name is Not Standardized

The core characteristic of this type of need is: the file name is unimportant or inaccurate, but there is a stable, recognizable field within the file content that can serve as the name. As long as the position and format of the field follow a pattern, it can be automatically extracted using rules.

The following scenarios are all very common:

  • E-books or article materials, after batch export, have file names like book1, book2, but the body text contains a Book ID.
  • Txt files exported from an internal database, with the body containing a material or archive number.
  • Text data from crawling or collection, with temporary sequential file names, but the content has a unique identifier.
  • Log, report, and record files contain task numbers, and you wish to rename them by these numbers.
  • When delivering files to clients or colleagues, you want the file name to directly reflect the content identifier for easier verification.

If your files are in Word format, such as doc, docx, or PDF format, be careful to select the corresponding function type. The screenshot in this tutorial shows the "Rename text files using file content" option, which is mainly for txt text files. Using the corresponding tool for different formats helps ensure stable content reading and renaming results.

Result Preview: From Temporary File Names to Searchable Identifier File Names

Before Processing: File names are only sequential, with no content meaning

The folder before processing contains 5 txt files, named book1.txt through book5.txt. From the file names alone, it is impossible to determine the Book ID corresponding to each file or quickly locate a file by its identifier. To find the material for a specific ID, the only way is to open and check the files one by one.

image-Batch rename txt files,rename files with body numbers,wildcard regular expressions,batch process text files,generate file names from file content

Opening one of the txt files, you can see Book ID:4829173056 on the first line of the body. This number is the key information actually suitable for a file name. Although there is an English title and body paragraphs afterwards, they are not suitable for direct use as a file name because the text is long and might contain characters inconvenient for naming.

image-Batch rename txt files,rename files with body numbers,wildcard regular expressions,batch process text files,generate file names from file content

After Processing: File names are uniformly changed to the Book ID from the body

After processing is complete, the names in the folder have changed from book sequences to numeric identifiers, such as 1958436720.txt, 4829173056.txt, 6094728315.txt, etc. Each file's name corresponds to the Book ID in its body, so future retrieval only requires searching the numeric identifier.

image-Batch rename txt files,rename files with body numbers,wildcard regular expressions,batch process text files,generate file names from file content

This result is suitable for data archiving and also facilitates subsequent comparison with databases, table lists, and delivery directories. Once file names become standardized, many tasks that originally relied on manual judgment can be further automated.

Steps: Complete Batch Renaming in HeSoft Doc Batch Tool

Step 1: Find the text content renaming function in the File Name category

After launching HeSoft Doc Batch Tool , there are different tool categories on the left. According to the screenshot, for this task you need to enter the "File Name" category, and then select "8. Rename text files using file content". The description for this entry is to batch use certain text from the content of text files as the file name for that file, which perfectly matches the requirements of this article.

image-Batch rename txt files,rename files with body numbers,wildcard regular expressions,batch process text files,generate file names from file content

The purpose of this step is to enter the batch renaming workflow specifically for txt files. Unlike a standard "Find and replace keywords in file names" operation, this does not replace parts of the old file name but instead reads the internal content of the file and generates a new name based on the matching result. Therefore, this function is more suitable when the original file name has no reference value at all.

Step 2: Import the txt files to be batch processed

After entering the function interface, the first step is to "Select records to process". On the upper right of the interface, you can see buttons like "Add Files," "Import Files from Folder," "Clear," and "More." If the number of files is small, you can use "Add Files"; if all the txt files to be processed are already placed in one folder, using "Import Files from Folder" is more convenient.

image-Batch rename txt files,rename files with body numbers,wildcard regular expressions,batch process text files,generate file names from file content

In the example, 5 files have been imported, and their names, paths, extensions, creation dates, and modification dates are displayed in a table. This list is very important, as it serves as a confirmation checklist before formal processing. It is recommended to check the following content here:

  • Whether the total count of records meets expectations (the summary record count in the example is 5).
  • Whether the file extensions are all .txt, to avoid mixing in irrelevant files.
  • Whether the paths are correct, to avoid processing files in the wrong folder.
  • Whether any files do not need processing; they can be removed via the operation column if present.

After confirming correctness, click "Next" at the bottom. The expected result is to enter the processing rule settings page.

Step 3: Set the search area to text matched by a custom formula

In the second step, "Set processing options," you need to choose which part of the text content to use as the file name. In the screenshot, the "Find what" area includes "First line text" and "Text matched by custom formula". This example selects "Text matched by custom formula" because we only want the numeric identifier and do not want the "Book ID:" prefix to be included in the file name.

image-Batch rename txt files,rename files with body numbers,wildcard regular expressions,batch process text files,generate file names from file content

After selecting custom matching, enter the following in the "Regular Expression" input box:

(?<=Book ID:)\d+

The function of this rule is to find the string of numbers immediately following "Book ID:" within the text. It will not include "Book ID:" itself in the result, only returning the identifier after it. For example, for the text "Book ID:4829173056," the match result is 4829173056.

If you are not familiar with regular expressions, you can think of them as a more precise wildcard rule. While ordinary wildcards generally mean "some characters," regular expressions can further specify conditions like "must be a digit," "must be after a certain word," "match one or more characters," etc. For batch renaming, it leads to more stable extraction results.

Step 4: Overwrite the entire file name with the match result

In the "Position" area on the same page, the screenshot shows "Overwrite the entire filename" is selected. This means the extracted identifier will replace the main body of the original file name. Taking book2.txt as an example, if 4829173056 is matched in the body text, the processed file name will be 4829173056.txt.

You can also see position options like "To the left of the filename" and "To the right of the filename". The scenario in this article aims to completely remove temporary names like book1, book2, so overwriting the entire file name is more suitable. The resulting file names are concise, uniform, and more conducive to later sorting and searching by identifier.

Step 5: Set the save location and execute according to the process flow

After completing the regular expression and position settings, continue by clicking "Next". As seen in the process bar, the subsequent steps are "Set save location" and "Start processing". The save location determines where the processed files will be output. To reduce the risk of operational errors, it is recommended to back up the original files before formal batch processing, or to first test the rule with a small number of sample files.

After confirming the save location and processing settings, proceed to the "Start processing" stage to execute the batch rename. After processing is complete, return to the target folder to check the results. If the file names have been changed to Book ID numbers and the extension remains .txt, it indicates that this batch rename based on text content was successful.

The Logic of Regex and Wildcards: Why the Book ID Can Be Accurately Extracted

Many users, when mentioning "wildcard renaming," are expressing a desire to match text based on certain patterns. Regular expressions can be seen as more powerful matching rules. In this example's (?<=Book ID:)\d+, \d+ is responsible for matching digits, and (?<=Book ID:) is responsible for limiting these digits to appearing only after "Book ID:". This way, even if there are other numbers in the body text, erroneous extraction is largely avoided.

For instance, an article's body text might contain dates, page numbers, paragraph sequence numbers, etc. If only \d+ was written, it might match the first number to appear, which might not be the Book ID. Adding "Book ID:" as a positioning condition makes the rule much clearer.

If your text format is slightly different, the rule needs to be adjusted accordingly. For example:

  • If the field is BookID:4829173056, the space in the expression needs to be removed.
  • If the field is Book ID: 4829173056, the space after the colon needs to be considered.
  • If the field is not purely numeric but a mix of letters and numbers, you cannot just use \d+ for matching.

It is recommended to observe several sample files first and confirm whether the target field is consistent across different files. When batch processing files, the clearer the rule, the more reliable the result.

Common Questions and Precautions

1. Is it necessary to fill in the regular expression?

If you select "Text matched by custom formula," you need to provide a regex that can match the target content. If the target content happens to be the exact text of the first line, you could also consider selecting "First line text," but in this example, to remove the "Book ID:" prefix, using a regex is more appropriate.

2. What if there are multiple Book IDs in the file content?

If multiple Book IDs in the same format appear in a single text file, the matching result might depend on the software's processing logic. To avoid uncertainty, it is recommended to ensure the field used for naming in each file is unique, or to write the regex more precisely, for example, by restricting it to appear near the first line. However, this article only demonstrates general operations based on screenshots and does not extend to additional settings not reflected in the interface.

3. Why back up before batch renaming?

The efficiency of batch operations is high, but if a rule is written incorrectly, it can also quickly generate names that do not meet expectations. Backing up original files or testing with a few files first is a very important habit when processing large amounts of data. Especially for formal office materials, client deliverables, or archived documents, it is highly recommended to keep the original version.

4. Can the file name contain any body text?

It is not recommended to directly use very long texts or sentences with special symbols as file names. File names should be as brief, explicit, and stable as possible. Identifiers, IDs, and short titles are more suitable for file names. The example using a purely numeric Book ID is a relatively safe naming field.

5. Can this method process Word and PDF files?

This article demonstrates this for txt text files. For Word documents like .doc or .docx, or for PDF files, you should select the corresponding content renaming function within the software. This allows the software to read the document content according to its respective format, rather than treating all formats as plain text.

Summary: Use Batch Processing Tools to Automatically Turn Text Identifiers into File Names

When the file names of a large number of txt files have no practical meaning, but the body contains a stable identifier, using batch renaming based on file content is a more efficient method. With HeSoft Doc Batch Tool , you can first import multiple txt files, then use the regular expression (?<=Book ID:)\d+ to extract the number after "Book ID:", and finally overwrite the original file name with the matched result, quickly obtaining a list of identifier-based, standardized txt files.

The value of this method lies not only in "renaming faster" but more so in reducing the repetitive labor of manually opening files, copying content, switching windows, and pasting names. For office tasks such as data organization, e-book archiving, log management, and batch text delivery, it is recommended to prepare sample files first, verify the matching rule, and then execute the batch process. This ensures accuracy while fully leveraging the efficiency advantages of office software for batch file processing.


KeywordBatch rename txt files , rename files with body numbers , wildcard regular expressions , batch process text files , generate file names from file content
Creation Time2026-07-07 06:38:08

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!