如何通过在Windows中使用批处理替换子字符串来重命名文件 [英] How to rename file by replacing substring using batch in Windows
问题描述
我想将文件名称重命名为how-to 重命名 -file.jpg到how-to strong> -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:
更新:
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:您可以在这里阅读更多关于子字符串的信息:
http://ss64.com/nt/syntax-substring.html
http://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屋!