Batch file 使用批处理重命名包含2个字符串的文件

Batch file 使用批处理重命名包含2个字符串的文件,batch-file,rename,batch-processing,batch-rename,Batch File,Rename,Batch Processing,Batch Rename,我有一个文件“codes.txt”,每行包含一个代码 我试图在文件夹中搜索并重命名名称中包含代码的所有文件,以及另一个字符串: @echo off for /F "tokens=*" %%b in (codes.txt) do ( // If file names in folder contain %%b and "foo", rename to %%b.'string' // If file names in folder contain %%b and "foo2", rename

我有一个文件“codes.txt”,每行包含一个代码

我试图在文件夹中搜索并重命名名称中包含代码的所有文件,以及另一个字符串:

@echo off

for /F "tokens=*" %%b in (codes.txt) do  (

// If file names in folder contain %%b and "foo", rename to %%b.'string'
// If file names in folder contain %%b and "foo2", rename %%b.'string2'

)

谢谢

假设文件“代码”包含:

1234
5678
您的目录中有一个文件名为:

foo1234.txt
1234ABCfoo.txt
5678.txt
然后此脚本将执行以下操作:

@echo off
for /f %%i in (codes.txt) do (
   for /f %%a in ('dir /b /a-d *%%i* ^| findstr "foo"') do echo %%a
)
首先,它通过codes.txt循环,使用换行符作为参数。然后,它将对包含代码的文件执行dir,并在名称中的任何位置对foo执行
findstr
。使用上述文件,它将只回显找到的2个匹配项:

foo1234.txt
1234ABCfoo.txt
它将不匹配
5678.txt
,因为它的名称中没有
foo


显然,您需要将我的脚本中的
echo
部分更改为您想要实现的命令。

代码中的代码是什么。txt也将在
%%b的名称之前或之后?或者两者都有?代码如下,纯数字:3614271992932。foo可以在%%b之前或之后,具体取决于文件名。