• 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    '//head.vbs
    '// Written by Richie Bartlett
    '// Purpose: Windows command supplement for the Linux "head" command
    '// (quick little "dirty" version)

    '// syntax: cscript /nologo /U head.vbs [lineCount] fileName.Ext
    '// alternate syntax: type TextSplit.vbs | cscript /nologo /U head.vbs
    '//
    '// This program also accepts piped data streams for processing.
    '// HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows Script Host\\Settings\\DisplayLogo=0

    On Error Resume Next

    Const verStr = "v0.13"
    Const ForReading = 1
    Dim stdFlag
    Dim lines
    Dim strRead
    Dim filePath
    Dim objFSO
    Dim objFile

    stdFlag=True 'assume stdIn pipe input
    lines = CInt(WScript.Arguments(0))
    If lines<1 And Len(WScript.Arguments(0))>1 Then
    lines =10 '// default to 10 lines
    filePath =WScript.Arguments(0)
    stdFlag =False
    End If 'no lineCount
    Err.Clear

    If WScript.Arguments.length>1 And Len(filePath)<1 Then
    filePath=WScript.Arguments(1)
    stdFlag =False
    End If ' arg

    ' WScript.Echo "lines= " & CStr(lines)
    ' WScript.Echo "filePath= " & filePath

    If stdFlag Then strRead=WScript.StdIn.ReadLine

    If Len(strRead)>0 Then
    stdFlag=True
    WScript.Echo strRead 'we echo here since readline increments to the next line on each read
    lines = lines-1
    End If '// strRead
    If Error<>0 Then stdFlag =False
    Err.Clear


    If stdFlag Then
    Do Until WScript.stdin.AtEndOfStream Or lines<1 Or Err.Number<>0
    strContents = WScript.stdin.ReadLine
    WScript.Echo strContents
    lines = lines-1
    Loop
    Else
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(filePath) Then
    Set objFile = objFSO.GetFile(filePath)
    If objFile.Size > 0 Then
    Set objReadFile = objFSO.OpenTextFile(filePath, ForReading)
    Do Until objreadfile.AtEndOfStream Or lines<1 Or Err.Number<>0
    strContents = objReadFile.ReadLine
    Wscript.Echo strContents
    lines = lines-1
    Loop
    objReadFile.Close
    Else
    Wscript.Echo "The file is empty."
    End If
    Else
    Wscript.Echo "File (" & filePath & ") is not found."
    End If 'file exist
    End If '// stdFlag

    Prev:
    tail.vbs
    Next:
    TextSplit.vbs