Dec 08

IDK.vbs

'=========================================================================
' IDK.vbs
' VERSION: 1.0
' AUTHOR: Brian Steinmeyer
' EMAIL: sigkill@sigkillit.com
' WEB: http://sigkillit.com
' DATE: 12/8/2012
' COMMENTS: If you have ever had an issue making a decision like "What Should
' I eat for dinner" or "What should I do tonight?" then IDK aka "I Don't Know"
' is the script for you. It will randomly select a decision for you from a line
' delimited text file you pass to the script. You can run the script with Wscript
' by dragging and dropping your file onto the script or with Cscript by using
' the file path as the argument passed. You can make lists for all types of
' decisions you commonly make, and a few ideas are Yes/No, Magic8Ball, Pizza Shops,
' Restaurants, Movies, etc.
' EXAMPLE: Drag-N-Drop with WScript
'          Drag and Drop a file called Pizza.txt onto IDK.vbs to select a pizza shop
' EXAMPLE: From and command prompt with Cscript
'          C:\>cscript IDK.vbs "C:\My Lists\pizza.txt"
'=========================================================================
Option Explicit

Wscript.Echo IDK(wscript.arguments.item(0))

Private Function IDK(filePath)

    On Error Resume Next

    Const ForReading = 1
    Dim strResult
    Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
    If Not objFSO.FileExists(filePath) Then
        strResult = "ERROR - Input File Does Not Exist!"
    Else
        'Parse Input File into Array
        Dim objTextFile: Set objTextFile = objFSO.OpenTextFile(filePath, ForReading)
        Dim arrLine()
        Dim intSize: intSize = 0
        Dim strLine, blnMatch: blnMatch = False
        Do Until objTextFile.AtEndOfStream
            strLine = Trim(objTextFile.Readline)
            If Not strLine = "" Then
                blnMatch = True
                ReDim Preserve arrLine(intSize)
                arrLine(intSize) = strLine
                intSize = intSize + 1
            End If
        Loop
        'Randomly Select
        Dim intHighNumber: intHighNumber = UBound(arrLine)
        Dim intLowNumber: intLowNumber = LBound(arrLine)
        Dim intNumber
        Randomize
        strResult = arrLine(Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber))      
    End If

    'Return Results
    If Err.Number <> 0 Then
        Err.Clear
        strResult = "ERROR - Parsing Input File!"
    End If 
    IDK = strResult

    Set objFSO = Nothing

    On Error Goto 0

End Function

 

Jan 01

CreateFileOrDir.vbs

'=========================================================================
' CreateFileOrDir.vbs
' VERSION: 1.0
' AUTHOR: Brian Steinmeyer
' EMAIL: sigkill@sigkillit.com
' WEB: http://sigkillit.com
' DATE: 1/1/2011
' COMMENTS: Pass a File or Folder Path to the Sub and It Will Create the
' Full Path of the Directory Structure.
' EXAMPLE: Call CreateFileOrDir("C:\Level 1\Level 2\test.txt")
'          Call CreateFileOrDir("C:\Test\Folder\Structure")
'=========================================================================
Option Explicit

Call CreateFileOrDir("C:\Level 1\Level 2\test.txt")

Private Sub CreateFileOrDir(strPath)

    On Error Resume Next

    Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.DriveExists(objFSO.GetDriveName(strPath)) Then
        If StrComp(objFSO.GetExtensionName(strPath), "", 1) = 0 Then
            If Not objFSO.FolderExists(strPath) Then
                If objFSO.FolderExists(objFSO.GetParentFolderName(strPath)) Then
                    objFSO.CreateFolder strPath 'Create Folder In Current Path
                Else
                    CreateFileOrDir(objFSO.GetParentFolderName(strPath)) 'Recurse Creating Parent Folder
                    CreateFileOrDir(strPath) 'Recurse Creating Current Folder
                End If
            End If
        Else
            If Not objFSO.FileExists(strPath) Then
                If objFSO.FolderExists(objFSO.GetParentFolderName(strPath)) Then
                    objFSO.CreateTextFile strPath, True  'Create File In Current Path
                Else
                    CreateFileOrDir(objFSO.GetParentFolderName(strPath))  'Recurse Creating Parent Folder
                    CreateFileOrDir(strPath)  'Recurse Creating Current Folder
                End If
            End If
        End If
    End If

    On Error Goto 0

End Sub

 

Jan 01

Logger.vbs

'=========================================================================
' Logger.vbs
' VERSION: 1.0
' AUTHOR: Brian Steinmeyer
' EMAIL: sigkill@sigkillit.com
' WEB: http://sigkillit.com
' DATE: 1/1/2011
' COMMENTS: Pass the Log Name, Message, and Whether It's a New Log File
' to the Sub and It will Create or Append the Specified Log File in the Current
' Directory. The Log Name Can Be a Full Directory Path, or Just the File Name
' To Create It in the Current Directory. This Sub is Useful When You Need to
' Create Multiple Output Files and Use Multiple Subs/Functions.
' EXAMPLE: Call Logger("Test.txt", "Test Message", True)
'          Call Logger("C:\Full\Path\test.txt", "Test Message", True)
'          Call Logger("C:\Full\Path\test.txt", "Call 2", False)
'          Call Logger("C:\Full\Path\test.txt", "Call 3" &amp; vbCrLf &amp; "Line 4", False)
'          Call Logger("Test.txt", "Test Message2", False)
'=========================================================================
Option Explicit

Call Logger("Test.txt", "Test Message", True)
Call Logger("C:\Full\Path\test.txt", "Test Message", True)
Call Logger("C:\Full\Path\test.txt", "Call 2", False)
Call Logger("C:\Full\Path\test.txt", "Call 3" &amp; vbCrLf &amp; "Line 4", False)
Call Logger("Test.txt", "Test Message2", False)
Wscript.Echo "Finished"

Private Sub Logger(fileName, logMessage, blnNewLog)

    On Error Resume Next

    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim scriptPath: scriptPath = Left(WScript.ScriptFullName,InstrRev(WScript.ScriptFullName,"\"))
    Dim logName
    If InStr(1,fileName,"\",1) > 0 Then
        logName = fileName
        If objFSO.DriveExists(objFSO.GetDriveName(logName)) Then
            If StrComp(objFSO.GetExtensionName(logName), "", 1) = 0 Then
                If Not objFSO.FolderExists(logName) Then
                    If objFSO.FolderExists(objFSO.GetParentFolderName(logName)) Then
                        objFSO.CreateFolder logName 'Create Folder In Current Path
                        Exit Sub
                    Else
                        Call Logger(objFSO.GetParentFolderName(logName), logMessage, blnNewLog) 'Recurse Creating Parent Folder
                        Call Logger(logName, logMessage, blnNewLog) 'Recurse Creating Current Folder
                        Exit Sub
                    End If
                End If
            Else
                If Not objFSO.FileExists(logName) Then
                    If Not objFSO.FolderExists(objFSO.GetParentFolderName(logName)) Then
                        Call Logger(objFSO.GetParentFolderName(logName), logMessage, blnNewLog)  'Recurse Creating Parent Folder
                        Call Logger(logName, logMessage, blnNewLog)  'Recurse Creating Current Folder
                    End If
                End If
            End If
        End If
    Else
        logName = scriptPath & fileName
    End If
    Dim logFile
    If blnNewLog = True Then
        Set logFile = objFSO.CreateTextFile(logName, True)
    Else
        If objFSO.FileExists(logName) Then
            Set logFile = objFSO.OpenTextFile(logName, ForAppending, True)
        Else
            Set logFile = objFSO.CreateTextFile(logName, True)
        End If
    End If
    logFile.WriteLine logMessage
    logFile.Close
    Set objFSO = Nothing

    On Error Goto 0

End Sub