Batch Renaming Method for TXT Files Based on Book ID in the Body Text


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

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 a large number of TXT files still use temporary file names like book1, book2, subsequent searching and archiving will be very inefficient. This article uses HeSoft Doc Batch Tool as an example to demonstrate how to use the "Rename Text Files Using File Content" feature to match the number after the Book ID from the body of each text file and batch overwrite the original file names. With the regular expression (?

Many office documents, when exported, downloaded, or batch-generated, often have filenames that are just temporary serial numbers, such as book1.txt, book2.txt, book3.txt. This naming convention is not a big issue when the number of files is small, but as the number of TXT files increases, finding a specific record becomes very troublesome: you have to open each file one by one to check the number or title inside to know its corresponding content.

A more ideal approach is to directly use the unique number in the file's body text to name the file. For example, if the first line of text reads Book ID:4829173056, then the filename should be 4829173056.txt. This way, the folder list becomes a searchable, sortable, and cross-referencable list of numbers. This article will introduce how to use the office software " HeSoft Doc Batch Tool " to batch extract TXT content using wildcard regular expressions and automatically rename multiple text files.

Applicable Scenario: Needing to Extract Numbers from Text Content as Filenames

This method is particularly suitable for handling materials where "filenames are meaningless, but the file content has standard numbers." Examples include book texts containing a Book ID, business export files containing an order number, system logs containing a device number, article materials containing a manuscript ID, and customer data containing a customer number. As long as these fields are written in a relatively fixed format in each TXT file, they can be batch extracted using regular expressions.

Unlike ordinary find-and-replace operations for filenames, the method in this article does not replace a character in the original filename like book1.txt. Instead, it reads the text content inside the TXT file and uses the matched content as the new filename. If done manually, this operation requires repeatedly opening files, copying content, returning to the folder, and pasting to rename. After using a batch processing tool, you can import multiple files at once and process them uniformly according to rules.

HeSoft Doc Batch Tool is positioned as batch processing software for office documents, suitable for handling common office file tasks involving file names, folder names, Word, Excel, PowerPoint, PDF, and text files. This article uses the text file renaming function. If your material is in formats like doc, docx, or pdf, you need to select the corresponding file content renaming function for that type; for the txt files in this example, you should enter "Rename text files using file content."

Effect Preview: From book Serial Numbers to Body Text Numbers

Before Processing: Multiple TXT Filenames Fail to Reflect Content

In the file list before processing, you can see 5 text files still using the naming convention from book1.txt to book5.txt. Such filenames can only indicate the import or generation order but cannot convey the Book ID corresponding to the file.

image-Auto-rename TXT files,rename by Book ID,batch rename text content

Opening one of the TXT files reveals Book ID:4829173056 at the top of the body text. This number is the truly valuable information for subsequent management. If done manually, you would need to copy 4829173056 and then rename book2.txt to 4829173056.txt.

image-Auto-rename TXT files,rename by Book ID,batch rename text content

After Processing: Each Filename Uses the Extracted Digital ID

After batch renaming is complete, the filenames have become a set of digital numbers, such as 1958436720.txt, 4829173056.txt, 6094728315.txt, 7305619482.txt, 8640295173.txt. In other words, each TXT file has been renamed according to the Book ID in its own body text.

image-Auto-rename TXT files,rename by Book ID,batch rename text content

This processing result is very suitable for subsequent archiving. You can directly search for a number by filename, sort them numerically, or cross-reference these files with IDs in a database or spreadsheet list, eliminating the need to repeatedly open texts to verify the content.

Operation Steps: Completing Settings in HeSoft Doc Batch Tool

Step 1: Select Text Content Renaming in the File Name Tool

After launching HeSoft Doc Batch Tool , select "File Name" from the left-side menu. In the right-side function cards, you can see multiple file name processing tools. This article will click on the 8th function: Rename text files using file content. This function card is highlighted in the screenshot.

image-Auto-rename TXT files,rename by Book ID,batch rename text content

After selecting this entry, the software enters a processing flow specifically designed for TXT text files. The core logic of this function is: read the text file content, extract text according to the user-set search area or regular expression, and then use the extracted result for the filename. For scenarios requiring batch extraction of numbers from the body text, this is more stable and time-saving than manual renaming.

Step 2: Import the TXT Files to be Processed

After entering the function page, the interface is at the "Select records to process" step. The top provides two common entry points: "Add Files" and "Import files from folder". In the example, five txt files from the D:\test directory have been added to the list, with filenames from book1.txt to book5.txt.

image-Auto-rename TXT files,rename by Book ID,batch rename text content

It is recommended to carefully check the import results at this step. The list will display information such as name, path, extension, creation time, and modification time. Confirming correctness before proceeding prevents accidentally processing the wrong directory or unrelated files. For batch file operations, verification before and after import is very important because the software will execute subsequent rules according to the records in the list.

If the files are concentrated in one folder, you can use "Import files from folder" to reduce the time for individual selection; if processing only a few specific files, you can use "Add Files". After importing, click "Next" at the bottom to enter the processing rule settings.

Step 3: Select "Text matched by custom formula"

In the second step, "Set processing options," you first need to determine the search area. In the screenshot, "Text matched by custom formula" is selected instead of "First line of text." This is because our goal is not to use the entire first line as the filename but to extract the number following the Book ID from the first line.

image-Auto-rename TXT files,rename by Book ID,batch rename text content

If "First line of text" were chosen, the result might be a complete string like Book ID:4829173056; whereas in many naming conventions, only 4829173056 is needed. By selecting custom matching, you can enter rules in the regular expression area below to precisely control the content to extract.

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

Fill in the following rule in the "Regular expression" input box:

(?<=Book ID:)\d+

The function of this rule is to match the consecutive digits that appear after Book ID:. It will not include the text Book ID: in the result, returning only the subsequent numeric code. For example, if the text contains Book ID:4829173056, the match result is 4829173056.

The \d+ here represents one or more digits, suitable for handling IDs of variable length. As long as the Book ID in each file is in the same format, i.e., "Book ID:" followed directly by digits, you can use this rule for batch processing. If there is a space between Book ID and the digits in some files, or if the field name is different, the expression needs to be adjusted according to the actual content.

Step 5: Choose to Cover the Entire Filename

In the "Position" settings, the screenshot shows "Cover the entire filename" is selected. This means the software will replace the original main part of the filename, like book1, book2, with the matched number. The extension txt will be retained as the text file extension, resulting in filenames like 4829173056.txt.

This setting is suitable for completely naming files using the body text number. If you wish to keep the original name, you can also choose to place the extracted content on the left or right side of the filename based on the position options provided in the interface. However, in this tutorial, to make the filenames cleaner and consistent with the post-processing screenshots, you should select to cover the entire filename.

Step 6: Set the Save Location and Execute Batch Processing

After completing the processing option settings, click "Next". The process prompt at the top of the page indicates that "Set save location" and "Start processing" are the subsequent steps. As this article focuses on content matching and renaming rules, the save location can be set according to actual archiving needs. It is recommended to keep a backup of the original files or test the rules with a small sample of files first when processing important data.

After confirming the save location and processing settings are correct, proceed to start processing. The software will read the TXT files in the import list one by one, search for text matching the regular expression, and use the matching result to rename the file. After processing is complete, open the target folder, and you will see the filenames have changed to Book ID numbers.

Why Using Regular Expressions is More Reliable Than Manual Copying

Common errors when manually copying numbers include copying extra field names, missing a digit, copying a line break, renaming the wrong file, or missing a file entirely. The advantage of regular expressions lies in rule uniformity; as long as the file content format is consistent, the software will extract according to the same logic.

Taking (?<=Book ID:)\d+ as an example, it explicitly requires that Book ID: appear before the digits, so it will not mistakenly use other numbers from body text paragraphs as filenames. It also returns only the digit part, preventing the filename from carrying redundant label text. This characteristic of "precise positioning and clean extraction" is a very important requirement in batch renaming scenarios.

For office workers, mastering a complex regex system is unnecessary; understanding the writing approach for this example is sufficient: a fixed prefix plus the character type to extract. This same logic can be adapted when encountering formats like "Number:12345", "ID=12345", or "Code:ABC001" in the future.

Common Questions and Precautions

1. Is it okay if the Book ID in the file content is not on the first line?

The Book ID in the screenshots for this article is on the first line, but when using custom formula matching, the key is whether the regular expression can find the target in the text content. It is still recommended to keep the file format stable during actual processing and use samples for testing to confirm the software can match the required number.

2. Does the case need to match in the regular expression?

The example rule writes Book ID:, which applies to situations where the file also writes it as Book ID:. If your text uses book id:, BOOK ID:, or the Chinese "图书编号:", you need to modify the matching rule according to the actual content.

3. What happens if the extraction result is empty?

If a file does not match any content, the expected new filename cannot be obtained. To avoid inconsistent results after batch processing, it is recommended to spot-check file content beforehand and ensure, as much as possible, that each file contains a number conforming to the rule.

4. Will the body content of the TXT file be changed?

Judging from the function name and processing goal, this function is used to rename text files using their content, with the focus on changing the filename, not modifying the body content. The examples before and after processing also show changes in the filename. For safety, it is still recommended to back up important files first.

5. Can a similar approach be used for Word and PDF files?

In the interface, function entries like "Rename Word files using file content" and "Rename PDF files using file content" can be seen. Therefore, for office documents like doc, docx, and pdf, corresponding tools can also be selected for processing. However, the operation steps and screenshots in this article are for TXT text files; when processing other formats, the corresponding function should be selected.

Summary: Automatically Mapping TXT Filenames to Body Text Numbers

Through the "Rename text files using file content" function, HeSoft Doc Batch Tool can help users batch replace a large number of meaningless TXT filenames with the Book ID numbers from the body text. The key to the entire process is importing files, selecting custom formula matching, entering (?<=Book ID:)\d+, choosing to cover the entire filename, and completing the save and start processing steps following the wizard.

For users who need to organize book texts, document numbers, log records, or business export files, this method significantly reduces the time spent opening files one by one to copy numbers and lowers the probability of manual renaming errors. It is recommended to verify the regex rule with a few sample files first, and after confirming the extraction results are correct, proceed to batch process the entire folder, making file management more standardized and efficient.


KeywordAuto-rename TXT files , rename by Book ID , batch rename text content
Creation Time2026-07-09 06:34:39

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!