dcsimg
September 22, 2020
Hot Topics:

Writing Active Server components in Visual Basic

  • By Thornton Rose
  • Send Email »
  • More Articles »


' Class FileIO

Option Explicit

Private objApplication As Application  ' Reference to Application object
Private objResponse As Response        ' Reference to Response object

' OnStartPage():
'
' This procedure is called when an instance of this class is created in an
' Active Server Page. It gets references to the Application and Response
' objects that are being used in the ASP page that created the instance of
' this class.
'
Public Sub OnStartPage(objScriptingContext As ScriptingContext)
   Set objApplication = objScriptingContext.Application()
   Set objResponse = objScriptingContext.Response()
End Sub

' OnEndPage():
'
' This procedure is called when an instance of this class is destroyed in 
' an Active Server Page. It is used to clean up object references.
'
Public Sub OnEndPage()
   Set objApplication = Nothing
   Set objResponse = Nothing
End Sub

' help():
'
' Writes help information to the response stream.
'
Public Sub help()
   objResponse.Write "

Class FileIO

" & vbCrLf objResponse.Write "Usage:" & vbCrLf objResponse.Write "set o = Server.CreateObject(""ASCExample.FileIO"")" & vbCrLf objResponse.Write "

Methods:" & vbCrLf objResponse.Write "

    " & vbCrLf objResponse.Write "
  • help() - shows help" & vbCrLf objResponse.Write "
  • fileSize(strName) - returns size of named file" & vbCrLf objResponse.Write "
  • fileExists(strName) - returns True if named file exists" & vbCrLf objResponse.Write "
  • deleteFile(strName) - deletes named file" & vbCrLf objResponse.Write "
  • appendLine(strName, strLine) - appends line to file" & vbCrLf objResponse.Write "
" & vbCrLf End Sub ' fileSize(): ' ' Returns the size of the file with the given name. ' Public Function fileSize(strFileName) As Long fileSize = FileLen(strFileName) End Function ' fileExists(): ' ' Returns True if the specified file exists. Note that the given file ' specification can contain wildcard characters. ' Public Function fileExists(strFileSpec) As Boolean fileExists = (Dir(strFileSpec) <> "") End Function ' deleteFile(): ' ' Deletes files that match the given file specification, which can include ' wildcard characters. ' Public Sub deleteFile(strFileSpec) Call Kill(strFileSpec) End Sub ' appendLine(): ' ' Appends a line to a text file (which is handy when working with log ' files.) ' Public Sub appendLine(strFileName, strLine) Dim objFileSystem As FileSystemObject Dim objStream As TextStream Set objFileSystem = New FileSystemObject Set objStream = objFileSystem.OpenTextFile(strFileName, 8, True, False) objStream.WriteLine strLine objStream.Close End Sub ' End Class FileIO [end] -->

  • Save everything and compile. That's it.

    Testing

    Testing your Active Server component is easy (although debugging can get tedious). A few lines of VBScript or JavaScript in an ASP page is all that is required. Here's the test script:

    <html> <head> <meta name="WT.qs_dlk" content="X2mtmU-32C24IatDc1jrBQAAAB4"/> <meta name="WT.qs_shmv" content="hv20200211-shoemaker.sf.quinstreet.net"/> <meta name="inject_params" content="WT.qs_dlk=X2mtmU-32C24IatDc1jrBQAAAB4&"/> <script>!function(){if(!window.BOOMR||!window.BOOMR.version){var e,t,n,o=document.createElement("iframe");o.src="javascript:false",o.title="",o.role="presentation",(o.frameElement||o).style.cssText="width:0;height:0;border:0;display:none;",(n=document.getElementsByTagName("script")[0]).parentNode.insertBefore(o,n);try{t=o.contentWindow.document}catch(n){e=document.domain,o.src="javascript:var d=document.open();d.domain='"+e+"';void(0);",t=o.contentWindow.document}t.open()._l=function(){var t=this.createElement("script");e&&(this.domain=e),t.id="boomr-if-as",t.src="/imageserver/common/sg/1.0/sg.min.js",BOOMR_lstart=(new Date).getTime(),this.body.appendChild(t)},t.write('<body onload="document._l();">'),t.close()}}();</script> <script> document.cookie = 'inject_params=WT.qs_dlk=X2mtmU-32C24IatDc1jrBQAAAB4&; path=/; domain=developer.com;; SameSite=None; Secure;'; document.cookie = 'WMUUID=X2mtmU-32C24IatDc1jrBQAAAB4; path=/; domain=developer.com;; SameSite=None; Secure;'; </script> <title>ASCExample Tester</title> <script> window.webtrendsAsyncInit=function(){ var dcs=new Webtrends.dcs().init({ dcsid:"dcs38irdn10000g0vc4171yva_9y7z", domain:"www.qsstats.com", timezone:-8, i18n:true, onsitedoms:"developer.com", fpcdom:".developer.com", plugins:{ //hm:{src:"//s.webtrends.com/js/webtrends.hm.js"} } }).track(); }; (function(){ var s=document.createElement("script"); s.async=true; s.src="/imageserver/common/v10/webtrends.min.js"; var s2=document.getElementsByTagName("script")[0]; s2.parentNode.insertBefore(s,s2); }()); </script> <noscript><img alt="dcsimg" id="dcsimg" width="1" height="1" src="//www.qsstats.com/dcs38irdn10000g0vc4171yva_9y7z/njs.gif?dcsuri=/index.php/net/asp/article.php/10917_602621_3/Writing-Active-Server-components-in-Visual-Basic.htm&amp;WT.js=No&amp;WT.tv=10.4.1&amp;dcssip=www.developer.com&amp;WT.qs_dlk=X2mtmU-32C24IatDc1jrBQAAAB4&"/></noscript> </head> <h3>ASCExample Tester</h3> <% ' Create a FileIO object. set objFileIO = Server.CreateObject("ASCExample.FileIO") %> <hr> <% ' Get some help from the FileIO object. objFileIO.help %> <hr> <% ' Create a file for testing. strLogFileName = "c:\temp\test.log" objFileIO.appendLine strLogFileName, "This is line 1." %> <!-- Get the file info. --> strLogFileName = <%= strLogFileName %><br> fileExists() => <%= CStr(objFileIO.fileExists(strLogFileName)) %><br> fileSize() => <%= objFileIO.fileSize(strLogFileName) %> bytes <% ' Delete the file. objFileIO.deleteFile(strLogFileName) %> <p> <!-- Check the file existance again. --> File deleted.<br> fileExists() => <%= CStr(objFileIO.fileExists(strLogFileName)) %> </html> [end] </pre> <!--end_section--></p> <br/> <br> <br> <br/>Page 3 of 3<br/><br/><div style="height: 40px;" id="pages"> <div style="margin-left: 50%;"> <ul style="margin-left: -120px;"> <li class="arrow-left"><a href="/net/asp/article.php/10917_602621_2/Writing-Active-Server-components-in-Visual-Basic.htm"><img src="/images0/arrow_left.jpg" width="16" height="16" /></a></li> <li class="begin-li"></li> <li><a href="/net/asp/article.php/602621/Writing-Active-Server-components-in-Visual-Basic.htm">1</a> </li> <li><a href="/net/asp/article.php/10917_602621_2/Writing-Active-Server-components-in-Visual-Basic.htm">2</a> </li> <li><a href="javascript:void(0);">3</a> </li> <li class="end-li"></li> </ul> </div> </div> <br/><br/> <div class="vspace clear"></div> <i>This article was originally published on April 30, 1998</i> <div class="vspace clear"></div> <div id="nodeSearchWidget" style="display: none"> </div> <div class="clear"></div> <div id="bottom-container"></div> <!-- START COMMENTING CODE --> <div id="article-comments" style="max-width: 100%;"></div> <br> <!-- END COMMENTING CODE --> <div class="vspace"></div> </div> <!-- article div ends --> </div> <div class="col colRight dimMainRight"> <div class="modal"></div> <div id="newsletter_subscribe" class="section"> <div id="heading" style="margin-top:5px"><h2>Enterprise Development Update</h2></div> <div id="staticText">Don't miss an article. Subscribe to our newsletter below.</div> <div id="nl_error" style="display:none;"></div> <div id="nl_success" style="display:none;"></div> <div id="nl_question_form"> <form id="newsletterform"> <div id="messageContainer" style="display: none;"></div> <div style="float:left;width:70%"> <input type="text" id="nl_email" name="nlemail" maxlength="255" class="nlemail" placeholder="Enter Email Address"> <input type="hidden" name="NewsletterSignup" value="Enterprise Development Update" id="NewsletterSignup"> <input type="hidden" name="nlNewsletterKey" value="1423" id="nlNewsletterKey"> <input type="hidden" name="nlBusinessUnit" value="Internet.com" id="nlBusinessUnit"> <input type="hidden" name="RegistrationWebsite" value="www.developer.com" id="RegistrationWebsite"> <input type="hidden" value="https://o1.qnsr.com/cgi/r?;n=203;c=1387912;s=15939;x=7936;f=201306031137030;u=j;z=TIMESTAMP;" name="nlQuadTrackUrl"> </div> <div style="float:left;width:30%"> <input type="submit" name="nlSubmit" onclick="newsletterGlobalSubmit('newsletterform', 'messageContainer', 'messageContainer'); return false;" value="SIGN UP" id="nl_submit_button"><br> </div> </form> </div> <div class="vspace"></div> </div> <div class="vspace"></div> <div class="adsBox"> <!-- QS-AD: 'imu' start --> <script language="JavaScript"> var t=displayDFPTag("imu"); document.write(t); </script> <!-- QS-AD: 'imu' end --> </div> <div class="vspace"></div> <div id="mostPopularStories" class="section"> <div class="sectionHeader"> <h2>Most Popular Developer Stories</h2> <a href="#" class="more"></a> </div> <div class="sectionContent"> <div class="tabs"> <div class="tabsMenu clearfix"> <ul> <li><a href="#mostPopularToday" class="selected">Today</a></li> <li><a href="#mostPopularThisWeek">This Week</a></li> <li><a href="#mostPopularAllTime">All-Time</a></li> </ul> </div> <div class="tabsContents"> <div class="tabsContent selected" id="mostPopularToday"> <ul> <li><a href="http://www.developer.com/java/data/article.php/3417381/Using-JDBC-with-MySQL-Getting-Started.htm"><span class="graytext">1</span> Using JDBC with MySQL, Getting Started</a></li> <li><a href="http://www.developer.com/design/article.php/2109801/Creating-Use-Case-Diagrams.htm"><span class="graytext">2</span> Creating Use Case Diagrams</a></li> <li><a href="http://www.developer.com/java/other/article.php/3556176/An-Introduction-to-Java-Annotations.htm"><span class="graytext">3</span> An Introduction to Java Annotations</a></li> <li><a href="http://www.developer.com/java/other/article.php/3559931/Hibernate-Basics.htm"><span class="graytext">4</span> Hibernate Basics</a></li> <li><a href="http://www.developer.com/net/asp/article.php/3096831/Using-ASPNET-To-Send-Email.htm"><span class="graytext">5</span> Using ASP.NET To Send Email</a></li> </ul> </div> <div class="tabsContent" id="mostPopularThisWeek"> <ul> <li><a href="http://www.developer.com/java/data/article.php/3417381/Using-JDBC-with-MySQL-Getting-Started.htm"><span class="graytext">1</span> Using JDBC with MySQL, Getting Started</a></li> <li><a href="http://www.developer.com/lang/php/article.php/3896056/10-Experimental-PHP-Projects-Pushing-the-Envelope.htm"><span class="graytext">2</span> 10 Experimental PHP Projects Pushing the Envelope</a></li> <li><a href="http://www.developer.com/java/other/article.php/3559931/Hibernate-Basics.htm"><span class="graytext">3</span> Hibernate Basics</a></li> <li><a href="http://www.developer.com/java/other/article.php/3556176/An-Introduction-to-Java-Annotations.htm"><span class="graytext">4</span> An Introduction to Java Annotations</a></li> <li><a href="http://www.developer.com/db/article.php/3379271/Oracle-Programming-with-PLSQL-Collections.htm"><span class="graytext">5</span> Oracle Programming with PL/SQL Collections</a></li> </ul> </div> <div class="tabsContent" id="mostPopularAllTime"> <ul> <li><a href="http://www.developer.com/java/data/article.php/3417381/Using-JDBC-with-MySQL-Getting-Started.htm"><span class="graytext">1</span> Using JDBC with MySQL, Getting Started</a></li> <li><a href="http://www.developer.com/java/other/article.php/3559931/Hibernate-Basics.htm"><span class="graytext">2</span> Hibernate Basics</a></li> <li><a href="http://www.developer.com/db/article.php/3379271/Oracle-Programming-with-PLSQL-Collections.htm"><span class="graytext">3</span> Oracle Programming with PL/SQL Collections</a></li> <li><a href="http://www.developer.com/java/other/article.php/3556176/An-Introduction-to-Java-Annotations.htm"><span class="graytext">4</span> An Introduction to Java Annotations</a></li> <li><a href="http://www.developer.com/design/article.php/2109801/Creating-Use-Case-Diagrams.htm"><span class="graytext">5</span> Creating Use Case Diagrams</a></li> </ul> </div> </div> </div> </div> </div> <!-- Code to put on site page to show pinning option --> <!-- commented out 10/22 as per CR "CHG231107: Remove IE 9 Site pinning check" <script type="text/javascript" language="javascript"> if(jQuery.browser.msie){ if(document.documentMode != undefined && document.documentMode==9){ if (!window.external.msIsSiteMode()) { document.write('<div id="pinMeContainer"><img class="msPinSite" src="/images0/developer64x64.png" height="64" weight="64" /><br/><span id="pinMe">To Pin this site, drag this image to the left to your Windows Taskbar. Try it now!</span></div>'); }else { try { if (window.external.msIsSiteMode()) { jQuery.get('https://www.developer.comlatest_articles', function(data){ displayLatestNews(data); }); } } catch (ex) { } } }else{ document.write('<div id="pinMeContainer"><img class="msPinSite" src="/images0/developer64x64.png" height="64" weight="64" /><br/><span id="pinMe">Upgrade your browser to IE 9 to see the power of Site Pinning!</span></div>'); } } function displayLatestNews(data){ try{ g_ext = window.external; if(data.articles != undefined && data.articles.length){ g_ext.msSiteModeClearJumpList(); g_ext.msSiteModeCreateJumplist('Developer.com Latest News'); for(i=0; i<data.articles.length; i++){ g_ext.msSiteModeAddJumpListItem (data.articles[i].title, data.articles[i].link, 'https://www.developer.comfavicon.ico'); } var dt = new Date(); var updated = (dt.getMonth()+1) +"/"+(dt.getDay()+1)+"/"+dt.getFullYear()+" "+dt.toLocaleTimeString(); g_ext.msSiteModeAddJumpListItem ('Last updated - '+updated, 'https://www.developer.com', 'https://www.developer.com/images0/refresh.ico'); g_ext.msSiteModeShowJumpList(); } }catch(ex){ } } </script> --> <!-- End of code to put on site page to show pinning option --> <div class="adsBox"> <!-- QS-AD: 'imu' start --> <script language="JavaScript"> var t=displayDFPTag("imu"); document.write(t); </script> <!-- QS-AD: 'imu' end --> </div> <div class="vspace"></div> <div id="mostCommentedOn" class="section"> <div class="sectionHeader"> <h2>Most Commented On</h2> <a href="#" class="more"></a> </div> <div class="sectionContent"> <div class="tabs"> <div class="tabsMenu clearfix"> <ul> <li><a href="#mostCommentedOnThisWeek" class="selected">This Week</a></li> <li><a href="#mostCommentedOnThisMonth">This Month</a></li> <li><a href="#mostCommentedOnAllTime">All-Time</a></li> </ul> </div> <div class="tabsContents"> <div class="tabsContent selected" id="mostCommentedOnThisWeek"> <ul> <li><a href="http://www.developer.com/article.php/3896056"><span class="graytext">1</span> 10 Experimental PHP Projects Pushing the<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Envelope</a></li><li><a href="http://www.developer.com/article.php/777761"><span class="graytext">2</span> Day 1: Learning the Basics of PL/SQL</a></li><li><a href="http://www.developer.com/article.php/3336751"><span class="graytext">3</span> C# Tip: Placing Your C# Application in the<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System Tray</a></li><li><a href="http://www.developer.com/article.php/641521"><span class="graytext">4</span> Logical Versus Physical Database Modeling</a></li><li><a href="http://www.developer.com/article.php/3896711"><span class="graytext">5</span> Is Ubuntu Contributing as Much as It Should to<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Free Software Projects?</a></li> </ul> </div> <div class="tabsContent" id="mostCommentedOnThisMonth"> <ul> <li><a href="http://www.developer.com/article.php/777761"><span class="graytext">1</span> Day 1: Learning the Basics of PL/SQL</a></li><li><a href="http://www.developer.com/article.php/3894316"><span class="graytext">2</span> The 5 Developer Certifications You'll Wish You<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Had in 2015</a></li><li><a href="http://www.developer.com/article.php/3896056"><span class="graytext">3</span> 10 Experimental PHP Projects Pushing the<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Envelope</a></li><li><a href="http://www.developer.com/article.php/1495931"><span class="graytext">4</span> An Introduction to Struts</a></li><li><a href="http://www.developer.com/article.php/3894566"><span class="graytext">5</span> Inside Facebook's Open Source Infrastructure</a></li> </ul> </div> <div class="tabsContent" id="mostCommentedOnAllTime"> <ul> <li><a href="http://www.developer.com/article.php/2109801"><span class="graytext">1</span> Creating Use Case Diagrams</a></li><li><a href="http://www.developer.com/article.php/777761"><span class="graytext">2</span> Day 1: Learning the Basics of PL/SQL</a></li><li><a href="http://www.developer.com/article.php/3336751"><span class="graytext">3</span> C# Tip: Placing Your C# Application in the<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System Tray</a></li><li><a href="http://www.developer.com/article.php/3096831"><span class="graytext">4</span> Using ASP.NET To Send Email</a></li><li><a href="http://www.developer.com/article.php/3417381"><span class="graytext">5</span> Using JDBC with MySQL, Getting Started</a></li> </ul> </div> </div> </div> </div> </div> <div class="vspace"></div> </div> <div class="vspace clear"></div> <div class="vspace clear"></div> <div style="clear:both;"> <div style="text-align: center;"> <div id="lb-wrapper" align="center"> <div id="ad_p2-lb" style="text-align:center;"> <!-- QS-AD: 'lb' start --> <script language="JavaScript"> var t=displayDFPTag("lb"); document.write(t); </script> <!-- QS-AD: 'lb' end --> </div> </div> </div> <br/> <FONT SIZE="-1" FACE="arial, helvetica"> <style type="text/css"> #brand-footer{text-align:center;margin:0 auto;} #commonfooterpropertytext{padding-bottom: 12px;} #commonfootercopyright{padding: 3px;} #eweekBrand-footer{text-align:center;margin:0 auto} #footerDisclaimerText{margin: 15px 10px 0px 10px; text-align: justify;} </style> <div id="brand-footer"> <div id="acceptableusepolicy" style="display:none;"> <a rel='nofollow' href='/aup.html' target='_blank' style='color: #000000;'>Acceptable Use Policy</a> </div> <div> <img id="commonfooterlogo" src="" alt="Site Logo" border="0" class="lazy" /> </div> <div id="commonfooterpropertytext"></div> <div id="footerlinks"> <a rel="nofollow" id="commonfootetearmslink" href="https://technologyadvice.com/terms-conditions/">Terms of Service</a> | <a rel="nofollow" id="commonfooteraboutuslink" href="https://technologyadvice.com/about-us/" target="_blank">About Us</a> | <a rel="nofollow" id="commonprivacylink" href="/privacy">Privacy Notice</a> | <a rel="nofollow" id="commonfootercontactuslink" href="https://technologyadvice.com/contact-us/">Contact Us</a> | <a rel="nofollow" id="commonadvertiselink" href="https://technologyadvice.com/programs/digital-advertising-solutions/" target="_blank">Advertise</a> | <a rel="nofollow" id="commonsitemaplink" href="/sitemap.html" target="_blank">Sitemap</a> | <a rel="nofollow" id="commonfootercalifornialink" href="https://technologyadvice.com/privacy-policy/ccpa-opt-out-form" target="_blank">California - Do Not Sell My Info</a> </div> <div id="commonfootercopyright"></div> <div id="footerDisclaimerText"> <p> <strong>Advertiser Disclosure:</strong>&nbsp;<span id="advDisclaimer"></span> </p> </div> </div> <script async language="Javascript" src="/icom_includes/footers/scripts/footerbrandingcode.js"> </script> </font> <link href='https://fonts.googleapis.com/css?family=Lato&subset=latin,latin-ext' rel='stylesheet' type='text/css'> <div id="newuserregistrationpopup"> <div id="closepopup"><a class="newuserregistrationpopup-sprite close" href="#"></a></div> <div id="thankuptext">Thanks for your registration, follow us on our social networks to keep up-to-date</div> <div id="socialsharebtn"> <div id="socialbtns"><a class="sprite-user-register-popup-tw newuserregistrationpopup-sprite" href="https://o1.qnsr.com/cgi/r?WT.qs_dlk=X2mtmU-32C24IatDc1jrBQAAAB4;;n=203;c=1459370;s=8986;x=7936;f=201411241658150;u=j;z=TIMESTAMP;k=https://twitter.com/DeveloperCom" target="_blank"></a></div> <div id="socialbtns"><a class="sprite-user-register-popup-ln newuserregistrationpopup-sprite" href="https://o1.qnsr.com/cgi/r?WT.qs_dlk=X2mtmU-32C24IatDc1jrBQAAAB4;;n=203;c=1459370;s=8986;x=7936;f=201411241658150;u=j;z=TIMESTAMP;k=https://www.linkedin.com/company/developer-com?trk=top_nav_home" target="_blank"></a></div> <div id="socialbtns"><a class="sprite-user-register-popup-gplus newuserregistrationpopup-sprite" href="https://o1.qnsr.com/cgi/r?WT.qs_dlk=X2mtmU-32C24IatDc1jrBQAAAB4;;n=203;c=1459370;s=8986;x=7936;f=201411241658150;u=j;z=TIMESTAMP;k=https://plus.google.com/u/0/b/117958898397590965610/+Developerdotcom/posts" target="_blank"></a></div> <div id="socialbtns"><a class="sprite-user-register-popup-rss newuserregistrationpopup-sprite" href="https://o1.qnsr.com/cgi/r?WT.qs_dlk=X2mtmU-32C24IatDc1jrBQAAAB4;;n=203;c=1459370;s=8986;x=7936;f=201411241658150;u=j;z=TIMESTAMP;k=https://www.developer.com/developer/dev-25.xml" target="_blank"></a></div> </div> </div> </div><script type="text/javascript"> /* Replace #discussion_reference# by the unique identifer of the page */ var discussion_reference = blurbKey; var _oneall = _oneall || []; _oneall.push(['loudvoice', 'set_providers', ['facebook', 'twitter', 'google', 'linkedin']]); _oneall.push(['loudvoice', 'set_page', document.title, window.location.href]); _oneall.push(['loudvoice', 'set_reference', discussion_reference]); _oneall.push(['loudvoice', 'do_render_ui', 'article-comments']); </script> </div> </div> </div> <script language="JavaScript" type="text/javascript"> <!-- // Make sure all image has an alt tag if it doesn't already $(document).ready(function(){ $("img").each(function() { var img = $(this); if (!img.attr("alt")) img.attr("alt", ""); }); }); //--> </script> </body> </html>