Random renamer

From TheAlmightyGuru
Revision as of 21:31, 7 September 2018 by TheAlmightyGuru (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Random renamer is a JScript program that renames all the files in the current directory to a random 8-digit number if they meet the specified criteria. This is useful if you want to make your files 8.3 compliant and don't care about retaining the file's existing identity. The example is set to only rename JPG files, but it can be easily altered to work with other file types.

Source

// Copyright 2014, Dean Tersigni.
// This program renames all the *.jpg files in the current directory with a random 8-digit number.

var oFSO = new ActiveXObject( "Scripting.FileSystemObject" );
var oWSH = new ActiveXObject("WScript.Shell");

var sFolder = oWSH.CurrentDirectory;			// Get the current folder.
var oFolder = oFSO.GetFolder( sFolder );		// Create an object reference to the current folder.
var oFiles = oFolder.files;				// Get a file object based on all the files.

var eFiles = new Enumerator( oFiles );			// Create an enumerator on the file object.

var sFilePath;
for(; !eFiles.atEnd(); eFiles.moveNext() ) {		// Loop through the files.
	oFile = eFiles.item();				// Get the path of the current file.
	
	if( oFile.Type == "JPEG image" ) {		// Only process JPEG images.
		sNewFile = Random(8) + ".jpg"
		
		// Rename the file if a new name has been generated.
		oFile.Copy( sNewFile, false );
		
		// Delete the old file.
		oFile.Delete( );
	}	
}

// Returns a string of random numbers the length specified.
function Random( nLength ) {
	var sString = "";
	var nDigit = 0;
	var i = 0;
	
	for(i = 0; i < nLength; i++ ) {
		nDigit = Math.floor( Math.random () * 10 );
		sString = sString + nDigit;
	}
	
	return(sString);
}