Take a Quick Look at a File

The Windows Script Host (WSH) supports many useful features, including VBScript’s FileSystemObject object and the ability to drag and drop filenames. You can drag and drop a data files icon onto the script (or a shortcut to the script) to see the first ten lines of a file, or you can click on it to get an input box. You can specify any range of lines if you use arguments in the input box. The code gets the requested lines, puts them in a temporary file, and opens the temp file in Notepad. This utility can come in handy when you want to take a quick look at the layout of lines in a large file.

You can download the WSH from Microsoft’s Web site here. Be sure to download the latest release version if the shortcut doesnt activate with drag-and-drop. Save this code into a file with a VBS extension, create a shortcut on your desktop, then take a quick look at the files:

Dim sInputLine, sMain, sDim i, iP, iEndFileNameDim fso, tf, fDim nStartPos, iLineCntDim iPopupDelayDim varAr' Edit for your system!Const TempFile = "C:Temp	emp.txt"nStartPos = 1		' Default first line.iLineCnt = 10		' Default number of lines to show.iPopupDelay = 4 	' Default Popup display, in seconds.Set objArgs = WScript.Arguments' If drag and drop was used,' the argument will be the filename.If objArgs.Count > 0 Then	sInputLine = objArgs(0)Else	sInputLine = InputBox( _		"Enter full name of file:" & vbCrLf & vbCrLf _		& "Arguments allowed after the file name:" & _		vbCrLf & "     [number of lines to" & _		"show] [line to start at]" & vbCrLf & _		"Use single space for argument separator.", _		"Display Ten Lines of a File", "C:")	sInputLine = Trim(sInputLine)End If' Clean up as we go.Set objArgs = Nothing' If the cancel button was clicked, exit.If sInputLine = "" Then	DisplayMsg "No file name entered."	WScript.quit (0)End If' Get start of extension for parsing ' reference point.i = InstrRev(sInputLine, ".")' If no extension, exit gracefully.If i = 0 Then	DisplayMsg "The filename " & sInputLine & _		" has no extension."	WScript.quit (0)End If' Check to see If there are arguments at End of ' sInputLinei = InStr(i, sInputLine, " ")' first arg = iLineCnt' second arg = nStartPos (optional)If i > 0 Then	iEndFileName = i - 1	s = Trim(Mid(sInputLine, i))	If Len(s) > 0 Then		varAr = Split(s, " ")		If UBound(varAr) > 0 Then nStartPos = _			CLng(varAr(1))		iLineCnt = CInt(varAr(0))		s = ""	End If	sInputLine = Left(sInputLine, iEndFileName)End If' Use the scripting file system object to retrieve ' file lines.Set fso = WScript.CreateObject( _	"Scripting.FileSystemObject")' If the file doesn't exist, exit.If Not (fso.FileExists(sInputLine)) Then	DisplayMsg "The file " & sInputLine & _		" does not exit."	Set fso = Nothing	WScript.quit (0)End IfSet tf = fso.OpenTextFile(sInputLine)' Read iLineCnt file lines starting with line ' nStartPosi = 1: iP = 0Do While tf.AtEndOfStream <> True	sMain = tf.ReadLine	If i >= nStartPos Then		s = s & sMain & vbCrLf		iP = iP + 1	End If	i = i + 1	If iP >= iLineCnt Then Exit DoLooptf.Close' Save file lines string to a temporary file.Set f = fso.CreateTextFile(TempFile)f.Write (s)f.Close' Use the script host shell method to open the ' temporary file in editor.Set WshShell = WScript.CreateObject("WScript.Shell")WshShell.Run "notepad " & TempFileSet fso = NothingSet WshShell = NothingSub DisplayMsg(sMsg)	Set WshShell = _		WScript.CreateObject("Wscript.Shell")	WshShell.Popup sMsg, iPopupDelay, _		"Exiting Windows Script Host", _		vbOKOnly + vbInformation	Set WshShell = NothingEnd Sub
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS