' =========================================================================== ' Description: This script launches the TWiki Windows Personal webserver ' Filename: Start.vbs ' Version: 1.0 ' ' Features: ' - Checks for valid installation ' - Relaunches web page but not TinyWeb service if TinyWeb service is started ' - Starts TinyWeb in %TMP% directory if read-only media is detected ' ============================================================================ Option Explicit ' Make sure our code is clean Dim CRLF ' Carriage Return / Line Feed Constant CRLF = Chr(13) + Chr(10) Dim objShell, objFSO, objFile, objEnv, objWMIService Dim strFile Dim strTMP Dim strTmpRoot, strTmpLogs, strTmpTinyWeb, strTmpPerl Dim strRoot, strRootTINYWEB, strRootHTDOCS, strRootPERL, strRootTOOLS Dim strCmd1, strCmd2 Dim strFolders Dim arrFolders Dim strNotFound Dim strProcess, strComputer Dim colProcesses Dim strLogDir Dim i ' Generic Loop Counter ' 1st command line argument = WScript.Arguments.Item(0) Set objShell = WScript.CreateObject("WScript.Shell") ' ---------------------------------------------------------------------------- ' Get path that the script was launched from..... ' ---------------------------------------------------------------------------- strFile = WScript.ScriptFullName 'WScript.echo strFile ' ---------------------------------------------------------------------------- ' ' Below, the %TMP% variable is read. See the link for more info. ' Called w/ "Process" argument for Windows 98 comapitibility. ' ' http://www.devguru.com/Technologies/wsh/quickref/wshshell_Environment.html ' ' ---------------------------------------------------------------------------- Set objEnv = objShell.Environment("Process") strTMP = objEnv("TMP") ' ---------------------------------------------------------------------------- Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.GetFile(strFile) 'WScript.Echo "Absolute path: " & objFSO.GetAbsolutePathName(objFile) 'WScript.Echo "Parent folder: " & objFSO.GetParentFolderName(objFile) 'WScript.Echo "File name: " & objFSO.GetFileName(objFile) 'WScript.Echo "Base name: " & objFSO.GetBaseName(objFile) 'WScript.Echo "Extension name: " & objFSO.GetExtensionName(objFile) strRoot = objFSO.GetParentFolderName(objFile) ' strRoot = Path script was executed from 'WScript.Echo strRoot ' ---------------------------------------------------------------------------- ' Check if installation is ok... This may not really be necessary but it's ' a good check to peformed... rolled over from the original batch file check ' for perl.exe ' ---------------------------------------------------------------------------- ' Make sure there aren't spaces in this string after the commas: strFolders = "\twiki\data,\twiki\lib,\twiki\locale,\twiki\templates," & _ "\twiki\tools,\htdocs\cgi-bin,\htdocs\pub,\logs," & _ "\perl\bin,\perl\lib,\tinyweb,\tools" arrFolders = split(strFolders, ",") strNotFound = "" For i = 0 To UBound(arrFolders) If NOT objFSO.FolderExists(strRoot & arrFolders(i)) THEN 'WScript.Echo strRoot & arrFolders(i) strNotFound = strNotFound & strRoot & arrFolders(i) & CRLF 'keep track of which folders aren't there End If Next '------------------------------------------------------------------------------ ' If the NotFound list isn't null, there was a problem... '------------------------------------------------------------------------------ If strNotFound <> "" Then ' (There was a problem...) strError = "Cannot find folders that are supposed to be installed:" & CRLF & CRLF & strNotFound strError = strError & CRLF & "- Make sure that the downloaded file is complete." strError = strError & CRLF & "- Make sure that your unzip pogram extracts subdirectories and preserves long filenames." MsgBox strError, vbOKOnly, "ERROR" WScript.Quit End If '------------------------------------------------------------------------------ ' No installation problems, next check to see if TinyWeb service is running or ' not... '------------------------------------------------------------------------------ '------------------------------------------------------------------------------ ' Put together Webserver commands to execute '------------------------------------------------------------------------------ strTmpRoot = strTMP & "\TWikiWP" strTmpLogs = strTmpRoot & "\logs" strTmpTinyWeb = strTmpRoot & "\TinyWeb" strTmpPerl = strTmpRoot & "\perl" strRootTINYWEB = strTmpTinyWeb strRootHTDOCS = strRoot strRootPERL = strTmpRoot strRootTOOLS = strRoot strCmd1 = strRootTINYWEB & "\tinyweb.exe " & _ strRootHTDOCS & "\htdocs 8765 127.0.0.1 .pl " & _ strRootPERL & "\perl\bin\perl.exe" strCmd2 = strRootTOOLS & "\tools\start.url" 'WScript.echo strCmd1 'WScript.echo strCmd2 ' ------------------------------------------------- ' Is TinyWeb running? ' ------------------------------------------------- strProcess = "TinyWeb.exe" strComputer = "." Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colProcesses = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = '" & strProcess & "'") If colProcesses.Count = 0 Then ' (no hits for process query, TinWeb NOT running... go ahead and launch) ' ------------------------------------------------------------------------ ' Create %TMP%\TWikiWP directory for copying webserver files ' ------------------------------------------------------------------------ If Not objFSO.FolderExists(strTmpRoot) Then objFSO.CreateFolder (strTmpRoot) End If ' ------------------------------------------------------------------------ ' Create %TMP%\TWikiWP\TinyWeb directory for copying webserver files ' ------------------------------------------------------------------------ ' If Not objFSO.FolderExists(strTmpTinyWeb) Then ' objFSO.CreateFolder (strTmpTinyWeb) ' End If ' ------------------------------------------------------------------------ ' Create %TMP%\TWikiWP\Logs directory for copying webserver files ' ------------------------------------------------------------------------ If Not objFSO.FolderExists(strTmpLogs) Then objFSO.CreateFolder (strTmpLogs) End If ' ------------------------------------------------------------------------ ' Copy folder and all contents, parent of destination folder must exist, ' but not the folder itself. Make sure parent of destination folder ends ' in "\" ' ------------------------------------------------------------------------ Const OverwriteExisting = True If Not objFSO.FolderExists(strTmpTinyWeb) Then objFSO.CopyFolder strRoot & "\TinyWeb", strTmpRoot & "\", OverWriteExisting ' Copy webserver End If If Not objFSO.FolderExists(strTmpPerl) Then objFSO.CopyFolder strRoot & "\Perl", strTmpRoot & "\", OverWriteExisting ' Copy Perl End If ' ------------------------------------------------------------------------ ' Change current working directory so TinyWeb will store logs in ' the right place... ' ------------------------------------------------------------------------ If 0 = 1 Then strLogDirDef = strRoot & "\logs" ' define the default that we can print in error msg strLogDir = strLogDirDef Set objFolder = objFSO.GetFolder(strLogDir) ' ------------------------------------------------------------------------ ' See if logs subdirectory is available for writing and not read-only... ' ------------------------------------------------------------------------ strFileName = objFSO.GetTempName ' Get a temp filename strFullName = objFSO.BuildPath(strLogDir, strFileName) ' Concatenate to [path + filename] On Error Resume Next ' Skip the create-file error if one is generated Set objFile = objFSO.CreateTextFile(strFullName) ' Create temp file objFile.Close ' Close(save) temp file On Error GoTo 0 ' Resume normal error handling ' ------------------------------------------------------------------------ ' Completed attempt to save a temporary file to disk. Now see if it's ' actually there which will detect read-only media or not, and take ' appropriate action. ' ------------------------------------------------------------------------ If objFSO.FileExists(strFullName) Then '(Read-Only media not detected) ' ---------------------------------------------------------------------- ' If file exists, that means it's not read only and TinyWeb will be able ' to create log files here... ' ---------------------------------------------------------------------- objFSO.DeleteFile(strFullName) ' Delete temp file, continue Else '(Read-Only media detected) ' ---------------------------------------------------------------------- ' Attempt to create temp file failed, which means the directory is ' read-only and TinyWeb won't be able to create logs there (which will ' make the process shut down). ' ---------------------------------------------------------------------- strLogDir = strTMP ' strLogDir = %TMP% directory strFullName2 = objFSO.BuildPath(strLogDir, strFileName) ' Append new TMP path to temp file Set objFile = objFSO.CreateTextFile(strFullName2) ' Create temp file objFile.Close ' Close(save) temp file If NOT objFSO.FileExists(strFullName2) Then strError = "Can't create log files in either location:" & CRLF strError = strError & CRLF & strLogDirDef & " (DEFAULT LOCATION)" strError = strError & CRLF & strLogDir & " (TEMPORARY LOCATION WHEN DEFAULT FAILS)" strError = strError & CRLF & CRLF & "This may cause the Webserver to exit immediately after startup..." MsgBox strError, vbCritical, "ERROR" End If End If End If strLogDir = strTmpLogs ' ------------------------------------------------------------------------ ' Change current working directory so TinyWeb will store logs in ' the right place... ' ------------------------------------------------------------------------ 'WScript.Echo "Starting from directory: " & strLogDir objShell.CurrentDirectory = strLogDir 'Wscript.echo strcmd1 objShell.Run (strCmd1) ' Start the web server Else ' Tinyweb is already running... ' Left this case here but don't need to do anything... ' Optionally could move execution of cmd2 here and above after cmd1 also (duplicate) End If On Error Resume Next ' Ignore error if browser won't stop objShell.Run (strCmd2) ' Start the homepage in the browser If Err <> 0 Then WScript.Echo "The server probably loaded and started correctly, but there was a problem launching the browser." 'Wscript.Echo Err.Number & " -- " & Err.Description 'Err.Clear End If On Error GoTo 0 ' Resume normal error handling WScript.Quit