Batch renaming files using text content as file names: Extracting numbers from the first line of a txt file using regex


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

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!

Many TXT file names are just temporary serial numbers, but the first line of the content contains the actual number that needs to be archived. This article introduces how to batch import text files in HeSoft Doc Batch Tool , use regular expressions to extract the number after the Book ID field, and overwrite the extracted result as the new file name, suitable for office scenarios requiring efficient organization, such as e-book materials, collected texts, and log records.

In daily office work and data management, text files are often generated in batches. For example, crawled or exported content is saved as book1.txt, book2.txt, book3.txt, where the filenames merely reflect the generation order and cannot indicate which data each file actually corresponds to. However, upon opening a file, you will find that the first line already contains a unique identifier, such as Book ID:4829173056. Using this identifier as the filename makes subsequent searching, sorting, and archiving much more convenient.

The problem is that when there are many files, manually opening each txt file, copying the identifier, returning to the folder, and renaming consumes a significant amount of time. Even more troublesome is that spaces might be accidentally selected during copying, or the wrong file might be renamed during pasting; the higher the number of files, the greater the probability of errors. This article uses HeSoft Doc Batch Tool as an example to demonstrate how to batch extract numeric identifiers from text content and automatically generate new txt filenames using the "Rename Text Files Using File Contents" feature combined with regular expressions. The value of such office software lies not in processing individual files, but in batching repetitive steps to make file organization more stable and time-saving.

Applicable Scenarios: Extracting IDs, Titles, or Codes from txt Content for Filenames

As long as uniformly formatted information exists within text files, the method in this article is very suitable. For example, the first line of each file contains a Book ID, article number, customer code, order number, document title, or date; or, despite having chaotic names, the files contain fields that can serve as unique identifiers; or you need to rename a batch of exported txt files to a system-recognizable naming format.

This tutorial focuses on "extracting the numbers following Book ID." If you are dealing with other fields, the approach is similar: first, confirm the location and format of the target content within the file, then write a regular expression that matches that content, and finally choose to overwrite the entire filename with the matching result. For doc, docx, PDF, and other files, you can also select the corresponding rename by content function in the software; this example specifically targets txt text files.

Effect Preview: Original txt Filenames Lack Business Meaning

Before processing, there were 5 txt files in the folder named book1.txt, book2.txt, book3.txt, book4.txt, and book5.txt respectively. This naming convention is common for batch-generated or temporarily exported files, but you cannot tell the Book ID corresponding to each file from the file list.

image-Use text content as file names,batch rename .txt files by content,and bulk rename files using regular expressions

Opening one of the text files reveals that the first line is Book ID:4829173056. This indicates that a more suitable identifier for a filename already exists within the file content. The red arrow points to the numeric part we want to extract. Since all files follow a similar structure, the same regular expression rule can be used for batch processing.

image-Use text content as file names,batch rename .txt files by content,and bulk rename files using regular expressions

Effect Preview: Filenames Auto-Generated Based on Book ID After Processing

After batch processing is completed, the filenames in the folder are no longer temporary names like book1, book2, but have become 1958436720.txt, 4829173056.txt, 6094728315.txt, 7305619482.txt, 8640295173.txt. That is, the software has extracted the matched identifiers from each text file and written them into the corresponding filenames.

image-Use text content as file names,batch rename .txt files by content,and bulk rename files using regular expressions

This result is excellent for archiving: you can know the identifier just by looking at the filename, without repeatedly opening files to confirm their content. For those who need to find files based on identifiers, folder searching becomes more direct; for materials to be uploaded, imported, or transferred later, standardized naming also reduces communication costs.

Step 1: Select the Rename by Text Content Feature in the Filename Category

After opening HeSoft Doc Batch Tool , go to "File Name" in the left feature category. The interface displays various filename processing capabilities, including find and replace, insert text, add prefix/suffix, add parent folder name, etc. This time, you need to click "Rename Text Files Using File Contents". The feature description shows that it is used to bulk use certain text from the content of text files as those files' new filenames.

image-Use text content as file names,batch rename .txt files by content,and bulk rename files using regular expressions

Note that you should not select "Rename Word Files Using File Contents" or "Rename PDF Files Using File Contents", because this example deals with txt files. Different file formats are read differently; choosing the feature corresponding to the file type can reduce issues like recognition failure or inapplicable rules.

Step 2: Import the txt Files to be Batch Processed

After entering the feature, the software displays the first step: "Select records to process." If the files are scattered, you can click "Add File" to select them individually; if they are all in the same folder, clicking "Import Files from Folder" is more convenient. In the example, 5 txt files from the D:\test directory were imported, and the list shows Name, Path, Extension, Creation Time, and Modification Time.

image-Use text content as file names,batch rename .txt files by content,and bulk rename files using regular expressions

The purpose of this step is to build a list of files to process. Do not rush to the next step after importing; it's recommended to first check if the extensions are all txt, if the paths are correct, and if the quantity matches expectations. The bottom of the interface shows a record count of 5, indicating that 5 files will be processed. If incorrect files were selected, you can use the operation column to delete them; if you need to reselect, you can clear the list and import again. After confirming everything is correct, click "Next."

Step 3: Set the Search Area and Regular Expression

On the "Set processing options" page, you need to tell the software where in the file content to look and by what rules to search. In the example, "Text matched by custom formula" is selected, and the regular expression (?<=Book ID:)\d+ is entered in the "Regular Expression" input box.

image-Use text content as file names,batch rename .txt files by content,and bulk rename files using regular expressions

This regular expression means: only extract the consecutive digits immediately following "Book ID:". It will not include "Book ID:" in the filename, nor will it match ordinary text in the body. For the content Book ID:4829173056 in the screenshot, the final match result is 4829173056. If your file has a space after Book ID, such as Book ID: 4829173056, you need to adjust the expression according to the actual format, for example, by accounting for the space.

Then, in the "Location" area, select "Overwrite entire filename". This option replaces the original filename main body with the matched text. The .txt extension is retained after processing, so the final result is "numeric_id.txt". If you wish to keep the original book1, book2 and append the identifier to the left or right, you can choose other locations based on the "On the left of the filename" or "On the right of the filename" options in the interface. In this example, to make the filename completely based on the identifier, the overwrite method is used.

Step 4: Set the Save Location and Execute Processing

After setting the rules, click the "Next" button at the bottom of the page. Following the wizard process, you will later proceed to "Set save location" and "Start processing". Since batch renaming affects multiple files, it is recommended to confirm two things before formal execution: first, whether the regular expression can match unique content in each file; second, whether the matched identifier is suitable as a filename. The safest practice is to test with a few files first, and after seeing that the output is correct, process all files.

After starting the process, the software will read the content of each text file one by one, obtain the target number based on the regular expression, and write the number into the filename. The entire process requires no manual file opening, copying, or pasting by the user. For dozens or even more txt files, this step can save significant manual operation time.

FAQ and Notes

What does \d+ mean in the regex? It represents a sequence of consecutive digits. Since the Book ID is purely numeric, using it can accurately extract the identifier.

Why use (?<=Book ID:)? This is to restrict that the digits must be preceded by "Book ID:", preventing other numbers in the main body from being mistakenly extracted as the filename.

What if the field name is not Book ID? Just replace the fixed text in the expression with your actual field. For example, if the field is "Order No:", you would need to rewrite the matching rule around that field.

Is backup necessary before processing? Backup is recommended. Although batch processing is efficient, once a rule is written incorrectly, it may lead to unexpected filenames. Backup allows you to test and adjust with more confidence.

Can the filename contain all text content? Not recommended. Filenames should be concise and stable; it's best to use identifiers, title phrases, or other key content. Overly long text, line breaks, or special symbols are unsuitable as filenames.

Summary: Using Office Software for Batch File Processing is More Reliable than Manual Renaming

Using text content as a filename essentially means offloading the three repetitive actions of "reading content, extracting fields, and renaming files" to the software. Through HeSoft Doc Batch Tool , you can select "Rename Text Files Using File Contents" in the File Name category, import the txt files, use (?<=Book ID:)\d+ to extract the Book ID number, and then choose to overwrite the entire filename to complete the batch rename. For scenarios like organizing e-book numbers, archiving collected texts, or classifying log files, this method can significantly reduce repetitive work. It is recommended that you first prepare a test folder, verify the expression and processing results, and then apply it to formal document organization.


Keyword:Use text content as file names , batch rename .txt files by content , and bulk rename files using regular expressions
Creation Time:2026-07-08 06:33:50

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!