如何从列表中查找和替换部分文件名 [英] How to find and replace part of filenames from list
问题描述
我有一个文件列表,我需要在每个文件的同一部分重命名这些文件,并使用不同的值。
I have a list of files that I need to rename at the same part of each file, with different values.
示例:
BL_1402B103_abc.wav > BL_C1234-1_abc.wav
BL_15489B59_abc.wav > BL_C1234-5_abc.wav
因此在上面的第一个示例中,我想替换 1402B103
与 C1234-1
所有文件的长度相同,我要替换的部分用 分隔_
。
So in the first example above I want to replace the 1402B103
with C1234-1
all the files are the same length and the sections I want to replace are separated by "_
".
我有一些代码可用于查找/替换文件名的一部分,但是我需要对数百个文件执行此操作-有没有办法将Pattern =&替换=作为来自csv / list的变量并批量运行?
I have some code for finding/replacing parts of a filename but I need to do this for hundreds of files - is there a way to pull Pattern= & Replace= as variables from a csv/list and run as a batch?
Setlocal enabledelayedexpansion
Set "Pattern=1402B103"
Set "Replace=C1234-1"
For %%f in (*.wav) Do (
Set "File=%%~f"
Ren "%%f" "!File:%Pattern%=%Replace%!"
)
推荐答案
您可以创建 csv
文件并添加搜索/替换字符串:
You could create a csv
file and add your search/replace strings:
myfile.csv
1402B103,C1234-1
15489B59,C1234-5
etc,etc
批处理文件 myrename.cmd
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,* delims=," %%i in (myfile.csv) do (
set "search=%%i"
set "replace=%%j"
call :fix
)
exit /b
:fix
for %%a in (*!search!*.wav) do (
set "file=%%a"
set "file=!file:%search%=%replace%!!"
echo ren "%%~fa" "!file!"
)
它将坐下对于 csv
文件中的每个字符串,用逗号分隔,将第一个元变量分配给 search
变量,第二个替换
变量。然后我们只需调用该过程对每个对象进行替换。
It will seatch for each string in the csv
file, split by comma assign the first meta variable to the search
variable and the second to the replace
variable. Then we simply do the replace for each by calling that procedure.
注意!在这种情况下,我使用了 echo
之前 ren
查看测试结果。只有对结果感到满意后,才能删除 echo
来执行实际的命令。
Note!! in this instance I used echo
before ren
for testing results. Only once you are happy with your results should you remove echo
to perform the actual command.
这篇关于如何从列表中查找和替换部分文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!