Eu escrevi este script em lote e o coloquei no meu Gist Repo . Requer acesso ao Windows Scripting Host. Aqui está o arquivo:
@set @junk=1 /*
@echo off
cscript //nologo //E:jscript %0 %*
goto :eof
*/
var args=WScript.Arguments, shell=WScript.CreateObject("WScript.Shell"), bForced=false, nStartIndex=-1;
if(args.length==0||(args.length==1&&(bForced=args(0).toLowerCase()=='-f'||args(0).toLowerCase()=='--force'))) {
showHelp();
WScript.Echo("\nERROR: You must provide a starting value to begin counting at.");
WScript.Quit(1);
}
if(args(0)=='-?'||args(0).toLowerCase()=='--help') {
showHelp();
WScript.Quit(0);
}
if(isNaN(nStartIndex=parseInt(args(bForced?1:0)))||nStartIndex<0) {
showHelp();
WScript.Echo(sprintf("\nERROR: The value [%s] which was given for start index is invalid. It should be a positive integer.", nStartIndex));
WScript.Quit(2);
}
var fso=new ActiveXObject("Scripting.FileSystemObject");
var folder=fso.GetFolder(shell.CurrentDirectory);
var enFiles=new Enumerator(folder.Files);
var oFile, file_path, file_ext;
if(!bForced) {
for(var nCurDX=nStartIndex; !enFiles.atEnd(); enFiles.moveNext(), nCurDX++) {
oFile=enFiles.item();
file_path=oFile.Name;
file_ext=getFileExt(file_path);
WScript.Echo(sprintf("Rename %s to %s", file_path, nCurDX+file_ext));
}
WScript.Echo("Type 'yes' to continue...");
if(WScript.StdIn.ReadLine().toLowerCase()!="yes") {
WScript.Echo("\nAction cancelled by user.");
WScript.Quit(-1);
}
enFiles=new Enumerator(folder.Files);
}
for(var nCurDX=nStartIndex; !enFiles.atEnd(); enFiles.moveNext(), nCurDX++) {
oFile=enFiles.item();
file_path=oFile.Name;
file_ext=getFileExt(file_path);
try {
oFile.Name = nCurDX+file_ext;
} catch(e) {
WScript.Echo(sprintf("\nERROR: Cannot rename file:\n\t%s\n\nReason: %s\n", file_path, e.description));
WScript.Quit(3);
}
}
/////////////////////////////////
/////////// FUNCTIONS ///////////
/////////////////////////////////
function sprintf(format, etc) { var arg=arguments, i=1; return format.replace(/%((%)|s)/g, function (m) { return m[2] || arg[i++] }); }
function showHelp() {
WScript.Echo("------------------");
WScript.Echo("Numeric Rename All");
WScript.Echo("------------------");
WScript.Echo("");
WScript.Echo("This file will change the file title of all files is a directory");
WScript.Echo("so that they are all just an incremented number with the original");
WScript.Echo("extension still in place.");
WScript.Echo("");
WScript.Echo("Usage: NUMERIC_RENAME_ALL [ -? | --help ] [ -f | --force ] <start-index>");
WScript.Echo("");
WScript.Echo("\t-?, --help\tDisplay this help screen and exit.");
WScript.Echo("");
WScript.Echo("\t-f, --force\tRename the files without prompting first.");
WScript.Echo("");
WScript.Echo("\t<start-index>\tThe value to begin counting at.");
WScript.Echo("");
WScript.Echo("This script takes one parameter and that is the number to begin at.");
WScript.Echo("For example (if called with 12345 as the parameter):");
WScript.Echo("");
WScript.Echo("\ttest1.jpg becomes 12345.jpg");
WScript.Echo("\ttest1.jpg.txt becomes 12346.jpg.txt");
WScript.Echo("\ttest2.pdf becomes 12347.jpg");
WScript.Echo("");
WScript.Echo("Currently, there is no way to sort the items and they will be");
WScript.Echo("numbered according to the default file order for your system.");
WScript.Echo("");
WScript.Echo("----------------------");
}
function getFileExt(file_path) {
var dx=file_path.lastIndexOf("\"), ret;
if(dx==-1) dx=file_path.lastIndexOf("/");
ret=file_path.substring(dx+1);
return (dx=ret.indexOf("."))==-1?"":("."+ret.substring(dx+1));
}
Salve-o como NUMERIC_RENAME_ALL.BAT
em um local de fácil acesso.
Fazer isso em um arquivo de lote simples seria difícil porque o cmd.exe falha ao definir a variável dentro de loops. É por isso que estamos usando um arquivo Batch \ WSH Hybrid para fazer isso (isso é declarado nas primeiras 5 linhas do script). Há provavelmente uma maneira bastante fácil de fazer isso é poder shell também.