Batch rename issue when dealing with special characters in the filename

被刻印的时光 ゝ 提交于 2019-12-06 07:27:50
lakshmanaraj

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

How about replacing it with a nice simple ruby script?

Dir["*.mp3"].each_with_index{ |filename,index|
  File.rename filename, "Test-#{index}.mp3"
}

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.

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.

if you don't mind using a windows script file, you can perform this functionality very easy, and quite possibly easier to read/update.

var path = "c:\\files";
var io = new ActiveXObject("Scripting.FileSystemObject");

Enumerator.prototype.each = function(f, c) {
  for (var i = 0; !this.atEnd(); this.moveNext()) {
    if (f.call(c, this.item(), i++, this)===false) break;
  }
  return this;
};

function fixedLength(n) {
  return (n < 100 ? "0" : "") + (n < 10 ? "0" : "") + n;
};

var results = [];
new Enumerator(io.GetFolder(path).Files).each(function(file, index) {
  var oldName = file.Name, newName = fixedLength(index+1) + ".mp3";
  if (/\.mp3$/i.test(oldName)) {
    // mp3 extension
    file.Name = newName;
    results.push(newName + " <-- " + oldName);
  }
});

WScript.Echo(results.join("\n"));

just save as renameMP3s.js or whatever.js you'd like and double-click.

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.

Have you tried putting quotes around the filenames for found?

rename "%%~na.mp3" instead of rename %%~na.mp3?

Majid Fouladpour

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)

rem @echo off
cls
set _number=%1

:F1TO10
IF NOT EXIST *.mp3. goto end
if %_number% gtr 9 goto F10TO100
for /f "usebackq delims=" %%x in (`dir /b *.mp3`) do rename %%~nsx.mp3 00%_number%-test.mp9
set /a _number +=1
goto F1TO10

:F10TO100
IF NOT EXIST *.mp3. goto end
if %_number% gtr 99 goto F100TO1000
for /f "usebackq delims=" %%x in (`dir /b *.mp3`) do rename %%~nsx.mp3 0%_number%-test.mp9
set /a _number +=1
goto F10TO100

:F100TO1000
IF NOT EXIST *.mp3. goto end
if %_number% gtr 999 goto end
for /f "usebackq delims=" %%x in (`dir /b *.mp3`) do rename %%~nsx.mp3 %_number%-test.mp9
set /a _number +=1
goto F100TO1000

:end
for /f %%a IN ('dir /b *.mp9') do rename %%~na.mp9 %%~na.mp3
echo Done.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!