I have two properties
Property FirstDayOfCsv As String
Property LastDayOfCsv As String
I want that properties obtains min and max values from parsed csv file. Values are days in "dd" format from date column. Now my code looks like this:
Dim csv As String() = IO.File.ReadAllLines("my.csv").Skip(1).ToArray
For Each line In csv
Dim col = line.Split(";"c)
Dim days As Date = col(2)
FirstDayOfCsv = days.ToString("dd").Min
LastDayOfCsv = days.ToString("dd").Max
Next
Unfortunately Min and Max doesn't return values, which I need and I'm stucking here. How to get them?
One option that you have is to add the values to a collection, but converting the String values to Integer values first. Then once you're finished iterating over the lines you would call Min/Max.
Dim csv = IO.File.ReadAllLines("my.csv").Skip(1).ToArray()
Dim everyday = New List(Of Integer)()
For Each line In csv
Dim col = line.Split(";"c)
Dim days = col(2)
Dim daysAsInt As Integer
If (Integer.TryParse(days, daysAsInt)) Then
everyday.Add(daysAsInt)
End If
Next
FirstDayOfCsv = everyday.Min()
LastDayOfCsv = everyday.Max()
Related
The problem now is how would I be able to format the values being displayed in datagridview from textfiles.
I have retrieved values from looping through textfiles removed the first two strings. Now I want to add separators or change the format of the displayed value like, for example:
textfile lines: result:
01Sample - line1
022 - line2
0306212019 - line3 06/21/2019
041234567890 - line4 12,345,678.90
I have already tried this one changing the defaultcellstyle but since the values are from textfiles in a directory its not affecting the output
DataGridView1.Columns("Gross Sales").DefaultCellStyle.Format = "##,0"
Private Sub ReadTextFiles()
Dim dt As New DataTable
dt.Columns.Add("Date")
dt.Columns.Add("Gross Sales")
Dim Folder As New IO.DirectoryInfo("c:\test\")
Dim lstLines As New List(Of String)
For Each fileentries As String In Folder.GetFiles("s*", IO.SearchOption.AllDirectories).OrderByDescending(Function(x) x.Name).Select(Function(x) x.FullName)
lstLines.AddRange(File.ReadAllLines(fileentries))
Next
Dim i As Integer
Dim OuterLoopIterations As Integer = CInt(lstLines.Count / 22)
For iterations = 0 To OuterLoopIterations - 1
Dim row = dt.NewRow
For col = 0 To 21
row(col) = lstLines(i).Remove(0, 2) 'i have removed the first 2 characters of each string
i += 1
Next
dt.Rows.Add(row(2), row(5), row(12), row(13), row(14), row(15), row(7), row(8), row(11))
Next
DataGridView1.DataSource = dt
'the code i tried applying
DataGridView1.Columns("Gross Sales").DefaultCellStyle.Format = "##,0"
this is my expected result
Current datagrid view:
the result should be for date column: 06/07/2019
for the gross : 48,990.14
Edit:
I tried this one
Dim B As Double
Dim Folder As New IO.DirectoryInfo("c:\test\")
Dim lstLines As New List(Of String)
For Each fileentries As String In Folder.GetFiles("s*", IO.SearchOption.AllDirectories).OrderByDescending(Function(x) x.Name).Select(Function(x) x.FullName)
B = CDbl(Val(fileentries))
lstLines.AddRange(File.ReadAllLines(B))
Next
If you want to format something as a number then it has to be a number. That means that, for example, if you read the text "1234.5" from the file and you want to display it as 1,234.50 in your grid then you have to convert the String you read to a Double or Decimal. If you do that then the numeric format specifier you're using in the grid column will work.
I've been working on this assignment for class but ran into an issue when creating a string from three other strings. It creates a invoice number based on the first letter in the first and last name and the last 3 numbers of the zip code.
Dim split As String() = txtName.Text.Split(", ")
Dim last As String = split(0)
Dim first As String = split(1)
Dim invFirst = first.Substring(0, 1)
Dim invLast = last.Substring(0, 1)
Dim invZip = cityState.Substring(cityState.Length - 3)
Dim invNumber = invFirst + invLast + invZip
lstInvoice.Items.Add("Invoice Number: " + invNumber)
Instead of printing out AB123 it will print out just B123. I have tried using + and & and even tired converting all components to a string just to be sure it wasn't trying to treat the values as numbers or something.
Am I missing something like flushing the stream or casting them differently?
Split() returns an array. https://msdn.microsoft.com/library/tabh47cf(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
So you need to trim the strings. And then it will work.
https://dotnetfiddle.net/U5gvh5
Dim split As String() = txtName.Split(",")
Dim last As String = split(0).Trim()
Dim first As String = split(1).Trim()
i am writing simple program to get the sorted date . But it does not work.
Dim filepath As String = FileStr
Dim directoryPath As String = System.IO.Path.GetDirectoryName(filepath)
for Each file As String In System.IO.Directory.GetFiles(directoryPath)
Dates = {
Date.Parse(System.IO.Path.GetFileNameWithoutExtension(file))
}.ToList
Next
Dates.Sort()
ComboBox1.DataSource = Dates
It only show one date ..where there are more than 10 date. and also the loop is working
I declare the List as global
You're replacing the content of your list in every loop
Dim filepath As String = FileStr
Dim directoryPath As String = System.IO.Path.GetDirectoryName(filepath)
for Each file As String In System.IO.Directory.GetFiles(directoryPath)
Dates.Add(Date.Parse(System.IO.Path.GetFileNameWithoutExtension(file)))
Next
Dates.Sort(AddressOf SortDate)
ComboBox1.DataSource = Dates
UPDATE: Sorting issue
Then as a seperate function add:
Function SortDate(ByVal a As DateTime, ByVal b As DateTime)
Dim result As Integer = a.Year.CompareTo(b.Year)
If result = 0 Then
a.Month.CompareTo(b.Month)
If result = 0 Then
a.Day.CompareTo(b.Day)
End If
End If
Return result
End Function
Your loop replaced the value of Dates each time, so you get only one date.
Change the loop like this:
Dates = System.IO.Directory.GetFiles(directoryPath)
.Select(Function(file) Date.Parse(System.IO.Path.GetFileNameWithoutExtension(file)))
.ToList
I have a text file with hundreds of prospects that I'm trying to convert to csv for importing.
Format for whole document.
Prospect Name
Description
Website
Prospect Name
Description
Website
How would I write a vb program to loop through this to make a csv file. Every 4 lines is a new prospect.
Get your file contents as an IEnumerable(of String), and spin through it, adding a CSV row for every record (into a new list(of String)). Finally write the file contents. You'll probably want to add a header row.
Dim lstLines As IEnumerable(Of String) = IO.File.ReadLines("C:\test\ConvertToCSV.txt")
Dim lstNewLines As New List(Of String)
Dim intRecordTracker As Integer = 0
Dim strCSVRow As String = String.Empty
For Each strLine As String In lstLines
If intRecordTracker = 4 Then
intRecordTracker = 0
'Trim off extra comma.
lstNewLines.Add(strCSVRow.Substring(0, (strCSVRow.Length - 1)))
strCSVRow = String.Empty
End If
strCSVRow += strLine & ","
intRecordTracker += 1
Next
'Add the last record.
lstNewLines.Add(strCSVRow.Substring(0, (strCSVRow.Length - 1)))
'Finally write the CSV file.
IO.File.WriteAllLines("C:\Test\ConvertedCSV.csv", lstNewLines)
Dim count As Integer = -1, lstOutput As New List(Of String)
lstOutput.AddRange(From b In File.ReadAllLines("C:\temp\intput.txt").ToList.GroupBy(Function(x) (Math.Max(Threading.Interlocked.Increment(count), count - 1) \ 4)).ToList() Select String.Join(",", b))
File.WriteAllLines("c:\temp\test\output.txt", lstOutput)
How to calculate the number of rows in a file which we have uploaded. The file might be either a text file or a CSV or Excel file.
I'm using the folowing code to get the record count like this:
Dim FileCount = From lin1 As String In File.ReadAllLines(hidFilePath.Value.ToString())
Let dd = lin1.Count
Select dd
Dim file_count As Integer = FileCount.Count
but in some cases the count is wrong.
ReadAllLines returns a string array, so you can simply take the length of the array.
Dim file_count As Integer = _
File.ReadAllLines(hidFilePath.Value.ToString()).Length
EDIT: read question to fast and answered with a loop solution
You can set your linecount as an integer and have the reader read to the end of the file.
Dim sr As New StreamReader("file path here")
Dim lineCount As Integer = System.Text.RegularExpressions.Regex.Split(sr.ReadToEnd(), Environment.NewLine).Length
sr.Close()
You can use a count variable and loop through the file until theres nothing
''name count and set it to 0
Dim count As Integer
count = 0
Dim obj As StreamReader
obj = New StreamReader("C:\...\source.txt")
''loop through the file until the end
Do Until obj.ReadLine Is Nothing
count = count + 1
Loop
''close file and show count
obj.Close()
MessageBox.Show(count)