Archive for July, 2009

h1

Shortcuts with VB Script

18/07/2009

Sometimes I need to create shortcuts with VB Script especially if the script is being installed and a shortcut needs to be created to the script.  The code to create a shortcut is quite straight forward,  but the CreateShortcut() function presented here pulls everything together into a single reusable form.  Optional parameters that you don’t want to use can be filled by the use of “NULL” and environment variables can be included in file paths to make this as flexible as possible.

Option Explicit

CreateShortcut "%UserProfile%\SendTo\Test Link.lnk", _
 "C:\Scripts\Test-Script.vbs", NULL, "A test script", NULL, NULL, NULL, NULL, NULL

Function CreateShortcut(p_strShortcutPath, p_strTargetPath, _
 p_strArguments, p_strComment, _
 p_strHotKey, p_strIconPath, p_strIconPosition, _
 p_strWindowStyle, p_strWorkingDirectory)

 'Example Parameters
 'p_strShortcutPath = "%UserProfile%\SendTo\Shortcut to Something"
 'p_strTargetPath = "%PROGRAMFILES%\Somewhere\Something.exe"
 'p_strArguments = "/O"
 'p_strDescription = "Start Something"
 'p_strHotKey = "CTRL+ALT+Q"
 'p_strIconPath = "%PROGRAMFILES%\Somewhere\Something.exe"
 'p_strIconPosition = "3"
 'p_strWindowStyle = "1"
 'p_strWorkingDirectory = "C:\Working\"
 'Use NULL for parameters which are not to be set

 Dim objShell, objShortcut

 Set objShell = WScript.CreateObject("WScript.Shell")
 Set objShortcut = objShell.CreateShortcut(objShell.ExpandEnvironmentStrings(p_strShortcutPath))

 If Not isNull(p_strTargetPath) Then
 objShortcut.TargetPath = objShell.ExpandEnvironmentStrings(p_strTargetPath)
 End If

 If Not isNull(p_strArguments) Then
 objShortcut.Arguments = p_strArguments
 End If

 If Not isNull(p_strComment) Then
 objShortcut.Description = p_strComment
 End If

 If Not isNull(p_strHotKey) Then
 objShortcut.HotKey = p_strHotKey
 End If

 If Not isNull(p_strIconPath) Then
 objShortcut.IconLocation = objShell.ExpandEnvironmentStrings(p_strIconPath & ", " & p_strIconPosition)
 End If

 If Not isNull(p_strWindowStyle) Then
 objShortcut.WindowStyle = p_strWindowStyle
 End If

 If Not isNull(p_strWorkingDirectory) Then
 objShortcut.WorkingDirectory = objShell.ExpandEnvironmentStrings(p_strWorkingDirectory)
 End If

 objShortcut.Save
End Function
h1

Self Installing VB Scripts

18/07/2009

I like using VB Scripts and the Windows Script Host to resolve issues as it has proved quick and flexible for most tasks.  One thing I have found problematic on occasion is the installation of the scripts on a user’s PC.  It takes time to do if it is not being deployed using logon scripts, group policy, etc.  To this end I decided to write a function to allow a script to install itself.   The script below is primarily made up from three functions/routines.

Starting from the bottom, the BuildFolderPath() function just takes a file path and adds a backslash to the end if it needs one.

The next function is a little more interesting.  ScriptPath() returns the folder in which the script is being run from.  Note that if it is being run from a shortcut it is the target folder.  This script works by removing the name of the script from the full file path of the script file.

The last chunk of code is the sub routine InstallScript() and does the majority of the work so to speak.  This checks to see if the script is in the correct location.  If it is not it asks the user for permission to copy it from the current location to the one against which it checked.  This does of course rely on the user having appropriate file permissions to copy the script there in the first place, but if we need anything more then some sort of executable or direct access by an administrator is always going to be best otherwise you’ll be handing out administrator passwords verbally or in plain text in script files.  Once installed (or if the user chose not to install) there is a bit of feedback to the user and the script ends.  If the script is running from the right location, no further action is taken and the script continues.

Option Explicit

Const INSTALL_TO = "%UserProfile%\SendTo"

InstallScript INSTALL_TO

Msgbox "The script is being run from the right place..."

'This routine checks if the script is in the right folder location and if it
'isn't it will give the user the option of copying the script to the right location
Sub InstallScript(p_strInstallTo)
 Dim strInstallToFolder
 Dim objShell, objFSO

 'Set the installation folder
 Set objShell = CreateObject( "WScript.Shell" )
 strInstallToFolder = BuildFolderPath(objShell.ExpandEnvironmentStrings(p_strInstallTo))

 If Not strInstallToFolder = ScriptPath Then
 'Copy the script to the correct folder if the user agrees
 If Msgbox ("This script is not being run from the expected location." & vbCrLf & _
 "Would you like to install it to " & strInstallToFolder & " now?", _
 vbYesNo + vbQuestion, "Install Script") = vbYes Then

 Set objFSO = CreateObject("Scripting.FileSystemObject")
 objFSO.CopyFile WScript.ScriptFullName, strInstallToFolder & WScript.ScriptName

 MsgBox "The script has been installed/updated.", vbOkOnly & vbInformation, "Install Complete"
 Else
 MsgBox "The script has not been installed and will not run.", vbOkOnly + vbExclamation, "Install Cancelled"
 End If

 'Finish the script at this point
 WScript.Quit
 End If

End Sub

'This function returns the path which the script is running from
Function ScriptPath()
 ScriptPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
End Function

'This function just adds a trailing backslash if there isn't one
Function BuildFolderPath(p_strPath)
 If Right(p_strPath,1) = "\" Then
 BuildFolderPath = p_strPath
 Else
 BuildFolderPath = p_strPath & "\"
 End If
End Function

An interesting thing to note is that I’ve passed folder paths through the ExpandEnvironmentStrings() function.  This converts any environment variables in the string and allows them to be converted to their full path.  If you want to know more about environment variables I’d recommend a few minutes browsing this page on environment variables in Windows XP.

So all you have to do is decide on a folder where you want the script run from and then pop these functions into your script.  Personally I like to create shortcuts for scripts too (e.g. in the user’s send to folder, start menu or the desktop).  I’ll be posting a little later on how to add that to your toolbox – particularly useful for installations like this.

h1

A portable web server where I want it

13/07/2009

I’ve been tinkering recently with some things around portable applications which I will probably post more about in the hopefully not too distant future.  One of the things I’ve been trawling around for and busily configuring are various free web server packages that are “portable”.  Unfortuately it has taken a fair bit of effort to actually track down and configure something that is portable enough.   The problem for me is that I have a particular need to locate the web server within a particular sub folder and in the case of most software packages this is not an option.

Most implementations are based around Apache (e.g. micro apache, XAMPP, TWAMP, WAMP, USB Web Server) but there are a few other options out there I also looked at  such as RITlabs TinyWeb.  Some of these experience problems when they are not installed to the root of a drive and others have a problem when the installation path contains spaces.  My desired installation path unfortunately is not in the root folder and does contain spaces which ruled out most of the options.

I found two options that did seem to cope with the path.  These were WAMP and USB Web Server.  My preference is for USB Web Server and I’ll explain why…

When installed my WAMP system tray application did not match up to the one shown on the web site.  Even though it was running, the status told me that it wasn’t and there was no option to stop/start individual applications such as Apache and MySQL.  Moreover WAMP comes as an installation package which adds an entry into the registry – at least for the add/remove software entry … which seems blatantly false for something that is supposed to be portable.

USB Web Server on the other hand does work as expected and does allow me to stop and start specific web related services and even to move it to a different folder (you have to close it in between).  The one thing I would like is to be able to specify which web services it should run at start-up.  It runs Apache, MySQL and SMTP by default, but I’d really just like to run Apache and free up my RAM without specifically having to manually turn them off.  I tried various renames of folders and changing of settings, but everything I tried either had no effect or stopped Apache running as well.  I’m guessing that there’s some sort of start-up chain at work rather than independent requests.  But maybe someone else knows what to change to address this or perhaps the developer will provide this option in a future release.

So if you want a truly portable web server try USB Web Server.