JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Batch file to remove X characters from a file name

Author:JIYIK Last Updated:2025/03/22 Views:

This article explains how we can remove specific characters from the filename of a file using a batch script. We will cover several methods below to rename files on Windows.


File Explorer Renaming on Windows

File Explorer offers the least flexible renaming option, even though it’s one of the simplest.

  1. Navigate to the folder that contains the file you want to rename.
  2. Put the files in the order you want them renamed.
  3. Select all files in the folder using CTRL+A and choose the Rename option from the context menu.
  4. Enter the new file name and hit Enter.

Rename files in batches using Command Prompt

Windows Command Prompt is a specialized tool for executing commands, repairing files, running batch files, and launching scripts. With the correct syntax, you can perform amazing tasks such as managing and repairing disk partitions, running applications, and even batch renaming files.

Here's how to rename files in batch in Windows using the Command Prompt.

For batch file renaming, the Windows Command Prompt offers more functionality.

The ren command allows you to rename multiple files at once. The abbreviation for rename is ren.

Although you can change the file extension and use wildcard characters * and ? with this command, you cannot move the file to a new folder after renaming it.

Rename a single file

To rename a single file, use the following command.

ren filename.jpg newfilename.jpg

Rename numbers in multiple files

You can use wildcards to modify the names of a large number of files. For example, you can use the following command to change the number of digits in the file name.

ren document??.txt document3??.txt

The program can find any matching files while also generating renamed files because the question mark wildcard character behaves the same as any character in this context.

Batch rename files with suffix

Consider adding a suffix to the collection of files. You can do this by issuing the following command:

ren *.* ???????-test.*

The asterisk wildcard character replaces any character in this command. *.*It means that you can search for any file with any extension in this folder.

The command is instructed to use an existing file name of up to seven characters in the second part (containing all the question marks), but append -test as a suffix. The asterisk again indicates that the command should be applied to any file extension.

If you want to add a prefix, move the -test element of the command to the front, as shown in the following example:

ren *.* test-???????.*

Batch file to remove X characters from a file name

You can also use batch file rename to remove part of the file name. Consider a set of files named "jan-budget.xlsx", "feb-budget.xlsx", "mar-budget.xlsx", etc.

The "-budget" suffix can be removed using the following command.

ren ???-budget.xlsx ???.xlsx

Let’s look at a practical example.

Suppose we have a directory containing files, and we want to remove the last 33 characters of each file name. How can we do this?

Here is a batch file that we can use to accomplish the above tasks.

@echo off
setlocal enabledelayedexpansion

set "folderpath=[Your Folder Here...]"
cd %folderpath%
for /f %%a in ('dir /b "*.txt"') do (
   set "fname=%%~na"
   ren "%%a" "!fname:~0,-33!.txt"
)
endlocal

The above script will delete the last 33 characters of the files in the specified folder.

In short, we can use renthe command to rename multiple files in a batch file on Windows.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Stop a running process from a batch file

Publish Date:2025/03/21 Views:201 Category:OPERATING SYSTEM

This article explains how to stop a running process from a batch file in Windows. We use Batch's taskkill command to terminate the running process. 请注意 , the command will be executed only if the specified process is open. Batch file t

Get the current batch file directory

Publish Date:2025/03/21 Views:125 Category:OPERATING SYSTEM

This article demonstrates how to determine the location of a batch file. Batch scripts are great for automation. Sometimes you may need to get the location of a batch file. This article will help you determine the working directory and batc

Running batch files remotely

Publish Date:2025/03/21 Views:176 Category:OPERATING SYSTEM

Sometimes we need to use a computer remotely from a different location. We can use some third-party software such as TeamViewer to do this. But we can execute batch files from a remote directory without the need for third-party software. In

Run the batch file as administrator

Publish Date:2025/03/20 Views:142 Category:OPERATING SYSTEM

Batch files consist of commands that are executed by the Command Prompt. Some of these commands cannot be run without administrator privileges; therefore, it is important to run batch files as an administrator. You can manually run a batch

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial