Take a Quick Look at a File

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes