Posts Tagged ‘VB Script’

h1

Skype and Web Servers

08/08/2009

I briefly had a problem with the web server that’s running my personal wiki yesterday.  Try as I might it just wouldn’t start-up.  I hadn’t had a problem before and after three or four attempts at clicking the start option I took a step back and surveyed my machine – a useful option in many circumstances.

I spotted the culprit straight away as I had come across the issue before – Skype.  When Skype starts up if the default port (51907 on my current instance of Skype) is unavailable (because another application is using it or more likely a firewall is blocking it) it tries to set-up on a port that is more likely to be available – 80 or 443 which are used by web servers for insecure and secure traffic respectively.

Whilst there is an advanced option in Skype to force it away from these two alternative ports often there are no other convenient ones to choose from.  This is because it is good practice security to block unused ports and Skype isn’t always a top runner so to speak on ports to have open.

The moral of the story is simple – start your web server before you start Skype.

However this got me to wondering what else might pick a port that could conflict and how would you know what it was without just systematically working through all the applications and services you have running … which for me could be a large number.  My solution was to use some DOS commands.  NETSTAT can be used to examine what ports are in use by what processes.  TASKLIST can be used then to identify the executable.

To smooth things over a little I’ve concocted a little script to automate this process.  It’s really just a bit of a nicer front end to the whole thing and whilst several command prompt windows may briefly flash up as it runs, the script will take in the information from the commands and process them to filter out the pertinent information.

Option Explicit

Dim objCommandPort
Dim astrResults
Dim astrInfoLine
Dim strOutput
Dim strPortCommand
Dim strPID
Dim strApps

strPortCommand = "netstat -ano" 

set objCommandPort = New clsDOSCommandExecutor

objCommandPort.ExecuteCommand(strPortCommand)
strOutput = objCommandPort.GetOutput
astrResults = split(strOutput, vbCrLf)

strApps = "PORT: APPLICATION" & vbCrLf
AssessPort "80"
strApps = strApps & vbCrLf
AssessPort "443"

MsgBox strApps, vbInformation + vbOKOnly, "Applications using web server ports"

Sub AssessPort(p_strPort)
 Dim intCounter

 For intCounter = 1 to Ubound(astrResults)
 If inStr(astrResults(intCounter), ":" & p_strPort) > 0 Then
 astrInfoLine = split(astrResults(intCounter))
 strPID = astrInfoLine(ubound(astrInfoLine))
 strApps = strApps & vbCrLf & p_strPort & ": " & AppOnPort(strPID)
 End If
 Next
End Sub

Function AppOnPort(p_strPID)
 Dim objCommandPID
 Dim strPIDCommand
 Dim strPIDOut
 Dim astrPIDOut

 set objCommandPID = New clsDOSCommandExecutor
 strPIDCommand = "tasklist /fi ""PID eq " & p_strPID & """"

 objCommandPID.ExecuteCommand(strPIDCommand)

 strPIDOut = Right(objCommandPID.GetOutput, len(objCommandPID.GetOutput) - inStrRev(objCommandPID.GetOutput, "=" & vbcrlf))
 astrPIDOut = split(strPIDOut)

 AppOnPort = Replace(astrPIDout(0), vbCrLf, "")
End Function

Class clsDOSCommandExecutor
 Dim objShell, objExec
 Dim strCommand
 Dim strError
 Dim objError
 Dim objOutput
 Dim strOutput

 Sub ExecuteCommand(p_strCommand)
 strCommand = "cmd /c " & p_strCommand
 Set objShell = CreateObject("Wscript.Shell" )

 objShell.Exec(strCommand)

 Set objExec = objShell.Exec(strCommand)

 Do Until objExec.Status
 Wscript.Sleep 200
 Loop

 Set objError = objExec.StdErr
 strError = objError.ReadAll

 Set objOutput = objExec.stdOut
 strOutput = objOutput.ReadAll
 End Sub

 Function GetOutput()
 GetOutput = strOutput
 End Function

 Function GetError()
 GetError = strError
 End Function

 Function Failed()
 If strError = "" Then
 Failed = false
 Else
 Failed = true
 End If
 End Function
End Class
Advertisements
h1

DOS Command Class for VBScript

08/08/2009

This post has now been migrated to ThoughtAsylum.com.

Follow this link to go directly to the article.

h1

VBScript and Getting the Message Across

08/08/2009

This post has now been migrated to ThoughtAsylum.com.

Follow this link to go directly to the article.

h1

DropBox Freezes Out Explorer

05/08/2009

I’ve been using DropBox for some time now and I use it in some ways that I think are quite innovative and will be the subject of future blog posts.  However in the last few days I’ve been having a few problems with Windows Explorer taking forever (well over a minute or two at times) to respond and it responds very sluggishly.  I’ve managed to track the problem down to DropBox misbehaving.

I’m running version 0.6.507.0 as I have been for some time, but something somewhere has started causing these little fits.  The solution is simple.  Bring up task manager, find the dropbox.exe process and kill it.  Then navigate to the start up programs on the Windows start menu and start Dropbox.

Actually whilst that’s simple it all seemed a bit too much of a hassle for lazy me so I wrote a little script to do it.

Option Explicit
Const DROPBOX_FOLDER = "C:\Program Files\Dropbox\"
Const DROPBOX_EXE = "Dropbox.exe"

KillProcess(DROPBOX_EXE)
StartApplication(DROPBOX_FOLDER & DROPBOX_EXE)

Sub KillProcess(p_strProcess)
 Dim objService, objProcess
 Dim colProcesses
 Dim strProcessKill

 Set objService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

 Set colProcesses = objService.ExecQuery("Select * from Win32_Process Where Name = '" & p_strProcess & "'")
 For Each objProcess in colProcesses
 objProcess.Terminate()
 Next
End Sub

Sub StartApplication(p_strAppPath)
 Dim objShell
 Set objShell = WScript.CreateObject( "WScript.Shell" )
 objShell.Run("""" & p_strAppPath & """")
End Sub

Pop a shortcut to the script on your desktop and you should be able to access it quick enough – or even quicker if you set a shortcut key combination on it.

The script uses a couple of fairly standard little routines.  One kills the process and the other starts the application.  If you happen to have DropBox installed in an unusual location (mine is in the default), then just amend the first constant.

You may see two icons

You may see two icons

NB: You’ll probably notice that after the script ruins you’ll have two DropBox icons in your system tray.  If you move your mouse pointer over the two icons the one for the now terminated process will disappear.

h1

Filter files into silo folders

31/07/2009

Recently a number of my friends have hard hard drive failures and have asked me to retrieve data from their hard drives.  I’ve been reasonably successful in each case recovering well over 95% of the data in each case – thanks not to me but the proliferation of free data recovery tools out there on the Internet.

The last one I’ve been working on however has had a particularly mangled folder structure such that a large number of the recovered files haven’t been able to be assigned the correct file name and folder.  The recovery software has auto generated file names and placed them in a single folder … all 245,528 of them.  As you might expect Windows isn’t all that happy with that many files in a single folder when you want to interact with them.

Windows Explorer is incredibly slow to interact with this amount of content and even using SnowBird the speed of access was prohibitive.  I had a need to somehow rearrange the files into some other structure and since the naming is random, the folder structure does not need to be particularly meaningful – just practical to sort through.  Given there was no (free) software up to the job and being the consummate IT support chap I am  I decided to script my way around the problem.

I wrote a VB Script to process the folder of files and to “silo” the files off into groups within other folders.  A couple of constants at the top of the script tell it which folder to process and how many files to put into each folder.  The script then calculates how many folders to create and starts to process them.  I’ve used my generic progress window to show what file is being moved and how far through the process the script is.

The script doesn’t contain anything particularly nifty that hasn’t already been covered in one of my previous VB Script posts, so rather than displaying the source code here I’ve just provided it as a download:

h1

A Ceiling Function for VB Script

31/07/2009

This post has now been migrated to ThoughtAsylum.com.

Follow this link to go directly to the article
.

h1

A Flexible Progress Window in VBScript

19/07/2009

This post has now been migrated to ThoughtAsylum.com.

Follow this link to go directly to the article.