Template for Command-Line utility

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Hellmuth

    Template for Command-Line utility

    I'm fairly new to VS/VB.net. I want to create a command line utility
    that uses two parameters, reads a file (first parm), and writes to
    another file (2nd parm). Which VS/VB.net template should I use to get
    started? Thanks in advance.
  • kimiraikkonen

    #2
    Re: Template for Command-Line utility

    On Oct 29, 7:01 pm, Phil Hellmuth <bill...@pacbel l.netwrote:
    I'm fairly new to VS/VB.net.  I want to create a command line utility
    that uses two parameters, reads a file (first parm), and writes to
    another file (2nd parm).  Which VS/VB.net template should I use to get
    started?  Thanks in advance.
    Start New Project, Choose "Console Application".

    BTW, is all your intention to copy files using like
    System.IO.File. Copy?

    Thanks,

    Onur Guzel

    Comment

    • Tom Shelton

      #3
      Re: Template for Command-Line utility

      On 2008-10-29, Phil Hellmuth <billort@pacbel l.netwrote:
      I'm fairly new to VS/VB.net. I want to create a command line utility
      that uses two parameters, reads a file (first parm), and writes to
      another file (2nd parm). Which VS/VB.net template should I use to get
      started? Thanks in advance.
      You'll want to create a Console application. Your main should probably
      look like this:

      Public Function Main (ByVal args() As String) As Integer
      ' args contains all the arguments passed to your application
      ' assuming that the arguments are correct (you'll want to put
      ' in validation/parsing code here...

      Dim inputFile As String = args(0)
      Dim outputFile As String = args(1)

      ' do cool stuff

      ' You will most likely want to return an exit code
      ' 0 means no error, non-zero means a problem - you might
      ' want to have different codes for different problems, but
      ' that's up to you. You don't have to do this at all :)
      ' you could make Main a sub instead of a function, but
      ' most command line apps return an exit code to the OS -
      ' so it's a good idea to do the same.
      Return AnExitCode
      End Function

      HTH

      --
      Tom Shelton

      Comment

      • Phil Hellmuth

        #4
        Re: Template for Command-Line utility

        Thanks for your response. This really helps. Unfortunately, I'm stuck
        with what I'm trying to do next. I want to see if the files specified
        in the arguments exist, using the FileExists function. However, I don't
        know what reference to use to import System.File.IO (as I said, I'm new
        to this). Next, I want to read each byte in the input file and write it
        to the output file if it's less than 128 (ascii). Is this relatively
        simple?

        Tom Shelton wrote:
        On 2008-10-29, Phil Hellmuth <billort@pacbel l.netwrote:
        >I'm fairly new to VS/VB.net. I want to create a command line utility
        >that uses two parameters, reads a file (first parm), and writes to
        >another file (2nd parm). Which VS/VB.net template should I use to get
        >started? Thanks in advance.
        >
        You'll want to create a Console application. Your main should probably
        look like this:
        >
        Public Function Main (ByVal args() As String) As Integer
        ' args contains all the arguments passed to your application
        ' assuming that the arguments are correct (you'll want to put
        ' in validation/parsing code here...
        >
        Dim inputFile As String = args(0)
        Dim outputFile As String = args(1)
        >
        ' do cool stuff
        >
        ' You will most likely want to return an exit code
        ' 0 means no error, non-zero means a problem - you might
        ' want to have different codes for different problems, but
        ' that's up to you. You don't have to do this at all :)
        ' you could make Main a sub instead of a function, but
        ' most command line apps return an exit code to the OS -
        ' so it's a good idea to do the same.
        Return AnExitCode
        End Function
        >
        HTH
        >

        Comment

        • Dennis

          #5
          Re: Template for Command-Line utility

          On Wed, 29 Oct 2008 11:13:35 -0700, Phil Hellmuth <billort@pacbel l.net>
          wrote:
          >Thanks for your response. This really helps. Unfortunately, I'm stuck
          >with what I'm trying to do next. I want to see if the files specified
          >in the arguments exist, using the FileExists function. However, I don't
          >know what reference to use to import System.File.IO (as I said, I'm new
          >to this). Next, I want to read each byte in the input file and write it
          >to the output file if it's less than 128 (ascii). Is this relatively
          >simple?
          Have you tried the Help for FileExists? Often there will be a code
          snippet to show how it works. Also try googling for sample code.

          --

          Dennis

          Comment

          • Tom Shelton

            #6
            Re: Template for Command-Line utility

            On 2008-10-29, Phil Hellmuth <billort@pacbel l.netwrote:
            Thanks for your response. This really helps. Unfortunately, I'm stuck
            with what I'm trying to do next. I want to see if the files specified
            in the arguments exist, using the FileExists function. However, I don't
            know what reference to use to import System.File.IO (as I said, I'm new
            to this). Next, I want to read each byte in the input file and write it
            to the output file if it's less than 128 (ascii). Is this relatively
            simple?
            >
            Actually, it is :) Here is some air-code, this untested, and is infact not
            likely to work the first try... I might have my indexing off or some minor
            syntax issues, but it should be enough to get you started :)

            Option Explict On
            Option Strict On

            Imports System
            Imports System.IO

            Public Function Main (ByVal args() As String) As Integer
            ' args contains all the arguments passed to your application
            ' assuming that the arguments are correct (you'll want to put
            ' in validation/parsing code here...

            Dim inputFile As String = args(0)
            Dim outputFile As String = args(1)
            Dim anExiteCode As Integer = 0
            Dim inBuffer() As Byte = new Byte(2048)
            Dim outBuffer() As Byte = new Byte(2048)
            Dim bytesRead As Integer = 0
            dim bytesWritten = 0

            Try
            Using inFile As New FileStream(inpu tFile, FileMode.Open, FileAccess.Read ), _
            outFile As New FileStream(oupu tFile, FileMode.Create , FileAccess.Writ e)

            bytesRead = inFile.Read(inB uffer, 0, inBuffer.Length )
            while bytesRead 0
            for i as integer = 0 to bytesRead - 1
            if inbuffer(i) 128 then
            outBuffer(bytes Written) = inbuffer(i)
            bytesWritten += 1
            end if
            next i
            outFile.Write(o utBuffer, 0, bytesWritten)
            bytesRead = inFile.Read(inB uffer, 0, inBuffer.Length )
            end while
            End Using
            Catch ex As Exception
            anExitCode = 1
            End Catch

            ' You will most likely want to return an exit code
            ' 0 means no error, non-zero means a problem - you might
            ' want to have different codes for different problems, but
            ' that's up to you. You don't have to do this at all :)
            ' you could make Main a sub instead of a function, but
            ' most command line apps return an exit code to the OS -
            ' so it's a good idea to do the same.
            Return anExitCode
            End Function

            Anyway, hth

            --
            Tom Shelton

            Comment

            • Tom Shelton

              #7
              Re: Template for Command-Line utility

              On 2008-10-29, Phil Hellmuth <billort@pacbel l.netwrote:
              Thanks for your response. This really helps. Unfortunately, I'm stuck
              with what I'm trying to do next. I want to see if the files specified
              in the arguments exist, using the FileExists function. However, I don't
              know what reference to use to import System.File.IO (as I said, I'm new
              to this). Next, I want to read each byte in the input file and write it
              to the output file if it's less than 128 (ascii). Is this relatively
              simple?
              >
              Actually, i didn't notice your question about references :) You don't need to
              add any additional references - just add the Imports System.IO.

              --
              Tom Shelton

              Comment

              Working...