
// This first line simply creates a variable where we will, later on, store the names
// of the files whose names we change. We do this so that at the end of the script we 
// can say, hey, we changed these names.
var filesRenamed = "We changed the names of these files: ";


// This next line causes a dialog box to open on screen and asks the
// user to choose a folder. Every file in the folder will be effected
// by this script. 
var selectedFolder = Folder.selectDialog("Select the folder to run this script on. All files in the folder will be affected.");

// This next line makes sure that we really do have a reference to a folder. 
// The user might cancel the operation at this point, and if so, selectedFolder will
// be empty, in which case there is no point going on with the script. 
if (selectedFolder instanceof Folder) {

	// what should be removed from the file name? 
	// If you have a file name like "P42_WTTI98_Ochre_Settings.psd" and 
	// you want to get rid of "_Settings" then in this dialog box, when
	// it opens on screen, you'd type "_Settings".
	var stringToBeRemoved = prompt("What portion of the file name do you wan to remove (example: '_p52' to remove '_P52' from the file name)", "Type the letters or numbers you'd like removed from file names.");
	
	// This next line checks to see that the user actually typed something in. If they
	// didn't then there is no point proceeding. 
	if (stringToBeRemoved != "") {

		// The variable selectedFolder is now a reference pointing at the folder we want to effect.
		// The getFiles command returns a list of every file in the folder. 
		var allFilesInFolder = selectedFolder.getFiles();

		alert("This folder contains this many files: " + allFilesInFolder.length);

		// This next line is a special line that causes the script to loop through the list
		// of files one at a time. All the lines below are then done to each file, one at a time.
		for (var i=0; i < allFilesInFolder.length; i++) {
		
			// This next lines gets a reference to one particular file from the list. 
			// Each time we go through the loop, the next file in the list will be choosen.
			var thisFile = allFilesInFolder[i];
			
			// This next lines gets the name of the file and stores the name in the 
			// variable called oldName.
			var oldName = thisFile.name; 
				
			// It's important that you don't erase the entire filename. The file might 
			// disappear if you erase the entire name. This next line makes sure that
			// you're not erasing the whole file name. This next line basically says
			// "Proceed only if the letters and numbers to be removed don't equal the 
			// whole file name." 
			if (stringToBeRemoved != oldName) {
				
				// The replace command, down below, uses a special variable called 
				// a "regular expression". This is created through the special command
				// RegExp. The next line basically formats the stringToBeRemoved in a way
				// that allows the replace command to understand it. 
				regEx = new RegExp(stringToBeRemoved, "g");
				
				// This next line takes whatever value is in stringToBeRemoved and replaces
				// it with nothing. Thus the string is removed from the name. However, we 
				// have not yet changed the file name, we've just created a variable called
				// newName that has stored the name we'd like the file to have.
				var newName = oldName.replace(regEx,"");
			
				// This next line is the one that actually changes the file name. We use
				// the rename command. 
				thisFile.rename(newName);
				
				// This next line checks what we just done. File name should now equal the
				// new name. If it doesn't, then something went wrong. 
				if (thisFile.name != newName) {
					alert("Unable to rename " + thisFile.absoluteURI + "->" + newName);
				} else {
					filesRenamed += newName + ", "; 
				}
			} else {
				alert("Sorry, but what you typed is the same as the name of the file. You'd erase the whole file name! Very bad!");
			}
		}
	} else {
		alert("Sorry, but you have to type in something for us to remove it from the filenames.");
	}
} else {
	alert("Sorry, but you didn't select a folder."); 
}

alert("The script is done now." + filesRenamed); 
