Posts Tagged ‘Command Line’

h1

ENScript – Scripting Evernote in Windows

21/08/2009

This post has now been migrated to ThoughtAsylum.com.

Follow this link to go directly to the article.

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

Command Line Utility to Create Test Files

31/07/2009
Using the utility on the command line

Using the utility on the command line

In the past I’ve used FSUTIL to create large test files of data to process.  Unfortunately you need administrator permissions to use this utility.  I decided to write a command line utility (called “Make Test File” but abbreviated to MTF.EXE) to allow non-administrators to do the same.  It’s written in VB.NET so it isn’t as quick as FSUTIL, but as they say “it does the job”, and it fulfilled my immediate needs.

It takes two parameters – the size of the file (in bytes) and the file path to write the file to.  To see an example of how to use it and the output click on the thumbnail image on the right.  The resulting file just contains zeros.

If there’s any demand I may look into additional features such as being able to specify what the file is filled with or creation of multiple files using some sort of incremental naming convention.

h1

Continuous Ping

31/01/2009

Sometimes when testing network connectivity to a server on the local LAN or a web site I use the ubiquitous PING to do the job.  If I need to do it repeatedly I tend to write a quick DOS batch file (a text based script with a file extension of BAT or CMD).

For example this script would repeatedly ping localhost – the name used for the machine the script is being run on.

@echo off
:START
Ping -n 1 localhost
Goto START

A few years ago I even extended this to include a ping count but it didn’t really add much. I decided it would be more useful to have something that at least gave an indication of how many “good and bad” pings had occurred. At the same time I also found that I wasn’t always necessarily at my computer as I might be tinkering with cables and other networking gear so having some sort of audible cue as to how the pings were going.

I put together the following VB Script to do just this. It hasn’t been tested extensively and I’m sure that there could be a bit more validation. When the script is run it asks for the host to ping and a time at which to stop pinging. It then opens up an Internet Explorer window that displays the information about the pinging.

Option Explicit

'Settings:
'- Number of PC speaker beeps on ping successes/failures
Const BEEPS_ON_PING = 1
Const BEEPS_ON_NO_PING = 3
'- Milliseconds of pause between pings
Const PAUSE_MS = 200
'- Milliseconds pause before automatically closing the progress window
Const AUTO_CLOSE = True
Const FINAL_PAUSE = 5000

'Variable definitions
Dim strHost
Dim dtEnd
Dim objShell, objExplorer
Dim intSuccessfulPingCount, intUnsuccessfulPingCount

'Initialise
Set objExplorer = CreateObject("InternetExplorer.Application")

objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Left = 0
objExplorer.Top = 0
objExplorer.Width = 400
objExplorer.Height = 400
objExplorer.Document.Body.Style.Cursor = "default"
intSuccessfulPingCount = 0
intUnsuccessfulPingCount = 0


'If we don't have a host then ask for one
If Wscript.Arguments.Count < 1 Then
	strHost = InputBox("Please enter the host to ping", "Enter Host", "localhost")
Else
	strHost = Wscript.Arguments(0)
End If

'Double check what we have
If Len(strHost)  0
	'Carry out the ping
	If Ping(strHost, False) = True Then
		'Ping successful
		intSuccessfulPingCount = intSuccessfulPingCount + 1
		Beep(BEEPS_ON_PING)
		WScript.Sleep(PAUSE_MS)
	Else
		'Ping unsuccessful
		intUnsuccessfulPingCount = intUnsuccessfulPingCount + 1
		Beep(BEEPS_ON_NO_PING)
		WScript.Sleep(PAUSE_MS)
	End If
	UpdatePings "<p><b>RUNNING...</b></p>"
Wend

'Finalise
objExplorer.Document.Body.Style.Cursor = "default"
If AUTO_CLOSE Then
	UpdatePings("<p><b>AUTO CLOSING...</b></p>")
	Wscript.Sleep FINAL_PAUSE
	objExplorer.Quit
Else
	UpdatePings("<p><b>PING CYCLE COMPLETED</b></p>")
End If


'Update the display of pings
Sub UpdatePings(p_strSuffix)
	Dim strDisplay

	strDisplay = ""

	strDisplay = strDisplay & "<p>"
	strDisplay = strDisplay & "Finish pinging @ "
	strDisplay = strDisplay & dtEnd
	strDisplay = strDisplay & "<br />"
	strDisplay = strDisplay & "Currently ... "
	strDisplay = strDisplay & Now()
	strDisplay = strDisplay & "</p>"

	strDisplay = strDisplay & "<hr>"

	strDisplay = strDisplay & "<p>"
	strDisplay = strDisplay & "<table border='0'>"
	strDisplay = strDisplay & "<tr>"
	strDisplay = strDisplay & "<td><font color='green'>Successful pings</font></td>"
	strDisplay = strDisplay & "<td><font color='green'>"
	strDisplay = strDisplay & intSuccessfulPingCount
	strDisplay = strDisplay & "</font></td>"
	strDisplay = strDisplay & "</tr>"
	strDisplay = strDisplay & "<tr>"
	strDisplay = strDisplay & "<td><font color='red'>Unsuccessful pings</font></td>"
	strDisplay = strDisplay & "<td><font color='red'>"
	strDisplay = strDisplay & intUnsuccessfulPingCount
	strDisplay = strDisplay & "</font></td>"
	strDisplay = strDisplay & "</tr>"
	strDisplay = strDisplay & "</table>"
	strDisplay = strDisplay & "</p>"

	strDisplay = strDisplay & "<hr>"

	strDisplay = strDisplay & "<p>"
	strDisplay = strDisplay & "Auto Close Enabled: "
	strDisplay = strDisplay & AUTO_CLOSE
	strDisplay = strDisplay & "</p>"

	strDisplay = strDisplay & "<hr>"

	strDisplay = strDisplay & p_strSuffix

	objExplorer.Document.Body.InnerHTML = strDisplay
End Sub


'This function pings the specified host
Function Ping(p_strHost, p_boolDisplay)
	Dim objPing, objStatus

	Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery ("select * from Win32_PingStatus where address = '" & p_strHost & "'")

	For Each objStatus in objPing
		If IsNull(objStatus.StatusCode) or objStatus.StatusCode0 then
			Ping = False
			If p_boolDisplay Then
				WScript.Echo "Status code is " & objStatus.StatusCode
			End If
		Else
			Ping = True
			If p_boolDisplay Then
				Wscript.Echo "Bytes = " & vbTab & objStatus.BufferSize
				Wscript.Echo "Time (ms) = " & vbTab & objStatus.ResponseTime
				Wscript.Echo "TTL (s) = " & vbTab & objStatus.ResponseTimeToLive
			End If
		End if
	Next
End Function 


'Beep the PC speaker a number of times equal to the p_intBeeps parameter
Function Beep(p_intBeeps)

	Dim objShell, intCount, strCommand

	'Check we have a valid number of beeps not fool proof but it will do for now
	Beep = False
	If IsNumeric(p_intBeeps) Then
		If p_intBeeps > 0 Then
			Beep = True
		End If
	End If

	If Beep Then
		'Build the command string to run
		strCommand = "cmd /c echo"
		For intCount = 1 to p_intBeeps
			strCommand = strCommand & " " & chr(007)
		Next

		'Run the command in the background
		Set objShell = Wscript.CreateObject("wscript.Shell")
		objShell.Run strCommand, 0

		Beep = True
	End If
End Function

There are also a few options in the script to specify numbers of beeps for successful and unsuccessful pings as well as auto closing of the Internet Explorer window and lengths of pauses between pings and before auto closing the window.

h1

Grab Web Page into NoteTab Text Editor

11/01/2009

NoteTab is a favoured text editor of mine and for a number of years I’ve used it for working with web pages and coding up HTML, PHP, Javascript and the like.  One of the best features of NoteTab (beyond the light version being free to download) is its powerful scripting language – creating what is known as NoteTab Clips.

Grab Web Page clip inputs

Grab Web Page clip inputs

One of my favourite clips is one that provides a basic integration with a command line utility to download a web page without having to go into your web browser and view it.

So if you don’t already have them download the following free pieces of software:

  • NoteTab Light – text editor with scripting capabilities.
  • wGet – command line utility for downloading files from the Internet.

The two clips below should be copied and pasted into an appropriate clip library in NoteTab (see the NoteTab help file on how to do this – it’s very straight forward).  Run the “Grab Web Page” clip and after specifying what URL to download, where to download it to, how to display it (open the downloaded file, copy the file content into a new file or insert the file content at the current cursor position) and what log file to log the activity to.

H="Grab Web Page"
;Initialise variables
^!Set %FETCH_URL%=^?[URL of web page to retrieve=http://rebootit.wordpress.org]
^!Set %OUTPUT_PATH%=^?[(T=D)Directory to download file to=c:wGet Output]
^!Set %INSERTION_TYPE%=^?[(T=C)Downloaded file==_Open|Copy into new file|Insert at cursor]
^!Set %LOG_PATH%=^?[(T=S;F="Log Files (*.log)|*.log")File to log download progress to=c:wGet Outputoutput.log]
^!Set %WGET_PATH%=^?[Path to wGet.exe=C:Program FilesCommand line Utilitieswget.exe]

;Remove trailing backslash if it exists - wGet doesn't like these
^!If "^$StrCopyRight("^%OUTPUT_PATH%";1)$"="" NEXT Else SKIP
^!Set %OUTPUT_PATH%=^$StrDeleteRight("^%OUTPUT_PATH%";1)$

;Create the output directory
;(If it exists it won't do any harm)
^!MkDir ^%OUTPUT_PATH%

;Fetch page
^!CmdShow MINIMIZE
^!Set %FETCH_OUTPUT%=^$GetOutput("^%WGET_PATH%" -P"^%OUTPUT_PATH%" -nv -o"^%LOG_PATH%" "^%FETCH_URL%")$

;Get the name the retrieved file was named as
^!Set %OUTPUT_FILE_PATH%=^$GetFileText(^%LOG_PATH%)$
^!Set %OUTPUT_FILE_PATH%=^$StrDeleteLeft("^%OUTPUT_FILE_PATH%";^$Calc(^$StrPos("-> ";"^%OUTPUT_FILE_PATH%";TRUE)$+3)$)$
^!Set %OUTPUT_FILE_PATH%=^$StrCopyLeft("^%OUTPUT_FILE_PATH%";^$Calc(^$StrPos(""";"^%OUTPUT_FILE_PATH%";TRUE)$-1)$)$
^!Set %OUTPUT_FILE_PATH%=^$StrReplace("/";"";"^%OUTPUT_FILE_PATH%";TRUE;TRUE)$

;Output the contents of the downloaded file
^!If "^%INSERTION_TYPE%"="Open" NEXT Else SKIP
^!Open ^%OUTPUT_FILE_PATH%
^!If "^%INSERTION_TYPE%"="Copy into new file" NEXT Else SKIP
^!Clip "New From Text" ^$GetFileText(^%OUTPUT_FILE_PATH%)$
^!If "^%INSERTION_TYPE%"="Insert at cursor" NEXT Else SKIP
^!InsertFile ^%OUTPUT_FILE_PATH%

H="_New From Text"
^!Menu File/New
^!InsertCode ^&