MacBook, defective by design banner

title:
Put the knife down and take a green herb, dude.


descrip:

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.

x

MarkUpDown is the best Markdown editor for professionals on Windows 10.

It includes two-pane live preview, in-app uploads to imgur for image hosting, and MultiMarkdown table support.

Features you won't find anywhere else include...

You've wasted more than $15 of your time looking for a great Markdown editor.

Stop looking. MarkUpDown is the app you're looking for.

Learn more or head over to the 'Store now!

Wednesday, May 25, 2022

Are you using vbscript?

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.

Labels: ,


posted by ruffin at 5/25/2022 08:07:00 AM
Thursday, December 04, 2008

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"

Debug.Print "#" & InStr(strTest, strToFind) & "#"
Debug.Print "#" & Left(strTest, InStr(strTest, strToFind)) & "#"
Debug.Print "#" & Right(strTest, Len(strTest) - InStr(strTest, strToFind)) & "#"
Debug.Print
Debug.Print "#" & Left(strTest, InStr(strTest, strToFind) - 1) & "#"
Debug.Print "#" & Right(strTest, Len(strTest) - (InStr(strTest, strToFind) + Len(strToFind))) & "#"


Here's our result:
#11#
#this is a l#
#ong line of words#

#this is a #
#line of words#


I don't know why, but I get more OBO errors doing this than I can stand.

----------------
Now playing: The Black Crowes - Cursed Diamond

Labels: ,


posted by ruffin at 12/04/2008 11:52:00 PM
Wednesday, July 30, 2003



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
                
                strFilter = strFilter & _
                    strPipe & _
                    strTemp & " Files (*" & strDot & strTemp & ")" & _
                    "|*" & strDot & strTemp
                
                strPipe = "|"   ' separator after the first extension type.
                
            Next
            
            .Filter = strFilter
        End If
        
        .FilterIndex = 0
        lngFlags = mscomdlg.cdlOFNPathMustExist + _
            mscomdlg.cdlOFNHideReadOnly

        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
    
    selectFile = strReturn
End Function

Labels: ,


posted by ruffin at 7/30/2003 10:08:00 AM

<< Older | Newer >>


Support freedom
All posts can be accessed here:


Just the last year o' posts:

URLs I want to remember:
* Atari 2600 programming on your Mac
* joel on software (tip pt)
* Professional links: resume, github, paltry StackOverflow * Regular Expression Introduction (copy)
* The hex editor whose name I forget
* JSONLint to pretty-ify JSON
* Using CommonDialog in VB 6 * Free zip utils
* git repo mapped drive setup * Regex Tester
* Read the bits about the zone * Find column in sql server db by name
* Giant ASCII Textifier in Stick Figures (in Ivrit) * Quick intro to Javascript
* Don't [over-]sweat "micro-optimization" * Parsing str's in VB6
* .ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture); (src) * Break on a Lenovo T430: Fn+Alt+B
email if ya gotta, RSS if ya wanna RSS, (?_?), ยข, & ? if you're keypadless


Powered by Blogger etree.org Curmudgeon Gamer badge
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.