Monday, January 28, 2008

An initial public offering

I no longer know how this thing works and so I must set it free. My old roomate remembers me cracking away at this some years ago - I'll take his word for it. I should probably remove VBScript from my résumé (and good riddance!). Save this text into a text file with a .wsf extension (i.e. playlistgenerator.wsf) It is a playlist generator BTW.
<job>
  <object id = oRS progid = "ADODB.Recordset"/>
  <reference object = "ADODB.Recordset"/>
  <script language = "VBScript">
    Option Explicit

    Sub DefineAndOpenRS()
      'Define and open the disconnected recordset

      With oRS
        .ActiveConnection = Nothing
        .CursorLocation = adUseClient
        .CursorType = adOpenStatic
        .LockType = adLockBatchOptimistic
        With .Fields
          .Append "MyStuff", adVarChar, 255
        End With
        .Open
      End With
    End Sub

    Sub InsertRow(sData)
      'Add data to a new row in the recordset

      oRs.AddNew
      oRS.Fields.Item("MyStuff").Value = sData
    End Sub

    Function ConcatRows()
      With oRS
        .MoveFirst
        ConcatRows = ""
        Do
          if oRS.Fields.Item("MyStuff").Value = "playlist generator" or _
             oRS.Fields.Item("MyStuff").Value = "playlist.m3u" then
            ' do nothing
          else
            ConcatRows = ConcatRows _
            & .Fields.Item("MyStuff").Value _
            & vbNewLine
          end if

          .MoveNext
        Loop Until .EOF
      End With
    End Function

    Dim lItem, sResults

    DefineAndOpenRS

    dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")

    dim folder
    Set folder = fso.GetFolder(".")

    dim fileList
    Set fileList = folder.Files

    dim playListFile
    Set playListFile = folder.CreateTextFile("playlist.m3u", true)

    dim file
    for each file in fileList
      if file.Name = "playlist generator.wsf" or file.Name = "playlist.m3u" then
        ' do nothing
      else
        InsertRow(file.Name)
      end if
    next

    oRS.Sort = "MyStuff ASC"

    do while not oRS.eof
      playListFile.WriteLine(oRS.Fields.Item("MyStuff").Value)
      oRS.movenext
    loop

    playListFile.Close

    sResults = ConcatRows()
    oRS.Close
    MsgBox sResults, vbOkOnly
  </script>
</job>



No comments: