Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Batch script rename file using Command Line (CMD) – PowerShell – Windows 10

The following article will use the “rename” or “ren” Command to rename the file using a command prompt. Also, we will try to show some trick playing with the rename command.

Batch script rename file using command prompt

Syntax:

RENAME [drive:][path][directoryname1 | filename1] [directoryname2 | filename2]

Let’s see it in some examples of renaming the file.

How to rename file using command line:

  1. Searching on windows the “cmd” name an open as administrator
  2. Navigate to your path where you need to rename the file by type cd and the path
  3. Click Enter
  4. Execute the following command
rename "TESTA.txt" "TESTB.txt"

Note! The quotation marks in the command are only required if the name includes spaces.

The above command will rename the file name “TESTA.txt” to “TESTB.txt”. You need to be located at the CMD on the folder where the file is.

If the “TESTA.txt” file is not located in your current directory, you must specify the path to the file as a prefix to the file name. For example, if the file was in the “C:” directory, you would type a command similar to the following example.

rename “c:\TESTA.txt” “TESTB.txt”

Since the “rename” command can address extensions, you can also use it to change the extensions of the file.

Rename a file keeping the original

Use the following command at the Windows command line or within a batch file.

xcopy "TESTA.txt" "TESTB.txt"

The command will create a copy of the original files with the new extension.

Rename a single file with the move command

Like using the rename command, you can also use the move command to rename a file as shown.

move "TESTA.txt" "TESTB.txt"

What you need to know:

  1. If the files are being used by a program, then the rename command fails with the below error.
The process cannot access the file because it is being used by another process.
  1. You also need to have sufficient privileges to rename the file.
Access is denied.
  1. Keep in mind that the rename changes just the file name, it does not convert a file from one type to another. For example, renaming a pdf file ‘test1.pdf’ to ‘test1.docx does not make the file readable in Microsoft Word.

How to create a simple batch script to rename the file

If you are going to automate the above activities or to execute over the network you will need to create a batch file. For example, you need to rename some files on all computers of the network:

  1. Open a notepad file
  2. Copy the below command
@echo off
rename “c:\TESTA.txt” “TESTB.txt”
  1. Save as rename.bat
  2. Execute the file and the file “TESTA.txt” will be changed to “TESTB.txt”

The batch files can be used for many other simple actions like to delete files in bulk or to delete folders.

Rename the file using PowerShell:

PowerShell offers even more flexibility for renaming files in a command-line environment. Using PowerShell, you can pipe the output of one command—known as a “commandlet” in PowerShell terms—to another command, just like you can on Linux and other UNIX-like systems. The two important commands you’ll need are Dir, which lists the files in the current directory, and Rename-Item, which renames an item (a file, in this case). Pipe the output of Dir to Rename-Item and you’re in business.

  1. Open Start.
  2. Search for PowerShell and click the top result to open the app.
  3. Type the following command to rename a single file and press Enter:
rename-item  "TESTA.txt" "TESTB.txt"

Advanced features of “Rename” Commands

Below you will find some undocumented features of “Rename” commands to chop off everything from a file name after the last occurrence of a specified character:

REN TESTA.txt *S

Will rename TESTA.txt to TES.

REN TESTA.txt *T

Will not change the name at all (remember: the last occurrence…?).

REN TESTA.txt *ST

Will rename TESTA.txt to TEST.

REN TESTA.txt *sa

Will rename TESTA.txt to TESA, so it seems to mean chop off everything after the last S and then append an A.

Conclusion:

This is all about some methods and tricks to play with the “rename” or “ren” command. The first goal was to create batch script rename file using Command-Line.

The post Batch Script Rename file using Command Line (CMD) – PowerShell – Windows 10 appeared first on Get IT Solutions.



This post first appeared on Get It Solutions, please read the originial post: here

Share the post

Batch script rename file using Command Line (CMD) – PowerShell – Windows 10

×

Subscribe to Get It Solutions

Get updates delivered right to your inbox!

Thank you for your subscription

×