Remove percent encoding from file names

From TheAlmightyGuru
Jump to: navigation, search

Remove percent encoding from file names is a JScript program that removes any instances of percent encoding from the file names in the current folder. Files downloaded from certain web servers will sometimes encode file names with percent encoding to make them more friendly to unknown operating systems. For example, if you downloaded a file with this illegible name:

Math%2C%20Vol%2E%201%2%3B1%20%2B%201%20%3D%202%20%26%20Other%20Equations.pdf

Running this script would rename it to:

Math, Vol. 1; 1 + 1 = 2 & Other Equations.pdf

To use the program, save the following code into a file and give it a .js extension. Then, move the file into any folder where you want to remove percent encoding from file names and double-click it. You can also modify the replacement substitutions if you prefer something different from what I used.

Source

// Copyright 2024, Dean Tersigni.
// This program removes any instances of percent encoding from the file names in the current folder.

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

var aReplace = new Array()
// Non-printables
aReplace["%00"] = "_";
aReplace["%01"] = "_";
aReplace["%02"] = "_";
aReplace["%03"] = "_";
aReplace["%04"] = "_";
aReplace["%05"] = "_";
aReplace["%06"] = "_";
aReplace["%07"] = "_";
aReplace["%08"] = "_";
aReplace["%09"] = "_";
aReplace["%0A"] = "_";
aReplace["%0B"] = "_";
aReplace["%0C"] = "_";
aReplace["%0D"] = "_";
aReplace["%0E"] = "_";
aReplace["%0F"] = "_";
aReplace["%10"] = "_";
aReplace["%11"] = "_";
aReplace["%12"] = "_";
aReplace["%13"] = "_";
aReplace["%14"] = "_";
aReplace["%15"] = "_";
aReplace["%16"] = "_";
aReplace["%17"] = "_";
aReplace["%18"] = "_";
aReplace["%19"] = "_";
aReplace["%1A"] = "_";
aReplace["%1B"] = "_";
aReplace["%1C"] = "_";
aReplace["%1D"] = "_";
aReplace["%1E"] = "_";
aReplace["%1F"] = "_";

// Printables
aReplace["%20"] = " ";
aReplace["%21"] = "!";
aReplace["%22"] = "";		// Windows can't have a double-quote (") in a file name. Replace it with two single quotes.
aReplace["%23"] = "#";
aReplace["%24"] = "$";
aReplace["%25"] = "%";
aReplace["%26"] = "&";
aReplace["%27"] = "'";
aReplace["%28"] = "(";
aReplace["%29"] = ")";
aReplace["%2A"] = "_";		// Windows can't have an asterisk (*) in a file name. Replace it with an underscore.
aReplace["%2B"] = "+";
aReplace["%2C"] = ",";
aReplace["%2D"] = "-";
aReplace["%2E"] = ".";
aReplace["%2F"] = "_";		// Windows can't have an slash (/) in a file name. Replace it with an underscore.
aReplace["%3A"] = "-";		// Windows can't have an colon (:) in a file name. Replace it with a hyphen.
aReplace["%3B"] = ";";
aReplace["%3C"] = "(";		// Windows can't have a less-than sign (<) in a file name. Replace it with an right parentheses.
aReplace["%3D"] = "=";
aReplace["%3E"] = ")";		// Windows can't have a greater-than sign (>) in a file name. Replace it with a left parentheses.
aReplace["%3F"] = "_";		// Windows can't have an question mark (?) in a file name. Replace it with an underscore.
aReplace["%40"] = "@";
aReplace["%5B"] = "[";
aReplace["%5C"] = "_";		// Windows can't have an backslash (\) in a file name. Replace it with an underscore.
aReplace["%5D"] = "]";
aReplace["%5E"] = "^";
aReplace["%5F"] = "_";
aReplace["%56"] = "`";
aReplace["%7B"] = "{";
aReplace["%7C"] = "_";		// Windows can't have a vertical line (|) in a file name. Replace it with an underscore.
aReplace["%7D"] = "}";
aReplace["%7E"] = "~";
aReplace["%7F"] = "_";		// Non-printable delete character.


var sFolder = oWSH.CurrentDirectory;					// Get the current folder.
var oFolder = oFSO.GetFolder( sFolder );				// Create an object reference to the current folder.
var eFiles = new Enumerator( oFolder.files );			// Create an enumerator on the files in the current folder.

var sFilePath;
for ( ; !eFiles.atEnd(); eFiles.moveNext() ) {			// Loop through the files.
	sFilePath = eFiles.item();							// Get the full file path of the current file.
	sPath = oFSO.GetParentFolderName(sFilePath)			// Get just the path of the current file.
	sFileName = oFSO.GetFileName(sFilePath)				// Get just the file name of the current file.

	var sNewFileName = ""
	for (var charNo = 0; charNo < sFileName.length; charNo++) {			// Loop through the file name.
		var check = sFileName.substring(charNo, charNo + 3);			// Get the next 3-character segment.

		if (aReplace[check.toUpperCase()] != null) {					// Check if the 3-character segment is in the aReplace array
			sNewFileName += aReplace[check.toUpperCase()];				// This is a percent-encoded character, replace the percent encoding with the proper value.
			charNo += 2;												// Move past the percent-encoded characters.
		} else {
			sNewFileName += check.substring(0, 1);						// This is not a percent-encoded character, add it to the new file name.
		}
	}
	
	oFSO.MoveFile(sFilePath, sPath + "\\" + sNewFileName)				// Rename the file.
}

oWSH.Popup("Complete");