# Websafe renaming

**URL:** <https://resource.dopus.com/t/websafe-renaming/9147>\
**Category:** Rename Presets\
**Created:** [January 28, 2010, 3:14pm UTC](https://resource.dopus.com/t/websafe-renaming/9147 "2010-01-28T15:14:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![SimonWpt](https://resource.dopus.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@SimonWpt](https://resource.dopus.com/u/SimonWpt)\
**Post date:** [January 28, 2010, 3:14pm UTC](https://resource.dopus.com/t/websafe-renaming/9147/1 "2010-01-28T15:14:47Z")

</div>

(See also: [Various simple rename presets](https://resource.dopus.com/t/various-simple-rename-presets/3297/1) -\> 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.

```auto
<?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](http://www.agentur-simon.de)

---

<div class="post-metadata">

**Author:** ![PeterPanino](https://resource.dopus.com/user_avatar/resource.dopus.com/peterpanino/32/12909_2.png) [@PeterPanino](https://resource.dopus.com/u/PeterPanino)\
**Post date:** [July 20, 2010, 1:44am UTC](https://resource.dopus.com/t/websafe-renaming/9147/2 "2010-07-20T01:44:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![SimonWpt](https://resource.dopus.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@SimonWpt](https://resource.dopus.com/u/SimonWpt)\
**Post date:** [July 20, 2010, 7:48am UTC](https://resource.dopus.com/t/websafe-renaming/9147/3 "2010-07-20T07:48:34Z")

</div>

> [@PeterPanino](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![dltrek](https://resource.dopus.com/letter_avatar_proxy/v4/letter/d/d26b3c/32.png) [@dltrek](https://resource.dopus.com/u/dltrek)\
**Post date:** [January 4, 2011, 8:30pm UTC](https://resource.dopus.com/t/websafe-renaming/9147/4 "2011-01-04T20:30:35Z")

</div>

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](https://resource.dopus.com/uploads/default/original/3X/8/6/86c14510aabc0aefffa54f2e0734b98f5c79dd48.zip) (2.55 KB)

---

<div class="post-metadata">

**Author:** ![zumaxb](https://resource.dopus.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@zumaxb](https://resource.dopus.com/u/zumaxb)\
**Post date:** [January 2, 2016, 10:38am UTC](https://resource.dopus.com/t/websafe-renaming/9147/5 "2016-01-02T10:38:55Z")

</div>

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](https://support.google.com/webmasters/answer/76329?hl=en)

> [@](#):
>
> We recommend that you use hyphens (-) instead of underscores (\_) in your URLs.

---

<div class="post-metadata">

**Author:** ![pctechtv](https://resource.dopus.com/user_avatar/resource.dopus.com/pctechtv/32/33246_2.png) [@pctechtv](https://resource.dopus.com/u/pctechtv)\
**Post date:** [February 28, 2022, 7:05pm UTC](https://resource.dopus.com/t/websafe-renaming/9147/6 "2022-02-28T19:05:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mosed](https://resource.dopus.com/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@Mosed](https://resource.dopus.com/u/Mosed)\
**Post date:** [March 2, 2022, 8:29pm UTC](https://resource.dopus.com/t/websafe-renaming/9147/8 "2022-03-02T20:29:16Z")

</div>

> [@pctechtv](#):
>
> 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. 🙂

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 `_`.

```javascript
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... 🙂

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

```javascript
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;
}

```

---

<div class="post-metadata">

**Author:** ![pctechtv](https://resource.dopus.com/user_avatar/resource.dopus.com/pctechtv/32/33246_2.png) [@pctechtv](https://resource.dopus.com/u/pctechtv)\
**Post date:** [March 2, 2022, 8:43pm UTC](https://resource.dopus.com/t/websafe-renaming/9147/9 "2022-03-02T20:43:07Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Mosed](https://resource.dopus.com/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@Mosed](https://resource.dopus.com/u/Mosed)\
**Post date:** [March 3, 2022, 4:37pm UTC](https://resource.dopus.com/t/websafe-renaming/9147/10 "2022-03-03T16:37:38Z")

</div>

> [@PeterPanino](#):
>
> 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.

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

> **[Percent-encoding](https://en.wikipedia.org/wiki/Percent-encoding)**
>
> URL encoding, officially known as percent-encoding, is a method to encode arbitrary data in a Uniform Resource Identifier (URI) using only the US-ASCII characters legal within a URI. Although it is known as URL encoding, it is also used more generally within the main Uniform Resource Identifier (URI) set, which includes both Uniform Resource Locator (URL) and Uniform Resource Name (URN). As such, it is also used in the preparation of data of the application/x-www-form-urlencoded media type, as is...

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 `_`.
