Template for Command-Line utility

P

Phil Hellmuth

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.
 
K

kimiraikkonen

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
 
T

Tom Shelton

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
 
D

Dennis

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.
 
T

Tom Shelton

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(inputFile, FileMode.Open, FileAccess.Read), _
outFile As New FileStream(ouputFile, FileMode.Create, FileAccess.Write)

bytesRead = inFile.Read(inBuffer, 0, inBuffer.Length)
while bytesRead > 0
for i as integer = 0 to bytesRead - 1
if inbuffer(i) > 128 then
outBuffer(bytesWritten) = inbuffer(i)
bytesWritten += 1
end if
next i
outFile.Write(outBuffer, 0, bytesWritten)
bytesRead = inFile.Read(inBuffer, 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
 
T

Tom Shelton

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top