Monday, January 12, 2009

Script Code: Stoopid Script Tricks

Don't even ask "why".  Just go with it.  Sometimes it's worth it to just do something that makes no sense.  This is as true with scripting as it is with driving in traffic.  This dumb script shows how to use one script to write another script and then run it.  The newly-written script also executes a third script.  You can get as crazy with this as you want, especially if you're really bored.  Enjoy!

    Option Explicit

    Const ForReading = 1
    Const ForWriting = 2
    Const scriptFile = "c:\scripts\runthis.bat"
    Const alertCode = "disconnect_file_sessions"

    Dim objFSO, objFile, objShell

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(scriptFile, ForWriting, True)

    objFile.WriteLine "@echo off"
    objFile.WriteLine "rem This script is generated by another script - do not modify!"
    objFile.WriteLine "openfiles.exe /disconnect /id *"
    objFile.WriteLine "cscript.exe /nologo c:\scripts\sendalert.vbs "alertCode

    objFile.Close
    Set objFile = Nothing
    Set objFSO = Nothing

    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run "cmd.exe /c " & scriptFile, 1, True
    Set objShell = Nothing

Post a Comment