Batch Rename TXT Files Using Wildcards and Regex: Generate New Filenames Based on File Content


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

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!

If txt file names have no patterns, but the file contents contain stable identifiers, you can use regular expressions to batch rename them. This article, using the interface of HeSoft Doc Batch Tool , explains how to select the text file content renaming function, import multiple txt files, fill in an expression to match the Book ID digits, and replace the matched results as the new file names, helping users quickly complete batch file organization.

Many people search for "batch rename files using wildcards" or "batch rename files using regular expressions," but what they really want to solve is often not a simple character replacement in filenames, but rather extracting a specific piece of information from the file's content to use as the filename. For example, a batch of txt files might be named book1.txt, book2.txt, which seem to have no business meaning; but upon opening a file, the first line reads Book ID, Order Number, or Document ID. In this ideal scenario, the software should automatically read each file's content, extract the target ID, and then generate new filenames in batch.

This article will use HeSoft Doc Batch Tool as an example to demonstrate how to use the "Rename Text Files Using File Content" feature. It shows how to use the regular expression (?<=Book ID:)\d+ to extract the number following "Book ID" and batch rename txt files to their corresponding IDs. HeSoft Doc Batch Tool is an office software focused on scenarios involving batch file processing, reducing repetitive tasks, and improving office efficiency. Compared to manually opening and renaming files one by one, rule-based batch processing is much more suitable for organizing large numbers of files.

Applicable Scenarios: When to Generate Filenames from File Content

Renaming by content applies to three common situations. First, filenames are temporarily generated by the system and lack readability, such as book1, data001, export_1. Second, the file content contains clear fields like Book ID, Contract Number, Customer Name, Product Code, or Article Title. Third, there is a need to search, archive, import into a system, or distribute files to colleagues by ID at a later stage.

The examples in this article use txt text files. If your files are Word documents, you can further differentiate between doc and docx; if they are PDFs, you should select the corresponding PDF content renaming function in the software. The reading logic differs for each format, but the idea is the same: import files in batch, set the matching rule, choose the filename generation method, and execute the batch process.

Effect Preview: Before Processing, Filenames are Just "book" Prefixes

A screenshot of the folder before processing shows there are currently 5 text files, named book1.txt, book2.txt, book3.txt, book4.txt, and book5.txt. Such names are common during testing phases but are not suitable for formal archiving. Looking at the filename alone, you cannot determine the specific book ID each file corresponds to, nor can you easily search by ID.

image-Wildcard batch rename,regular expression rename txt,file content rename text file

Upon opening one of the files, you can see that the first line contains Book ID:4829173056. A red arrow marks the position of the ID to be extracted. Although the main text contains many English paragraphs, this content is not suitable for a filename; we only need the stable, short, and unique Book ID number.

image-Wildcard batch rename,regular expression rename txt,file content rename text file

Effect Preview: After Processing, Filenames Become Extracted Numeric IDs

After processing is complete, the filenames have become a set of numeric IDs while retaining the .txt extension, for example, 1958436720.txt, 4829173056.txt, 6094728315.txt, 7305619482.txt, 8640295173.txt. The new filenames correspond to the Book ID in the file contents, and the file list itself now reflects the key information.

image-Wildcard batch rename,regular expression rename txt,file content rename text file

Such results are very valuable for document organization. You can search for the ID directly in File Explorer, sort by filename, and present these files to other systems for recognition. Most importantly, the entire process is completed in batch by the software, avoiding the inefficiency and errors caused by manually copying IDs.

Step 1: Find the Target Function in the File Name Tool

After launching HeSoft Doc Batch Tool , enter the "File Name" module on the left side. The main area displays several file name processing functions, where the 8th item is "Rename Text Files Using File Content." As indicated by the description in the screenshot, this function is used to batch-extract certain text from the content of text files to be used as the filename, which is exactly what this tutorial requires.

image-Wildcard batch rename,regular expression rename txt,file content rename text file

There is a selection principle here: if you are processing txt files, choose the text file function; if processing Word files, such as doc or docx, choose the related Word file function; if processing PDFs, choose the related PDF file function. Selecting the correct file type allows the software to read the content in the corresponding format.

Step 2: Add Files or Import from a Folder

After entering "Rename Text Files Using File Content," the first step is to select the records to be processed. The top right of the page has "Add Files" and "Import Files from Folder." When the number of files is small or they are scattered in different locations, you can use "Add Files"; when files are concentrated in a single directory, using "Import Files from Folder" is more efficient.

image-Wildcard batch rename,regular expression rename txt,file content rename text file

The example shows 5 imported records, with the table listing the file name, path, extension, creation time, modification time, and other information. This list represents the scope of this batch processing task. It is recommended to carefully confirm the number and paths of files before proceeding, especially when processing formal materials, to avoid adding irrelevant files to the list. If a record does not need processing, it can be removed through the action column; once you confirm the list is correct, click "Next" at the bottom.

Step 3: Select Custom Matching and Fill in the Regular Expression

The second step is to set the processing options. The screenshot shows that "Search Area" includes "First Line Text" and "Text Matched by Custom Formula." In this example, although the target content is on the first line, we do not want the entire line as the filename—only the number after "Book ID". Therefore, select "Text Matched by Custom Formula."

image-Wildcard batch rename,regular expression rename txt,file content rename text file

Enter (?<=Book ID:)\d+ in the "Regular Expression" field. This rule is ideal for this example: "Book ID:" is the fixed prefix, and \d+ captures the consecutive digits. Using this method separates the "anchor text" from the "extraction result," so the filename retains only the numbers without the "Book ID" text.

Next, select "Overwrite the entire filename" in the "Position" area. This step determines how the matched content is written into the filename. After choosing "overwrite," the original names like book1, book2 will be replaced by the numeric ID. As seen in the post-processing screenshots, the file still retains its .txt extension, so the new filename becomes "ID.txt". If your naming convention requires keeping the original filename, you could consider appending to the left or right, but the goal of this article is a complete rename based on the content ID.

Step 4: Follow the Wizard to Set the Save Location and Start Processing

Once the rule is set, click "Next." The progress flow at the top of the page shows that "Set Save Location" and "Start Processing" are the subsequent steps. Before performing the batch rename, it is advisable to confirm whether the expression can match all files. The simplest check is to open a few sample files and confirm they all have the same field structure—for instance, all contain "Book ID:" followed by the number intended for the filename.

Once processing begins, the software will read the text content of each file in the record list, use the regular expression rule to obtain the match result, and then generate a new filename based on the position setting. Compared to manual operation, this method only requires configuring the rule once to process multiple files. For office users who frequently perform data cleaning, file archiving, and document delivery, the efficiency advantage of batch processing is significant.

Frequently Asked Questions and Notes

1. What is the difference between wildcards and regular expressions? Wildcards are typically used for simple matching, such as an asterisk representing any character; regular expressions are more precise and can express rules like "consecutive digits following a specific phrase." The (?<=Book ID:)\d+ used in this article is a regular expression.

2. What if there is a space after "Book ID"? The rule needs to be adjusted based on the actual content. For example, if the text is "Book ID: 4829173056" with a space after the colon, the original expression might not match the number directly, and the space needs to be accounted for.

3. What if multiple "Book ID" instances appear in a single file? It is recommended to ensure the target field is unique, or to restrict the match location using a more precise search area and expression. Otherwise, the match result may not be as expected.

4. Should I back up files before batch renaming? A backup is recommended, especially when using a new rule for the first time. First, copy a test folder, confirm the processed filenames are correct, and then execute on the official files.

5. Can filenames contain special symbols? When naming, it's best to use safe characters like numbers, letters, Chinese characters, or underscores. Special characters from text content, such as line breaks, slashes, and colons, are not suitable for direct use as filenames. This example extracts pure numeric IDs, which is relatively safe.

Summary: Batch Rename by Content for More Efficient TXT File Organization

If the true identifiers for a large number of txt files are hidden within the content, manual renaming is extremely inefficient. Using HeSoft Doc Batch Tool , you can automate the steps of reading content, extracting with regex, and generating filenames through the "Rename Text Files Using File Content" function. In the example provided, the digital sequence after "Book ID" was successfully extracted using (?<=Book ID:)\d+, and files like book1.txt were batch-renamed into ID format. It is recommended to first verify the rule with a small number of files in practice before batch-processing an entire directory, which improves efficiency while ensuring accurate file naming.


KeywordWildcard batch rename , regular expression rename txt , file content rename text file
Creation Time2026-07-08 06:34:06

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!