JScript to Rename Files in a Folder
So a guy I work with needed to rename a lot of files in a folder to have the extension be upper case. I don’t know why. I don’t really care. I found a VBScript that I could modify to do what I wanted, but since I like JScript better (real error handling is the main reason I use it instead) I decided to convert it. The following is the fruit of my labor:
// BatchFileRename.js - Renames files in a specified folder
// Author - Jace Gregg
// Version 1.0 - 03 December 2008
// -----------------------------------------------------------------
var cTITLE = "Dawson Scripting: Batch File Rename";
try {
var objWMIService = GetObject("winmgmts:\\\\.\\root\\CIMV2");
var filesRenamed = 0;
var colFileList = objWMIService.ExecQuery("ASSOCIATORS OF {Win32_Directory.Name='C:\\temp'} Where ResultClass = CIM_DataFile");
var enumItems = new Enumerator(colFileList);
for (; !enumItems.atEnd(); enumItems.moveNext()) {
var objFile = enumItems.item();
var strNewName = objFile.Drive + objFile.Path + objFile.FileName + "." + objFile.Extension.toUpperCase();
errResult = objFile.Rename(strNewName);
filesRenamed ++;
}
msgbox("Renamed " + filesRenamed + " files.");
}
catch(e) {
msgbox("Error " + e.number + "\n\n" + e.description + "\nCould not rename all files.");
}
function msgbox(message, title, duration) {
if(title == null) {
title = cTITLE;
}
if(duration == null) {
duration = 10;
}
var objWSH = WScript.CreateObject("Wscript.Shell");
objWSH.Popup (message, duration, title);
}
function ParseEnvironStrings(strInput) {
var WshShell = WScript.CreateObject("WScript.Shell");
return WshShell.ExpandEnvironmentStrings(strInput);
}
This code will rename the extensions of all the files in c:\temp to the uppercase version of themselves, but the code could easily be changed to include a year before a date or you could add some regular expressions to fix a lot of problems in your pr0n folders. Up to you.