One feller's views on the state of everyday computer science & its application (and now, OTHER STUFF) who isn't rich enough to shell out for www.myfreakinfirst-andlast-name.com
Using 89% of the same design the blog had in 2001.
FOR ENTERTAINMENT PURPOSES ONLY!!!
Back-up your data and, when you bike,
always wear white.
As an Amazon Associate, I earn from qualifying purchases. Affiliate links in green.
Okay, this was a fun time travel... Remember that post not even a week ago when I talked about our working museum of code at my current workplace?
That's (not surprisingly) relevant again, as I was reviewing some old .aspx code and incrementally refactoring out all the Response.Writes so we could move it into the 21st century. That meant trying to remember some vbscript string operators from my past life as a classic ASP dev.
I was trying to figure out how to test quick snippets of VB code and found this "gem" of a script (not in Ruby, to be clear ;^)) that allows you to have a limited interactive shell for vbscript:
do while true
wscript.stdout.write(">>>")
ln = wscript.stdin.readline
if lcase(trim(ln)) = "exit" then exit do
err.clear
ln = "str = """":" & ln & ":wscript.echo(""#"" & str & ""#"")"
execute ln
if err.number <> 0 then
wscript.echo(err.description)
end if
loop
(Yes, I added the echo for str overhead to save some typing if I'm playing with strings)
It requires a session to be a single line, which means you have to separate commands with :, but it's fairly useful in a pinch. Admittedly I wasn't as careful in that code with variable and keyword casing as I used to be when I used to code in vbscript... last century. ;)
Example usage:
c:\temp>cscript vbsimple.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.
>>>str = " asdfASDFasDF ":str = Trim(LCase(str))
#asdfasdfasdf#
>>>str =
Syntax error
>>>
There's an even fancier script over here (c2013) that allegedly allows multiple line entries, but I wasn't able to get that running with a trivial time expenditure.
It would be easy enough to make the one above a multiline shell, but for now, enjoy as-is, I reckon.
I don't know why, but when I need to code something ugly and quickly, I use VB6. When I need to parse lines, I use String functions instead of regexps. I tell myself it's because it's hard to anticipate how flawed, complicated regexps might work. This is true. Not a good excuse, but it's true. regexps can be difficult to debug.
Still, there's something I freakin' always screw up, and that's finding a substring in a line and cutting it into two. So here's some code for idiots that are me.
Dim strTest As String Dim strToFind As String
strTest = "this is a long line of words" strToFind = "long"
Untested code showing how to use the CommonDialog control in Visual Basic 6.
Basically I just wanted to stop having to hunt for this on CD to type it up from some slightly application-specific code and just come here from now on. Or, worse still, not paste the code in at all and start from scratch. QUELLE HORROR!!!
Just paste this code into a module called "mdlComDlg" or something similar. Poof.
(Updated 1/28/2004)
Option Explicit
'File Selection routine 'Copyright (C) 2003-4 R. Bailey ' 'This library is free software; you can redistribute it and/or 'modify it under the terms of the GNU Lesser General Public 'License as published by the Free Software Foundation; either 'version 2.1 of the License, or (at your option) any later version. ' 'This library is distributed in the hope that it will be useful, 'but WITHOUT ANY WARRANTY; without even the implied warranty of 'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 'Lesser General Public License for more details. ' 'You should have received a copy of the GNU Lesser General Public 'License along with this library; if not, write to the Free Software 'Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Public fso As FileSystemObject
' You must have a CommonDialog on some widget somewhere to pass in. You apparently ' can't create one at runtime from what I've googled. ' ' Will return "" if no file is selected/user cancels the select/an error occurs ' before a file is selected. ' ' Pass in a collection of extensions to search for with code similar to the following: ' ' Dim colExts As Collection ' Set colExts = New Collection ' ' With colExts ' .Add ".txt" ' .Add ".html" ' .Add ".htm" ' End With ' ' Me.RichTextBox1.Text = mdlComDlg.selectFile(Me.CommonDialog1, "C:\", colExts)
Public Function selectFile(cdlgTemp As CommonDialog, _ Optional strInitDir As String = "", _ Optional colFileExtensions As Collection, _ Optional bCreateFileNotOpen As Boolean = False, _ Optional strRetValIfCancel As String = "") As String On Error GoTo errHand ' mainly just going to be capturing ' for the cancel button in the common dialog control
Dim strReturn As String Dim strFilter As String Dim strDot As String Dim strPipe As String Dim strTemp As String Dim varTemp As Variant Dim lngFlags As Long
Dim isFileSelected As Boolean ' b/c of the way cancel errors ' are handled, we might branch out of the logic before everything's saved. ' If the user got far enough to select a file, ' we'll save it out in the error handler on cancel. ' This var will tell us if they got that far.
strReturn = "" ' no file yet isFileSelected = False ' no file yet strFilter = "" strPipe = "" strDot = "" Set fso = New FileSystemObject
With cdlgTemp If fso.FolderExists(strInitDir) Then .InitDir = strInitDir Else .InitDir = App.Path End If
.FileName = ""
' set up filters with the extensions (strings) in colFileExtensions. If Not colFileExtensions Is Nothing Then For Each varTemp In colFileExtensions strTemp = CStr(varTemp)
If InStr(strTemp, ".") = 1 Then strDot = "" Else strDot = "." End If
If Not bCreateFileNotOpen Then lngFlags = lngFlags + mscomdlg.cdlOFNFileMustExist Else lngFlags = lngFlags + mscomdlg.cdlOFNOverwritePrompt End If .Flags = lngFlags
.CancelError = True ' cancelling causes an error
If bCreateFileNotOpen Then .ShowSave Else .ShowOpen End If
strReturn = .FileName ' If they cancelled, ' we'll have "errored out" (cancelerror = true) isFileSelected = True ' If they didn't, ' remember that we've gotten this far.
' insert any code that needs to happen after a file is selected. End With
selectFile = strReturn
Exit Function errHand: If Not Err.Number = 32755 Then ' 32755 means that cancel ' was selected, and we'll ignore that one ' by just dropping out of the sub entirely
MsgBox Err.Number & " :: " & Err.Description If Not isFileSelected Then strReturn = "" End If Else ' else we've got a cancel error strReturn = strRetValIfCancel End If
The postings on this site are [usually] my own and do not necessarily reflect the views of any employer, past or present, or other entity. About Our Author