处理文件名中的特殊字符时批量重命名问题
我在 c:\files 中有数百个 *.mp3 文件。 那里有所有可以想象到的文件名,例如
- milad.mp3(表现良好)
- hey you.mp3(文件名中的空格)
- systemofadown.mp3(长文件名)
- howdy(1).mp3(文件名中的括号)
以及最后三个条件的任意组合。 我想将文件重命名为 001-test.mp3, 002-mp3, ... 哪个文件使用哪个名称并不重要。 已经编写了一个批处理文件来进行重命名。 这是我的代码(添加了行号):
01 rem @echo off
02 cls
03 set _number=%1
04 lfnfor on
05
06 :F1TO10
07 IF NOT EXIST *.mp3. goto end
08 if %_number% gtr 9 goto F10TO100
09 for /f %%a IN ('dir /b *.mp3') do rename %%~na.mp3 00%_number%-test.mp4
10 set /a _number +=1
11 goto F1TO10
12
13 :F10TO100
14 IF NOT EXIST *.mp3. goto end
15 if %_number% gtr 99 goto F100TO1000
16 for /f %%a IN ('dir /b *.mp3') do rename %%~na.mp3 0%_number%-test.mp4
17 set /a _number +=1
18 goto F10TO100
19
20 :F100TO1000
21 IF NOT EXIST *.mp3. goto end
22 if %_number% gtr 999 goto end
23 for /f %%a IN ('dir /b *.mp3') do rename %%~na.mp3 %_number%-test.mp4
24 set /a _number +=1
25 goto F100TO1000
26
27 :end
28 for /f %%a IN ('dir /b *.mp4') do rename %%~na.mp4 %%~na.mp3
29 echo Done
该代码适用于表现良好的文件名(即没有空格、没有括号、长度不超过 8 个字符)。 但是,如果我有一个文件名行为不良的文件,脚本就会中断(它会无限循环,直到我使用 Ctrl-C 停止它)。
问题显然是文件名问题。 如何解决这个问题? 有任何想法吗? 我将非常感谢任何帮助。
I have several hundred *.mp3 files in c:\files. There are all imaginable filenames in there like
- milad.mp3 (good behaving)
- hey you.mp3 (space in filename)
- systemofadown.mp3 (long filename)
- howdy(1).mp3 (parentheses in filename)
and any combination of the last three conditions.
I want to rename the files to 001-test.mp3, 002-mp3, ...
It doesn't matter which file gets which name.
have written a batch file to do the rename. Here is my code (with line numbers added):
01 rem @echo off
02 cls
03 set _number=%1
04 lfnfor on
05
06 :F1TO10
07 IF NOT EXIST *.mp3. goto end
08 if %_number% gtr 9 goto F10TO100
09 for /f %%a IN ('dir /b *.mp3') do rename %%~na.mp3 00%_number%-test.mp4
10 set /a _number +=1
11 goto F1TO10
12
13 :F10TO100
14 IF NOT EXIST *.mp3. goto end
15 if %_number% gtr 99 goto F100TO1000
16 for /f %%a IN ('dir /b *.mp3') do rename %%~na.mp3 0%_number%-test.mp4
17 set /a _number +=1
18 goto F10TO100
19
20 :F100TO1000
21 IF NOT EXIST *.mp3. goto end
22 if %_number% gtr 999 goto end
23 for /f %%a IN ('dir /b *.mp3') do rename %%~na.mp3 %_number%-test.mp4
24 set /a _number +=1
25 goto F100TO1000
26
27 :end
28 for /f %%a IN ('dir /b *.mp4') do rename %%~na.mp4 %%~na.mp3
29 echo Done
This code works fine for good behaving filenames (i.e. no spaces, no parentheses, no longer than 8 chars long). But if I have even a single file with a bad behaving filename, the script breaks (it loops endlessly until I stop it with Ctrl-C).
The problem is obviously a filename issue. How can this be fixed? Any ideas? I'd greatly appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试使用
%%~fsa.mp3
而不是%%~na.mp3
。如 http://www.ss64.org/viewtopic.php?id 结尾处给出的=93
或在长名称中添加引号..
Try
%%~fsa.mp3
instead of%%~na.mp3
.as given at end in http://www.ss64.org/viewtopic.php?id=93
or add quotes to the longname..
用一个简单的 ruby 脚本替换它怎么样?
How about replacing it with a nice simple ruby script?
我真的认为你在这件事上走的是艰难的道路。 我经常进行此类操作,并使用 BRU 进行此类操作(不计其数)还有其他替代品) - 它体积小,便于携带,工作出色。
找不到为此浪费时间编写新批处理文件的充分理由。
I really think you're taking the hard route with this one. I do that kind of operations often, and use BRU for that kind of thing (zillion other alternatives are available) - it's small, portable, does its job terrifically.
Cannot see one good reason for wasting time with writing a new batch file for that.
根本问题是空格被用作单词分隔符,即“foo bar.txt”被认为是两个文件。
您需要以某种方式正确转义 cmd.exe 的参数,因此您需要检查文档...对您来说不幸的是,世界其他地方已经前进,并且更喜欢使用任意数量的替代 shell(哎呀,甚至 bash在 cygwin 上),或者按照 Chris 的建议,使用另一种语言。
Ruby、Python 和 Perl 都是不错的选择。
The fundamental issue is that a space is used as a word separator, ie, "foo bar.txt" is considered to be TWO files.
You need to somehow escape the arguments correctly for cmd.exe, so you need to check the documentation... Unfortunately for you, the rest of the world has moved on, and prefers to use any number of alternative shells (heck, even bash on cygwin), or as Chris suggested, to use another language instead.
Ruby, python, and perl are good choices.
如果您不介意使用 Windows 脚本文件,您可以非常轻松地执行此功能,并且很可能更容易阅读/更新。
只需另存为 renameMP3s.js 或您想要的任何.js 并双击即可。
if you don't mind using a windows script file, you can perform this functionality very easy, and quite possibly easier to read/update.
just save as renameMP3s.js or whatever.js you'd like and double-click.
Windows Vista 支持批量重命名。 只需选择资源管理器窗口中的所有项目,然后按 F2。 输入您想要的文件名(在本例中为“test”)并按 Enter 键。 嘭,大家都改名了。
这至少会让你正常化。 然后您可以编写一个脚本,在所有内容周围加上引号,例如“%1”、“%%~na.mp3”等,然后就完成了。
Windows Vista supports bulk rename. Just select all the items in an Explorer window, and hit F2. Enter the filename you want (in this case "test") and hit enter. Bam, everyone re-named.
This will at least normalize it for you. Then you can write a script that puts quotes around everything, e.g. "%1", "%%~na.mp3", etc, and you're done.
您是否尝试过在找到的文件名
周围加上引号?
重命名“%%~na.mp3”
而不是重命名%%~na.mp3
?Have you tried putting quotes around the filenames
for
found?rename "%%~na.mp3"
instead ofrename %%~na.mp3
?首先感谢热心解答的朋友们。 但没有一个是我正在寻找的。 实际上,我使用 Spire 提供的帮助得出了一个完美的工作脚本,该帮助作为对 这个问题。
这是最终的代码:(这次不需要行号)
First of all I would like to thank the friends who offered help through answers. But none was what I was looking for. Actually I arrived at a flawlessly working script using help provided by Spire posted as an answer to this question.
Here is the final code: (no need for line numbers this time)