28

I want to rename file name like "how-to-rename-file.jpg" to "how-to-reuse-file.jpg" by using a Windows batch file

I.e. I only want to replace one or two words in a file name.

Marco Demaio
  • 33,578
  • 33
  • 128
  • 159
Varun
  • 501
  • 2
  • 6
  • 12

2 Answers2

57
@echo off

Set "Filename=how-to-rename-file.jpg"
Set "Pattern=rename"
Set "Replace=reuse"

REM Call Rename "%Filename%" "%%Filename:%Pattern%=%Replace%%%"

Call Echo %%Filename:%Pattern%=%Replace%%%
:: Result: how-to-reuse-file.jpg

Pause&Exit

I give you other example for a loop of files:

UPDATE:

I've missed some things in the syntax 'cause fast-typing my last edit, here is the corrected code:

@echo off
Setlocal enabledelayedexpansion

Set "Pattern=rename"
Set "Replace=reuse"

For %%# in ("C:\Folder\*.jpg") Do (
    Set "File=%%~nx#"
    Ren "%%#" "!File:%Pattern%=%Replace%!"
)

Pause&Exit

PS: You can read here to learn more about substring: http://ss64.com/nt/syntax-substring.html http://ss64.com/nt/syntax-replace.html

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
37

The code above doesn't rename the files - The paths are an issue and the source filename is incorrect.

This will work on files in the current folder - except those with ! in the names will be a problem.

@echo off
Setlocal enabledelayedexpansion

Set "Pattern=rename"
Set "Replace=reuse"

For %%a in (*.jpg) Do (
    Set "File=%%~a"
    Ren "%%a" "!File:%Pattern%=%Replace%!"
)

Pause&Exit
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • I know this is an old topic, but what should I add to define a folder instead of using the local folder? – Schuere Jan 05 '15 at 08:24
  • 1
    Place `pushd "c:\folder"` above the loop, or use `cd /d "c:\folder"` – foxidrive Jan 06 '15 at 10:27
  • It works like a charm! Great stuff! – Marco Demaio Jan 14 '15 at 12:31
  • Just one note, I think `Set "File=%%~a"` can be replaced with `Set "File=%%a"` (with no ~) because the ~ is used with a parameter extension that you are not using. http://ss64.com/nt/syntax-args.html – Marco Demaio Jan 14 '15 at 12:47
  • @MarcoDemaio You are absolutely correct that the tilda is not needed, though it will never cause any issue there either. – foxidrive Jan 14 '15 at 14:28
  • @foxidrive Your solution is an absolute beauty. Very precise and simple to understand. Helped me a great deal. – nam Apr 12 '16 at 17:48
  • This works really neat!! – Rodrigo Zuluaga Jan 18 '18 at 17:42
  • 2
    This worked great. Now could you make it recursive on subfolders? – Scott Hutchinson Apr 04 '18 at 20:42
  • @RodrigoZuluaga I'm curious how you guys made it work. I tried all that is posted above. No luck for me. – Data Engineer May 25 '20 at 04:58
  • @enigma6205 create a in notepad a yourname.bat file, place that top code, and put the file where the files you're renaming are. If you don't wanna mess around with bat files u can download Bulk Rename Utility. I don't think im allowed to to place url here, so search it on google, the site is a .co.uk. I found this and replaced all my bat files for masive renaming. – Rodrigo Zuluaga May 25 '20 at 13:46