我制作了一个批处理脚本来重命名大量的文件。它获取它们的名称并在文本文档中搜索它,复制行并从其中获取我需要的数据,然后重命名该文件。
在大多数情况下,它似乎工作得很好,但我无法检查它是如何工作的,因为它不断地在控制台中产生错误/警告。
@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多个文件一次被重命名,而且我没有迹象表明这个过程有多远。
这些错误是:
“不能打开.”“命令的语法不正确。”
发布于 2016-11-09 01:33:24
@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此代码应与您发布的代码等效,但已修复。
修复:
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指令。您应该用一个小的有代表性的样本来验证这一点。
用于计数/进展:
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次的进步。
你可以用
if %stringline% equ 0 echo !count! Processed&pause在进展前等待用户操作.
顺便说一句,我假设新名称来自您文件中的27+列,因为您没有向我们展示一个sample.Also,您应该知道一个简单的findstr会将目标字符串定位为文件中任何位置的子字符串--无论是作为新名称还是旧名称。如果在/B上调用findstr开关,则字符串将仅在行的开头匹配。
https://stackoverflow.com/questions/40498801
复制相似问题