首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从文本文档重命名的Windows批处理

从文本文档重命名的Windows批处理
EN

Stack Overflow用户
提问于 2016-11-09 00:49:09
回答 1查看 33关注 0票数 0

我制作了一个批处理脚本来重命名大量的文件。它获取它们的名称并在文本文档中搜索它,复制行并从其中获取我需要的数据,然后重命名该文件。

在大多数情况下,它似乎工作得很好,但我无法检查它是如何工作的,因为它不断地在控制台中产生错误/警告。

代码语言:javascript
复制
@echo off
set ogg=.ogg
Setlocal EnableDelayedExpansion
for %%a in (*.ogg) do (
    set fileNameFull=%%a
    set fileName=!fileNameFull:~0,-4!
    for /F "delims=" %%a in ('findstr /I !fileName! strings.txt') do (
        endlocal
        set "stringLine=%%a%ogg%"
    )
    Setlocal EnableDelayedExpansion
    set fullString=!stringLine:~26!
    ren %%a "!fullString!"
)

pause

代码工作,我只是希望能够跟踪进度,因为10,000多个文件一次被重命名,而且我没有迹象表明这个过程有多远。

这些错误是:

“不能打开.”“命令的语法不正确。”

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-09 01:33:24

代码语言:javascript
复制
@echo off
Setlocal EnableDelayedExpansion
for %%a in (*.ogg) do (
    for /F "delims=" %%q in ('findstr /I /L /c:"%%~na" strings.txt') do (
     set "stringLine=%%q"
    )
    ECHO ren "%%a" "!stringLine:~26!.ogg"
)

pause

此代码应与您发布的代码等效,但已修复。

修复:

代码语言:javascript
复制
Removed the endlocal/setlocal complication - not required  
changed the inner `for` metavariable - must not be duplicate `%%a`  
Changed the `findstr` switches - add `/L` for literal and `/c:` to force single token in case of a separator-in-name; use `%%~na` to specify "the name part of `%%a`" to avoid the substringing gymnastics.
removed said gymnastics
Removed 2-stage string manipulation of destination filename
Removed superfluous setting of `ogg`

生成的代码应该与原来的代码重复,只不过它只需报告rename指令。您应该用一个小的有代表性的样本来验证这一点。

用于计数/进展:

代码语言:javascript
复制
set /a count=0
for %%a in (*.ogg) do (
    for /F "delims=" %%q in ('findstr /I /L /c:"%%~na" strings.txt') do (
     set "stringLine=%%q"
    )
    ECHO ren "%%a" "!stringLine:~26!.ogg"
    set /a count +=1
    set /a stringline= count %% 1000
    if %stringline% equ 0 echo !count! Processed
)

pause

它应该显示你每1000次的进步。

你可以用

代码语言:javascript
复制
    if %stringline% equ 0 echo !count! Processed&pause

在进展前等待用户操作.

顺便说一句,我假设新名称来自您文件中的27+列,因为您没有向我们展示一个sample.Also,您应该知道一个简单的findstr会将目标字符串定位为文件中任何位置的子字符串--无论是作为新名称还是旧名称。如果在/B上调用findstr开关,则字符串将仅在行的开头匹配。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40498801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档