Showing posts with label Text file manipulation. Show all posts
Showing posts with label Text file manipulation. Show all posts

Thursday, 9 May 2019

DeDup.exe Removes duplicate lines from StdIn and writes to StdOut

REM DeDup.bat
REM This file compiles DeDup.vb to DeDup.exe
REM DeDup.exe Removes duplicate lines from StdIn and writes to StdOut
REM To use
REM DeDup  < inputfile.txt > outputfile.txt
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\DeDup.exe" "%~dp0\DeDup.vb" /verbose
pause










'DeDup.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module DeDup
Sub Main
        Dim Dict as Object
        Dict = CreateObject("Scripting.Dictionary")
        Dim Line As Object
        Line=Console.readline
        Do Until Line = vbnull
                On Error Resume Next
                Dict.Add(Line, "")
                On Error Goto 0
                Line=Console.readline
        Loop


        For Each thing in Dict.Keys()
                Console.writeline(thing)
        Next
End Sub
End Module










Monday, 6 May 2019

Tee.exe Reads from StdIn and writes to StdOut and a file.

REM Tee.bat
REM This file compiles Tee.vb to Tee.exe
REM Tee.exe Reads from StdIn and writes to StdOut and a file.
REM To use
REM    Tee Filename
Rem     Filename - name of file
Rem
Rem Example
Rem
Rem     tee "%userprofile%\Desktop\winini.txt" < "%windir%\win.ini"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\Tee.exe" "%~dp0\Tee.vb"
pause








-------------------------------------------------------------------------------------





'Tee.vb

Imports System

Imports System.IO

Imports System.Runtime.InteropServices

Imports Microsoft.Win32

 

Public Module Tee

Sub Main

        Dim Line as Object

        Dim FSO as Object

        Dim File as Object

        On Error Resume Next

        Fso = CreateObject("Scripting.FileSystemObject")

        File = Fso.CreateTextFile(Replace(Command(), """", ""), True)

        If err.number <> 0 then

                Console.WriteLine("Error: " & err.number & " " & err.description & " from " & err.source)

                err.clear

        Else

                Line=Console.readline

                Do Until Line = vbnull

                        File.Writeline(Line)

                        Console.WriteLine(Line)

                        Line = Console.ReadLine

                Loop

        End If

End Sub       

End Module

Sunday, 5 May 2019

Cut.exe Removes specified lines from top or bottom of streams from StdIn and writes to StdOut.


REM Cut.bat
REM This file compiles Cut.vb to Cut.exe
REM Cut.exe Removes specified from top or bottom of lines from StdIn and writes to StdOut
REM To use
REM     cut {t|b} {i|x} NumOfLines
Rem Cuts the number of lines from the top or bottom of file.
Rem     t - top of the file
Rem     b - bottom of the file
Rem     i - include n lines
Rem     x - exclude n lines
Rem
Rem Example - Includes first 5 lines Win.ini
Rem
Rem     cut t i 5 < "%systemroot%\win.ini"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\Cut.exe" "%~dp0\Cut.vb"
pause


----------------------------------------------------------------------





'Cut.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module Cut
Sub Main
    Dim Arg() As Object
    Dim RS as Object
    Dim LineCount as Object
    Dim Line as Object
    Arg = Split(Command(), " ")
    rs = CreateObject("ADODB.Recordset")
    With rs
        .Fields.Append("LineNumber", 4)
        .Fields.Append("Txt", 201, 5000)
        .Open
        LineCount = 0
        Line=Console.readline
        Do Until Line = vbnull
            LineCount = LineCount + 1
            .AddNew
            .Fields("LineNumber").value = LineCount
            .Fields("Txt").value = Console.readline
            .UpDate
            Line = Console.ReadLine
        Loop

        .Sort = "LineNumber ASC"

        If LCase(Arg(0)) = "t" then
            If LCase(Arg(1)) = "i" then
                .filter = "LineNumber < " & LCase(Arg(2)) + 1
            ElseIf LCase(Arg(1)) = "x" then
                .filter = "LineNumber > " & LCase(Arg(2))
            End If
        ElseIf LCase(Arg(0)) = "b" then
            If LCase(Arg(1)) = "i" then
                .filter = "LineNumber > " & LineCount - LCase(Arg(2))
            ElseIf LCase(Arg(1)) = "x" then
                .filter = "LineNumber < " & LineCount - LCase(Arg(2)) + 1
            End If
        End If

        Do While not .EOF
            Console.writeline(.Fields("Txt").Value)
            .MoveNext
        Loop
    End With

End Sub   
End Module