Skip to content

Instantly share code, notes, and snippets.

@krowe krowe/numeric_rename.bat
Last active Oct 24, 2018

Embed
What would you like to do?
This file will change the file title of all files is a directory so that they are all just an incremented number with the original extension still in place.
@set @junk=1 /*
@cscript //nologo //E:jscript %~f0 %*
@goto :eof */
var args=WScript.Arguments, shell=WScript.CreateObject("WScript.Shell"), bForced, nStartIndex, sFilter;
if(args.length==0||(args.length==1&&(bForced=args(0).toLowerCase()=='-f'||args(0).toLowerCase()=='--force')))
err(1, "You must provide a starting value to begin counting at.");
if(args(0)=='-?'||args(0).toLowerCase()=='--help') { showHelp(); WScript.Quit(0); }
if(isNaN(nStartIndex=parseInt(args(bForced?1:0)))||nStartIndex<0)
err(2, sprintf("The value [%s] which was given for start index is invalid. It should\n\tbe a positive integer.", nStartIndex));
sFilter=(args.length==(bForced?3:2))?args(bForced?2:1):".*";
var fso=new ActiveXObject("Scripting.FileSystemObject"), folder=fso.GetFolder(shell.CurrentDirectory),
enFiles=new Enumerator(folder.Files), oFile, file_path, file_ext;
if(!bForced) {
if(sFilter!=".*") err(255, sFilter, "FILTER", false);
for(var nCurDX=nStartIndex; !enFiles.atEnd(); enFiles.moveNext(), nCurDX++) {
oFile=enFiles.item();
file_path=oFile.Name;
if(file_path.search(sFilter)==-1) {
err(255, file_path, " SKIP", false);
continue;
}
file_ext=getFileExt(file_path);
err(255, sprintf("%s\n TO:\t%s", file_path, nCurDX+file_ext), "RENAME", false);
}
WScript.Echo("\nType 'yes' to continue...");
if(WScript.StdIn.ReadLine().toLowerCase()!="yes")
err(-1, "Action cancelled by user.", "INFO", false);
enFiles=new Enumerator(folder.Files);
}
for(var nCurDX=nStartIndex; !enFiles.atEnd(); enFiles.moveNext(), nCurDX++) {
oFile=enFiles.item();
file_path=oFile.Name;
if(file_path.search(sFilter)==-1) continue;
file_ext=getFileExt(file_path);
try {
oFile.Name = nCurDX+file_ext;
} catch(e) {
err(3, sprintf("Cannot rename file:\n\t%s\n\nReason: %s\n", file_path, e.description));
}
}
/////////// FUNCTIONS ///////////
function sprintf(format, etc) { var arg=arguments,i=1; return format.replace(/%((%)|s)/g, function(m){return m[2]||arg[i++]}); }
function err(exitCode, msg, type, help) {
if(typeof(help)==='undefined') help=true;
if(typeof(type)==='undefined') type="ERROR";
if(help) showHelp();
WScript.Echo("\n"+type+":\t"+msg);
if(exitCode!=255) WScript.Quit(exitCode);
}
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));
}
function showHelp() {
l ="\n--------------",str=l;
str+="\nNumeric Rename"+l+"\n\n";
str+="This file will change the file title of all files is a directory so that they\n";
str+="are named an incremented number with the original extension still in place.\n\n";
str+="Usage: NUMERIC_RENAME_ALL [ -? | --help ] [ -f | --force ] <start-index> [ <filter> ]\n\n";
str+="\t-?, --help\tDisplay this help screen and exit.\n\n";
str+="\t-f, --force\tRename the files without prompting first.\n\n";
str+="\t<filter>\tA regular expression which the file name must match\n\t\t\tto be processed.\n\n";
str+="\t<start-index>\tThe value to begin counting at.\n\n";
str+="For example (if called with 12345 as start-index):\n\n";
str+="\ttest1.jpg\t\tis renamed to\t\t12345.jpg\n";
str+="\ttest1.jpg.txt\t\tis renamed to\t\t12346.jpg.txt\n";
str+="\ttest2.pdf\t\tis renamed to\t\t12347.jpg\n\n";
str+="Currently, there is no way to sort the items and they will be numbered\n";
str+="according to the default file order for your system.\n"+l;
WScript.Echo(str);
}
@krowe

This comment has been minimized.

Copy link
Owner Author

commented Dec 27, 2014

I've fixed this script so that it can be called without the .bat extension. To do this in your own scripts replace cscript //nologo //E:jscript %0 %* with cscript //nologo //E:jscript %~f0 %*.

@Programmer786

This comment has been minimized.

Copy link

commented Oct 24, 2018

sir how can use of this file because it is not working in my destination folder.sir please help me fast

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.