Batch file rename - find and replace

自闭症网瘾萝莉.ら 提交于 2019-12-11 12:17:02

问题


I'm trying to write a Windows batch file to rename a set of files based on their original name. What I essentially want to do is find text within a file name and replace it with other text. For example, if the files had the naming structure "Family Christmas 001.jpg" I might want to change it to "Photos - Xmas 001.jpg". ie replace "Family Christmas" with "Photos - Xmas". This is just an example.

I've found a piece of code from a user of this site, dbenham, that does almost exactly what I'm after. In this example he's replacing "120x90" in a filename to "67x100" Here's the code:

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

Can anyone help me adapt this code to make it handle spaces in the file name?

Thanks


回答1:


All you're missing is quotes within the FOR statement - to follow your example:

@echo off
setlocal enableDelayedExpansion
for %%F in ("Family Christmas*.jpg") do (
  set "name=%%F"
  ren "!name!" "!name:Family Christmas=Photos - Xmas!"
)


来源:https://stackoverflow.com/questions/23763363/batch-file-rename-find-and-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!