如何通过在 Windows 中使用批处理替换子字符串来重命名文件 [英] How to rename file by replacing substring using batch in Windows
问题描述
我想将文件名像 "how-to-rename-file.jpg" 重命名为 "how-to-reusestrong>-file.jpg" 使用 Windows 批处理文件
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.
推荐答案
@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:
更新:
我在语法中遗漏了一些东西,因为我的最后一次编辑太快了,这里是更正后的代码:
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:您可以在这里阅读以了解有关子字符串的更多信息:http://ss64.com/nt/syntax-substring.htmlhttp://ss64.com/nt/syntax-replace.html
PS: You can read here to learn more about substring: http://ss64.com/nt/syntax-substring.html http://ss64.com/nt/syntax-replace.html
这篇关于如何通过在 Windows 中使用批处理替换子字符串来重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!