如果文件包含2个批处理字符串,则重命名文件

问题描述 投票:0回答:1

我有一个文件“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'

)

谢谢

batch-file rename batch-processing batch-rename
1个回答
1
投票

假设文件`代码包含:

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部分更改为你想要实现的命令。

© www.soinside.com 2019 - 2024. All rights reserved.