I have multiple folders with multiple files in each of them. The file names are as following: static-string-1.wav static-string-2.wav .... static-string-10.wav ... static-string-99.wav static-string-100.wav ... and so on. The static string remains same but the number before .wav changes from 1 - 999. Now the problem is if I want to play this on any media player or want to join all the files, windows will sort like 1,10,100,2,20,200 not 1,2,3,4,5,6,7,8,9,10 which messes up the playback. So to fix this, I have to rename each file from static-string-1.wav to static-string-0001.wav and so on. Currently, I am doing a dir command to an output file and then I copy the file list in excel from where I do some playing around with concatenate and text to columns and come up with two columns of old name and new name which I then again convert to a .bat file with and run it. The bat file has multiple rows with individual rename commands something like this:
我有多个文件夹,每个文件夹中都有多个文件。文件名如下:static-string-1.wav static-string-2.wav .... static-string-10.wav ... static-string-99.wav static-string-100.wav。 .. 等等。静态字符串保持不变但.wav之前的数字从1 - 999变化。现在问题是如果我想在任何媒体播放器上播放或想要加入所有文件,窗口将排序为1,10,100,2,20,200不是1,2,3,4,5,6,7,8,9,10,它会混淆播放。所以要解决这个问题,我必须将每个文件从static-string-1.wav重命名为static-string-0001.wav,依此类推。目前,我正在对输出文件执行dir命令,然后我在excel中复制文件列表,从中进行连接和文本到列的操作,并提出两列旧名称和新名称然后我再次转换为.bat文件并运行它。 bat文件有多行,各个重命名命令如下所示:
@echo off
rename <oldname1> <newname0001>
rename <oldname2> <newname0002>
.
..
exit
It is getting the work done but I know there is and easier and more efficient way to use for loops. I saw few example and previous answers but they dont have a similar requirement as me. Any help will be appreciated.
它正在完成工作,但我知道有更简单,更有效的循环使用方式。我看到了一些示例和之前的答案,但他们没有像我一样的要求。任何帮助将不胜感激。
2 个解决方案
#1
0
Below is a significant improvement to Clayton's answer
以下是克莱顿答案的重大改进
- Only numbers less than 100 need be modified
- The script automatically works with any static prefix. See How does the Windows RENAME command interpret wildcards? for an explanation of how this works.
- The script reports any file names that could not be renamed
只需要修改少于100的数字
该脚本自动适用于任何静态前缀。请参阅Windows RENAME命令如何解释通配符?解释这是如何工作的。
该脚本报告无法重命名的任何文件名
@echo off
setlocal enableDelayedExpansion
for /l %%N in (1 1 99) do (
set "n=00%%N"
for %%F in (*-%%N.wav) do ren "%%F" *-!n:~-3!.* || >&2 echo ERROR: Unable to rename "%%F"
)
Or, you could grab my JREPL.BAT regular expression renaming utility and simply use:
或者,您可以获取我的JREPL.BAT正则表达式重命名实用程序,只需使用:
jren "-(\d{1,2})(?=\.wav$)" "'-'+lpad($1,'000')" /j
#2
2
Leading zeros can be added and (later) truncated if not needed. This question is a possible duplicate, but I don't like how files are sorted like that either.
如果不需要,可以添加前导零并(稍后)截断。这个问题可能是重复的,但我不喜欢文件的排序方式。
Delaying the expansion allows b
to change within the for loop instead of being static (haha puns) throughout the whole program. Therefore you can increment b
each loop and rename the files. This is a simple example:
延迟扩展允许b在for循环中改变而不是在整个程序中静态(哈哈双关语)。因此,您可以增加每个循环b并重命名文件。这是一个简单的例子:
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /l %%a in (1,1,99) do (
set b=00%%a
rename static-string-%%a.wav static-string-!b:~-2!.wav
)
This should work. Contact me if you need more help
这应该工作。如果您需要更多帮助,请与我联系
#1
0
Below is a significant improvement to Clayton's answer
以下是克莱顿答案的重大改进
- Only numbers less than 100 need be modified
- The script automatically works with any static prefix. See How does the Windows RENAME command interpret wildcards? for an explanation of how this works.
- The script reports any file names that could not be renamed
只需要修改少于100的数字
该脚本自动适用于任何静态前缀。请参阅Windows RENAME命令如何解释通配符?解释这是如何工作的。
该脚本报告无法重命名的任何文件名
@echo off
setlocal enableDelayedExpansion
for /l %%N in (1 1 99) do (
set "n=00%%N"
for %%F in (*-%%N.wav) do ren "%%F" *-!n:~-3!.* || >&2 echo ERROR: Unable to rename "%%F"
)
Or, you could grab my JREPL.BAT regular expression renaming utility and simply use:
或者,您可以获取我的JREPL.BAT正则表达式重命名实用程序,只需使用:
jren "-(\d{1,2})(?=\.wav$)" "'-'+lpad($1,'000')" /j
#2
2
Leading zeros can be added and (later) truncated if not needed. This question is a possible duplicate, but I don't like how files are sorted like that either.
如果不需要,可以添加前导零并(稍后)截断。这个问题可能是重复的,但我不喜欢文件的排序方式。
Delaying the expansion allows b
to change within the for loop instead of being static (haha puns) throughout the whole program. Therefore you can increment b
each loop and rename the files. This is a simple example:
延迟扩展允许b在for循环中改变而不是在整个程序中静态(哈哈双关语)。因此,您可以增加每个循环b并重命名文件。这是一个简单的例子:
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /l %%a in (1,1,99) do (
set b=00%%a
rename static-string-%%a.wav static-string-!b:~-2!.wav
)
This should work. Contact me if you need more help
这应该工作。如果您需要更多帮助,请与我联系