Hi all,
With your help I have a perfectly working console application.
Now I need to do it a bit more flexible.
So far I have the file paths for all files involved in the applications execution hardcoded.I need to be able to execute the console application from anywhere. i.e the file paths to change dynamically .Meaning to be able to pass the input file names , output file names for the application fro cmd prompt.Is that possible.
I am posting the code :
Imports System
Imports System.IO
Imports System.Xml
Module Module1
Public Sub ImportXML(ByVal FileName As String, ByRef Names() As String, _
ByRef CountStart() As Integer, ByRef CountEnd() As Integer)
Dim FStream As FileStream
Dim XReader As System.Xml.XmlReader
Dim XNav As XPath.XPathNavigator
Dim XDoc As XPath.XPathDocument
Dim XIter As XPath.XPathNodeIterator
Dim TempStr As String
Dim TempInt As Integer
Dim TempCount As Integer
TempCount = 0
FStream = New FileStream(FileName, FileMode.Open, FileAccess.Read)
XReader = New Xml.XmlTextReader(FStream)
XDoc = New XPath.XPathDocument(XReader)
XNav = XDoc.CreateNavigator()
XNav.MoveToRoot()
XIter = XNav.Select("descendant::Strings/String")
While XIter.MoveNext
ReDim Preserve Names(TempCount)
ReDim Preserve CountStart(TempCount)
ReDim Preserve CountEnd(TempCount)
' Name
TempStr = XIter.Current.SelectSingleNode("descendant::Name").Value
Names(TempCount) = TempStr
' CountStartPosition
TempStr = XIter.Current.SelectSingleNode("descendant::CountStartPosition").Value
If Integer.TryParse(TempStr, TempInt) Then
CountStart(TempCount) = TempInt
Else
CountStart(TempCount) = -1
' -1 indicates invalid value
End If
' CountEndPosition
TempStr = XIter.Current.SelectSingleNode("descendant::CountEndPosition").Value
If Integer.TryParse(TempStr, TempInt) Then
CountEnd(TempCount) = TempInt
Else
CountEnd(TempCount) = -1
' -1 indicates invalid value
End If
TempCount += 1
End While
FStream.Close()
End Sub
Public Function MatchString(ByVal OneLine As String, ByVal StringToMatch As String, ByVal StartPos As Integer, ByVal EndPos As Integer) As Boolean
If String.IsNullOrEmpty(StringToMatch) OrElse String.IsNullOrEmpty(OneLine) Then
Return False
End If
If StartPos < 0 OrElse StartPos >= EndPos Then
Return False
End If
If StringToMatch.Length <> ((EndPos - StartPos) + 1) Then
Return False
End If
If StartPos >= OneLine.Length OrElse EndPos > OneLine.Length Then
Return False
End If
If String.Compare(OneLine.Substring(StartPos, (EndPos - StartPos) + 1), StringToMatch, True) = 0 Then
Return True
End If
Else
' No match
Return False
End If
End Function
Sub Main()
Dim Names(0) As String
Dim CountStart(0) As Integer
Dim CountEnd(0) As Integer
ImportXML("FilePath.xml", Names, CountStart, CountEnd)
Console.Write("Reading Started")
Console.Write(vbCrLf)
Dim FileToSplit As FileStream
Try
FileToSplit = New FileStream("C:\FileToSplit.txt", FileMode.Open)
Catch ex As Exception
'Couldn`t find the input file.Please check if the input file exists!
Exit Sub
End Try
Dim stream As New StreamReader(FileToSplit)
stream.BaseStream.Seek(0, SeekOrigin.Begin)
Dim FileMatch As FileStream
Try
FileMatch = New FileStream("C:\FileMatch.txt", FileMode.OpenOrCreate)
Catch ex As Exception
'Couldn't open the output file.
Exit Sub
End Try
Dim filetosplitstream As New StreamWriter(FileMatch)
Console.SetIn(stream)
Console.SetOut(filetosplitstream)
Dim i As Integer
' Loop counter
Dim bMatch As Boolean
Dim Line As String
' Indicates if we found a match
While stream.Peek() > -1
Line = Console.ReadLine()
bMatch = False
' Loop xml file
For i = 0 To Names.GetUpperBound(0)
' Check for a match
If MatchString(Line, Names(i), CountStart(i), CountEnd(i)) Then
' It's a match. Set the flag and exit inner loop
bMatch = True
Exit For
End If
Next i
If bMatch Then
' We had a match so write the line to the output stream
Console.WriteLine(Line)
End If
End While
stream.Close()
filetosplitstream.Close()
MsgBox("Finished Writing the File")
End Sub
End Module
Ok so bits that are in green I need to be able to pass from command line , like
applicationname <filetosplit> <xml > <filematch>
I read that I can use GetFullPath method , but I don`t think that this will work here .
I will have to be able to change name of the input file, output file and xml file name.Structure of the all Three will remain the same.
If anyone can give a clue how that can be done will be greatly appreaciated.
Would that be a kind of function or is there any orher way of declaring the files paths to be read from the cmd line each time?
Thanks in advance:)