Websafe renaming

(See also: Various simple rename presets -> Make Safe Name)

Hi,

i am a webdeveloper and get often files from my clients to link on websites. The filename has often special characters , which are not websafe like: äöüßáàâ...

The following button uses a vbscript to convert the selected filenames to a websafe format (a-z0-9-_). I hope, you like it.

<?xml version="1.0"?>
<button backcol="none" display="both" textcol="none">
	<label>Secure Rename</label>
	<tip>Websafe rename of selected files</tip>
	<icon1>#format</icon1>
	<function type="normal">
		<instruction>@nodeselect </instruction>
		<instruction>RENAME TO &quot;*&quot; </instruction>
		<instruction />
		<instruction>@script vbscript </instruction>
		<instruction>Option Explicit </instruction>
		<instruction />
		<instruction>Function Rename_GetNewName(strFileName, strFilePath, fIsFolder, strOldName, strNewName) </instruction>
		<instruction />
		<instruction>Dim strAllowed</instruction>
		<instruction>Dim tmpFile</instruction>
		<instruction>Dim iLen</instruction>
		<instruction>Dim i</instruction>
		<instruction>Dim strChar</instruction>
		<instruction />
		<instruction>strAllowed = &quot;abcdefghijklmnopqrstuvwxyz0123456789.&quot;</instruction>
		<instruction>strNewName = Trim(LCase(strNewName))</instruction>
		<instruction />
		<instruction>strNewName = Replace(strNewName, &quot;ä&quot;, &quot;ae&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;á&quot;, &quot;a&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;à&quot;, &quot;a&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;â&quot;, &quot;a&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;é&quot;, &quot;e&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;è&quot;, &quot;e&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;ê&quot;, &quot;e&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;ö&quot;, &quot;oe&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;ó&quot;, &quot;o&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;ò&quot;, &quot;o&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;ô&quot;, &quot;o&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;í&quot;, &quot;i&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;ì&quot;, &quot;i&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;î&quot;, &quot;i&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;ü&quot;, &quot;ue&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;ú&quot;, &quot;u&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;ù&quot;, &quot;u&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;û&quot;, &quot;u&quot;)</instruction>
		<instruction>strNewName = Replace(strNewName, &quot;ß&quot;, &quot;ss&quot;)</instruction>
		<instruction />
		<instruction />
		<instruction>iLen = len(strNewName)</instruction>
		<instruction />
		<instruction>for i = 1 to iLen</instruction>
		<instruction>    strChar = mid(strNewName, i, 1)</instruction>
		<instruction>    if instr(strAllowed, strChar) &gt; 0 then </instruction>
		<instruction>        tmpFile = tmpFile &amp; strChar          </instruction>
		<instruction>    elseif right(tmpFile, 1) &lt;&gt; &quot;_&quot; then</instruction>
		<instruction>        tmpFile = tmpFile &amp; &quot;_&quot;</instruction>
		<instruction>    end if</instruction>
		<instruction>next</instruction>
		<instruction />
		<instruction>if left(tmpFile, 1) = &quot;_&quot; then</instruction>
		<instruction>    tmpFile = right(tmpFile, len(tmpFile) - 1)</instruction>
		<instruction>end if</instruction>
		<instruction />
		<instruction>strNewName = Replace(tmpFile, &quot;_.&quot;, &quot;.&quot;)</instruction>
		<instruction>End Function</instruction>
	</function>
</button>

Feel free to change the script for your purpose.

Bertram Simon

agentur-simon.de

Hi Bertram, thank you for this useful button!

I have changed it for me to include also ",-()" (of course without quotes) into strAllowed since all web-servers support this whithout using URL-encoding. What do you think?

Hi Peter,

I use this script for web- and cd-rom-safe renaming. if you use it only for web renaming, you can enhance it with more characters. feel free to post your version!

Greetings from germany

Bertram

With the help of Jonathan Potter of Directory Opus support, who I want to thank for his help, I have a combination of websafe and titlecase renaming.
See attached file.

Leo Lesage
Websafe and titlecase.zip (2.55 KB)

Allow me to pick up in this to discuss underscore -vs- hyphen.

Should this script transpose to hyphen only, for websafe file names if used on websites, for SEO-reasons? Because - as I understand it - underscore renders one word while hyphen renders a word separation.

See Google Webmaster Resources: https://support.google.com/webmasters/answer/76329?hl=en

This is perfect except for the period. How could I rename so that I replace the period in names except for the last one that is used for the file extension? Thanks

You would have to add that the extension is ignored. No idea how to do this in VB script.

I wrote now an adapted script for me in Jscript.
My version replaces all unsafe characters with a hyphen. Multiple hyphen are changed to a single one and a hyphen/underscore at the end or beginning of the file name is removed.
Perhaps you can use this as basis to adapt it to your needs.
e.g. replace the hypen with underscore or change the allowed characters.

According to my testing it works like I want it, but the button is fresh coded. :slight_smile:

EDIT: I recognized that the defined replacement (e.g. ä to ae) was only done once. I updated the scripts so that it is replaced global and the script uses now regular expressions instead of the loop to replace the unallowed characters. Underscore is allowed and if there is a -_ or _- in the name it will be changed to _.

function OnClick(clickData) {
	var cmd = clickData.func.command;
	var tab = clickData.func.sourcetab;
	cmd.deselect = false;
	
	DOpus.ClearOutput();
				
		for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
			var item = e.item();
			var	str = item.name_stem;
			
			str = str.replace(/^\s+|\s+$/gm,''); // removes spaces at the beginning and end of the file name						
			str = str.toLowerCase(); // change every character to lower case

			//replace special characters with defined alternatives
			str = str.replace(/ä/g, "ae");
			str = str.replace(/á/g, "a");
			str = str.replace(/à/g, "a");
			str = str.replace(/â/g, "a");
			str = str.replace(/é/g, "e");
			str = str.replace(/è/g, "e");
			str = str.replace(/ê/g, "e");
			str = str.replace(/ö/g, "oe");
			str = str.replace(/ó/g, "o");
			str = str.replace(/ò/g, "o");
			str = str.replace(/ô/g, "o");
			str = str.replace(/í/g, "i");
			str = str.replace(/ì/g, "i");
			str = str.replace(/î/g, "i");
			str = str.replace(/ü/g, "ue");
			str = str.replace(/ú/g, "u");
			str = str.replace(/ù/g, "u");
			str = str.replace(/û/g, "u");
			str = str.replace(/ß/g, "ss");
						
			// replace unsafe characters with "-"
			str = str.replace(/[^0-9a-zA-Z._-~]/g, '-') //remove the underscore if it shall not be allowed
				
			// replace multiple "-" with one "-"
			str = str.replace(/-{2,}/g, '-') 

			//remove "-" if it is next to an underscore. Only relevant if underscore is allowed.
			str = str.replace(/-_/g, '_');
			str = str.replace(/_-/g, '_');

			//remove "-" and "_" at the beginning if existing
			if (str.charAt(0) === '-' || '_') {
				str = str.substr(1, str.length - 1);	
			}
		
			//remove "-" and "_" at the end if existing
			if (str.charAt(str.length - 1) === '-' || '_') {
				str = str.substr(0, str.length - 1);	
			}
			
			cmd.RunCommand('Rename FROM="'+ item +'" TO="' + str + '" IGNOREEXT');
		}

}

PS: Original comment is quite old, but you changed your comment recently. So perhaps still interesting... :slight_smile:

EDIT: Here the version for the rename dialog script area:

function OnGetNewName(getNewNameData) {
	var item = getNewNameData.item;
	var str = item.name_stem;
	str = str.replace(/^\s+|\s+$/gm,''); // removes spaces at the beginning and end of the file name						
	str = str.toLowerCase(); // change every character to lower case

	//replace special characters with defined alternatives
	str = str.replace(/ä/g, "ae");
	str = str.replace(/á/g, "a");
	str = str.replace(/à/g, "a");
	str = str.replace(/â/g, "a");
	str = str.replace(/é/g, "e");
	str = str.replace(/è/g, "e");
	str = str.replace(/ê/g, "e");
	str = str.replace(/ö/g, "oe");
	str = str.replace(/ó/g, "o");
	str = str.replace(/ò/g, "o");
	str = str.replace(/ô/g, "o");
	str = str.replace(/í/g, "i");
	str = str.replace(/ì/g, "i");
	str = str.replace(/î/g, "i");
	str = str.replace(/ü/g, "ue");
	str = str.replace(/ú/g, "u");
	str = str.replace(/ù/g, "u");
	str = str.replace(/û/g, "u");
	str = str.replace(/ß/g, "ss");
				
	// replace unsafe characters with "-"
	str = str.replace(/[^0-9a-zA-Z._-~]/g, '-') //remove the underscore if it shall not be allowed
		
	// replace multiple "-" with one "-"
	str = str.replace(/-{2,}/g, '-') 

	//remove "-" if it is next to an underscore. Only relevant if underscore is allowed.
	str = str.replace(/-_/g, '_');
	str = str.replace(/_-/g, '_');

	//remove "-" and "_" at the beginning if existing
	if (str.charAt(0) === '-' || '_') {
		str = str.substr(1, str.length - 1);	
	}

	//remove "-" and "_" at the end if existing
	if (str.charAt(str.length - 1) === '-' || '_') {
		str = str.substr(0, str.length - 1);	
	}
	
	return str + item.ext;
}

Hey, thanks for the reply. This is great. It is also super that you rewrote it in JScript. I fully understand and can extend it now. Thanks so much!

This characters are reserved for special purposes in the URL. So you should not use it in names.
But "~" is allowed.

I changed my script accordingly.

@pctechtv Perhaps you recognized it by yourself: I recognized that the defined replacement (e.g. ä to ae) was only done once. I updated the script so that it is replaced global and the script uses now regular expressions instead of the loop to replace the unallowed characters. Underscore is allowed and if there is a -_ or _- in the name it will be changed to _.