【问题标题】:Renaming files if they contain 2 strings with batch如果文件包含 2 个带批处理的字符串,则重命名文件
【发布时间】:2019-02-14 18:38:13
【问题描述】:

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

)

谢谢

【问题讨论】:

  • codes.txt 中的代码是什么,foo 在 %%b 的名称之前还是之后?还是两者兼而有之?
  • 代码将是这样的,纯数字:3614271992932。foo 可以在 %%b 之前或之后,具体取决于文件名。

标签: batch-file rename batch-processing batch-rename


【解决方案1】:

假设文件`codes包含:

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
)

首先它循环遍历code.txt,使用换行符作为分隔符。然后它将在包含代码的文件上执行一个目录,并在名称中的任何位置为 foo 执行一个findstr。使用上述文件,它将回显找到的仅有的 2 个匹配项:

foo1234.txt
1234ABCfoo.txt

它不会匹配5678.txt,因为它的名称中没有foo

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 2013-05-29
    • 1970-01-01
    相关资源
    最近更新 更多