bat文件用自定义名称重命名多个文件

bat文件用自定义名称重命名多个文件

本文介绍了Windows bat文件用自定义名称重命名多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个文件夹,每个文件夹中都有多个文件.文件名如下:静态字符串 -1.wav 静态字符串 -2.wav .... 静态字符串- 10.wav ... 静态字符串 -99.wav 静态字符串 -100.wav ...等等.静态字符串保持不变,但.wav之前的数字从1-999更改.现在的问题是,如果我想在任何媒体播放器上播放此音乐或想要加入所有文件,则Windows的排序方式将为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文件有多行,并带有单独的重命名命令,如下所示:

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:

@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.

推荐答案

以下是对 Clayton的答案

  • 仅需要修改小于100的数字
  • 该脚本会自动使用任何静态前缀.请参阅 Windows RENAME命令如何解释通配符?,以获取有关其工作原理的说明.
  • 该脚本报告所有无法重命名的文件名
  • 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
@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"
)

或者,您可以获取我的 JREPL.BAT正则表达式重命名实用程序并简单地使用:

Or, you could grab my JREPL.BAT regular expression renaming utility and simply use:

jren "-(\d{1,2})(?=\.wav$)" "'-'+lpad($1,'000')" /j

这篇关于Windows bat文件用自定义名称重命名多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 15:56