Rename all files in a directory with a Windows batch script
Asked Answered
A

3

25

How would I write a batch or cmd file that will rename all files in a directory? I am using Windows.

Change this:

750_MOT_Forgiving_120x90.jpg
751_MOT_Persecution_1_120x90.jpg
752_MOT_Persecution_2_120x90.jpg
753_MOT_Hatred_120x90.jpg
754_MOT_Suffering_120x90.jpg
755_MOT_Freedom_of_Religion_120x90.jpg
756_MOT_Layla_Testimony_1_120x90.jpg
757_MOT_Layla_Testimony_2_120x90.jpg

To this:

750_MOT_Forgiving_67x100.jpg
751_MOT_Persecution_1_67x100.jpg
752_MOT_Persecution_2_67x100.jpg
753_MOT_Hatred_67x100.jpg
754_MOT_Suffering_67x100.jpg
755_MOT_Freedom_of_Religion_67x100.jpg
756_MOT_Layla_Testimony_1_67x100.jpg
757_MOT_Layla_Testimony_2_67x100.jpg
Acceptor answered 21/2, 2012 at 18:28 Comment(2)
windows and whatever runs in a .bat or .cmd file?Acceptor
Dude, if you want help you can't expect everyone to be psychic.Humboldt
F
41

A FOR statement to loop through the names (type FOR /? for help), and string search and replace (type SET /? for help).

@echo off
setlocal enableDelayedExpansion
for %%F in (*120x90.jpg) do (
  set "name=%%F"
  ren "!name!" "!name:120x90=67x100!"
)


UPDATE - 2012-11-07

I've investigated how the RENAME command deals with wildcards: How does the Windows RENAME command interpret wildcards?

It turns out that this particular problem can be very easily solved using the RENAME command without any need for a batch script.

ren *_120x90.jpg *_67x100.*

The number of characters after the _ does not matter. The rename would still work properly if 120x90 became x or xxxxxxxxxx. The important aspect of this problem is that the entire text between the last _ and the . is replaced.

Fullscale answered 21/2, 2012 at 18:47 Comment(3)
I'm not sure if this is a complete example. But This worked for me like a charm :)Silveira
@Anand - I've added a much simpler solution using just the REN command. The link in your comment is basically the same as my original answer.Fullscale
Can you split out your two answers? I would leave the ren one-liner in this one; it's the best!Abysm
A
11

As of Windows 7 you can do this in one line of PowerShell.

powershell -C "gci | % {rni $_.Name ($_.Name -replace '120x90', '67x100')}"

Explanation

powershell -C "..." launches a PowerShell session to run the quoted command. It returns to the outer shell when the command completes. -C is short for -Command.

gci returns all the files in the current directory. It is an alias for Get-ChildItem.

| % {...} makes a pipeline to process each file. % is an alias for Foreach-Object.

$_.Name is the name of the current file in the pipeline.

($_.Name -replace '120x90', '67x100') uses the -replace operator to create the new file name. Each occurrence of the first substring is replaced with the second substring.

rni changes the name of each file. The first parameter (called -Path) identifies the file. The second parameter (called -NewName) specifies the new name. rni is an alias for Rename-Item.

Example

$ dir
 Volume in drive C has no label.
 Volume Serial Number is A817-E7CA

 Directory of C:\fakedir\test

11/09/2013  16:57    <DIR>          .
11/09/2013  16:57    <DIR>          ..
11/09/2013  16:56                 0 750_MOT_Forgiving_120x90.jpg
11/09/2013  16:57                 0 751_MOT_Persecution_1_120x90.jpg
11/09/2013  16:57                 0 752_MOT_Persecution_2_120x90.jpg
               3 File(s)              0 bytes
               2 Dir(s)  243,816,271,872 bytes free

$ powershell -C "gci | % {rni $_.Name ($_.Name -replace '120x90', '67x100')}"

$ dir
 Volume in drive C has no label.
 Volume Serial Number is A817-E7CA

 Directory of C:\fakedir\test

11/09/2013  16:57    <DIR>          .
11/09/2013  16:57    <DIR>          ..
11/09/2013  16:56                 0 750_MOT_Forgiving_67x100.jpg
11/09/2013  16:57                 0 751_MOT_Persecution_1_67x100.jpg
11/09/2013  16:57                 0 752_MOT_Persecution_2_67x100.jpg
               3 File(s)              0 bytes
               2 Dir(s)  243,816,271,872 bytes free
Abysm answered 11/9, 2013 at 15:59 Comment(3)
@MethodMan what value should the timestamp have?Abysm
I figured it out by messing around with the RobbCopy script and creating a goto function inside the bat file.. thanks I am not familiar with powershell as much but if I had to have a timestamp value it would be yyyymmddss with a mask that would have _1 _2 _3 etc... depending on the number of files.. I was able to do this in a batch file using robocopy..Psychology
In Windows Powershell, I got a "The term '.Name' is not recognized" error. I was trying to replace blanks with underscores. In the error message, there was a squiggly line (~~~~~) under the second .Name. I then tried simply Get-ChildItem | Foreach-Object {Rename-item $_.Name ($_.Name -replace ' ', '')} and got a "Source and destination path must be different." error. Finally, this worked: Get-ChildItem -File | Foreach-Object {Rename-item $_.Name ($.Name -replace ' ', '_')} I think it was because there were zip files in the folder where I was renaming.Canula
K
-1

I found this batch script quite useful (see below), which is basically a variation on dbenham's solution:

@echo off
setlocal enabledelayedexpansion

set "search=searchtext"
set "replace=replacetext"
set "extension=.jpg"

for %%F in (*%search%*) do (
 set "filename=%%~nF"
 set "newname=!filename:%search%=%replace%!"
 ren "%%F" "!newname!!extension!"
)

endlocal

You just save as a .bat file and run it in the folder with the files you want to rename. First you switch out the search text and enter the replacement text and save it before running. Then it will only rename those files that contain the entered string (now "searchtext"). Using the wild-cards like this (i.e. *%search%* ), the string can be anywhere in the name.

However, this version also replaces the extension to the entered extension name (.jpg). For my own use that was what I was looking for, but if you don't want to change the extension, dbenham's version with "name" instead of "filename" in combination with deleting the parts about the extension will work better.

I ran it a couple of times with different files, including ones with underscores, and it worked as intended so far.

Kerato answered 17/11, 2023 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.