<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>mitch&#039;s meanderings&#187; VBScript</title>
	<atom:link href="http://mitchcontla.com/tag/vbscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://mitchcontla.com</link>
	<description>life, technology, running; links, photos, and videos</description>
	<lastBuildDate>Tue, 18 Oct 2011 19:32:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Sanitize File Names Using VBScript</title>
		<link>http://mitchcontla.com/2006/05/18/sanitize-file-names-using-vbscript/</link>
		<comments>http://mitchcontla.com/2006/05/18/sanitize-file-names-using-vbscript/#comments</comments>
		<pubDate>Fri, 19 May 2006 02:46:48 +0000</pubDate>
		<dc:creator>mitch</dc:creator>
				<category><![CDATA[Geekery]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[regular-expressions]]></category>

		<guid isPermaLink="false">http://mitch.contla.net/2006/05/18/sanitize-file-names-using-vbscript/</guid>
		<description><![CDATA[We use an ASP based form to enable our customers to submit files created as part of a registration process. During the submission, the file is sent as an email attachment to our support staff, and a copy is written to a file server as a backup. The application uses a standard Win32 Save dialog [...]]]></description>
			<content:encoded><![CDATA[<p>We use an <acronym title="Active Server Pages">ASP</acronym> based form to enable our customers to submit files created as part of a registration process. During the submission, the file is sent as an email attachment to our support staff, and a copy is written to a file server as a backup. The application uses a standard <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/windows_api_start_page.asp">Win32</a> Save dialog that suggests a default filename. In nearly every case, the file is submitted by the customer without changing the default name. If the email process fails, retrieving the backup copy is difficult at best.</p>

<p>I modified the form to create a unique filename using values from certain form elements. Since these values can, and often do, use characters that are invalid as part of a filename, I needed a way to &#8220;sanitize&#8221; the name during the submission process. I wrote the following <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/0a8270d7-7d8f-4368-b2a7-065acb52fc54.asp"><span class="caps">VBS</span>cript</a> code to replace these characters before attempting to save the file.</p>


<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">   <span style="color: #E56717; font-weight: bold;">Function</span> SanitizeFilename(<span style="color: #151B8D; font-weight: bold;">byVal</span> strFilename, <span style="color: #151B8D; font-weight: bold;">byVal</span> strReplChar)	
&nbsp;
      <span style="color: #151B8D; font-weight: bold;">Set</span> objRegExp = <span style="color: #E56717; font-weight: bold;">New</span> RegExp <span style="color: #008000;">' Create new RegExp object
</span>      
      <span style="color: #008000;">' Define regex pattern and set Global replacement property
</span>      objRegExp.Pattern = <span style="color: #800000;">&quot;[\x00-\x1f\x22\\\/:\*\?&lt;&gt;\|]&quot;</span>
      objRegExp.Global = <span style="color: #00C2FF; font-weight: bold;">True</span>
&nbsp;
      <span style="color: #008000;">' Check strReplChar parameter for invalid length or character,
</span>      <span style="color: #008000;">' default to underscore
</span>      <span style="color: #8D38C9; font-weight: bold;">If</span> Len(strReplChar) = 1 <span style="color: #8D38C9; font-weight: bold;">Then</span> 
         strReplChar = objRegExp.Replace(strReplChar, <span style="color: #800000;">&quot;_&quot;</span>)
      <span style="color: #8D38C9; font-weight: bold;">Else</span> 
         strReplChar = <span style="color: #800000;">&quot;_&quot;</span>
      <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span>
&nbsp;
      <span style="color: #008000;">' Return clean filename
</span>      SanitizeFilename = objRegExp.Replace(strFilename, strReplChar)
&nbsp;
      <span style="color: #151B8D; font-weight: bold;">Set</span> objRegExp = <span style="color: #00C2FF; font-weight: bold;">Nothing</span>
   <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></pre></div></div>




<p>This function accepts two arguments, <code>strFilename</code> which receives the string created from the form values, and <code>strReplChar</code> which receives the character used for replacement. The function uses a <a href="http://en.wikipedia.org/wiki/Regex">regular expression</a> pattern to match the invalid characters (control characters through 31, double-quotes, and a handful of others). There is a check to make sure that the <code>strReplChar</code> argument is valid, but not much else. The <a href="http://msdn.microsoft.com/library/en-us/script56/html/9f1c25ba-46ce-46af-9f19-ac1d2bcf05d8.asp">RegExp Object</a> member <code>Replace</code> does the heavy lifting, before the function returns the modified string.</p>

<p>The <acronym title="Regular Expression">regex</acronym> pattern replaces invalid characters for Windows <a href="http://en.wikipedia.org/wiki/NTFS"><acronym title="New Technology File System">NTFS</acronym></a> (generally), but could easily be altered for other <acronym title="Operating System">OS</acronym> file systems. You could also use a more restrictive regular expression like:</p>

<div class="example">


<pre><code>
      [&amp;#094;\w\x20\&amp;%'`\-\@{}~!#\(\)&amp;_\&amp;#094;\+,\.=\[\]]
</code></pre>


</div>

<p>Then again, that may be a bit much. Wikipedia has a good <a href="http://en.wikipedia.org/wiki/Filename">filename</a> reference that includes a fairly comprehensive list of reserved words and allowable character sets. As always, if you need a good regex reference, try <a href="http://www.regular-expressions.info/">Regular-Expressions.info</a>.</p>

<p><strong><span class="caps">UPDATE</span>:</strong> The logic in my if/then statement to check the length and validity of the <code>strReplChar</code> was backwards. The example has been corrected.</p>]]></content:encoded>
			<wfw:commentRss>http://mitchcontla.com/2006/05/18/sanitize-file-names-using-vbscript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

