how to a selection of data from a csv based on dates within the csv - vb.net

im very new to vb.net. im making a piece of software and im very nearly finished (its my first piece of standalone software). i have a trackbar next to a button, which im trying to use to control how large a selection of this csv file should be, and then it can download it to a seperate file. my issue is im unsure how to parse the file itself and assign variables and do some variable maths and get the selection i need. ive written some pseudocode here to sort of show what im trying to do. any help or pointing in the right direction of what im looking for would be brilliant, thankyou.
Private Sub TrackBar4_Scroll(sender As Object, e As EventArgs) Handles TrackBar4.Scroll
Label44.Text = TrackBar4.Value
Dim MonthSelection As Integer = 3
MonthSelection = Label44.Text
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If Label32.Text = "C:\" Then
MessageBox.Show("Please select Log from sign first")
Else
Dim fbd As FolderBrowserDialog = New FolderBrowserDialog()
If fbd.ShowDialog() = DialogResult.Cancel Then Exit Sub
Dim outputPath As String = IO.Path.Combine(fbd.SelectedPath, "selection log.txt")
'pseudocode
'parse file with date, time, direction, speed fields in each line
'dim SelectionRangeStart As String = LastLine.Date - MonthSelection(1,2,3,4,5,6)
'dim CSVSelectionRange As String = SelectionRangeStart to LastLine
IO.File.WriteAllText(outputPath, CSVSelectionRange)
MessageBox.Show("Success!")
End If
End Sub
ive searched for how to parse but its very complex and all forums and guides seem to be very specific and a bit difficult to understand. any help at all is greatly appreciated :)

Related

Not read text file correct

I want when I read txt file and add in ListBox1.items, add this text http://prntscr.com/on12e0 correct text §eUltra §8[§716x§8].zip not like this http://prntscr.com/on11kv
My code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
My.Computer.FileSystem.CopyFile(
appDataFolder & "\.minecraft\logs\latest.log",
appDataFolder & "\.minecraft\logs\latestc.log")
Using reader As New StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\.minecraft\logs\latestc.log")
While Not reader.EndOfStream
Dim line As String = reader.ReadLine()
If line.Contains(" Reloading ResourceManager: Default,") Then
Dim lastpart As String = line.Substring(line.LastIndexOf(", ") + 1)
ListBox1.Items.Add(lastpart)
End If
End While
End Using
My.Computer.FileSystem.DeleteFile(appDataFolder & "\.minecraft\logs\latestc.log")
End Sub
This question is only different from your first question in that your have substituted a ListBox for a RichTextBox. It seems you got perfectly acceptable answers to your first question. But I will try again.
First get the path to the file. I don't know why you are copying the file so I didn't.
Add Imports System.IO to the top of your file. The you can use the File class methods. File.ReadAllLines returns an array of strings.
Next use Linq to get the items you want. Don't update the user interface on each iteration of a loop. The invisible Linq loop just adds the items to an array. Then you can update the UI once with .AddRange.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim appDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\.minecraft\logs\latest.log")
Dim lines = File.ReadAllLines(appDataFolder)
Dim lstItems = (From l In lines
Where l.Contains(" Reloading ResourceManager: Default,")
Select l.Substring(l.LastIndexOf(", ") + 1)).ToArray
ListBox1.Items.AddRange(lstItems)
End Sub
If this answer and the previous 2 answer you got don't work, please lets us know why.

What is the best loop to use for a big list?

I was never any good with loops for some reason, but can not live without them. following is the code I use to read a list of user details in XXXX:XXXX format. (not 4 chars exactly but you know) I can load the first user and am able to split the string and add from the colon back to listbox1, but just cannot get a loop to work and cant walk away until this is done. Normally a break from work solves the war but not an option right now.
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim R As New IO.StreamReader(OpenFileDialog1.FileName)
Dim str As String() = R.ReadLine().Split(":")
Dim Info As String = str(1)
ListBox1.Items.Add(Info)
ListBox1.SelectedIndex = 0
TextBox5.Text = ListBox1.SelectedItem
R.Close()
End Sub
I know its right in front of me.
I just tried a for next loop which I thought for sure would work and didnt.
Please help. Someone
No need for loop:
Dim lines = IO.File.ReadAllLines(OpenFileDialog1.FileName)
Dim items = Array.ConvertAll(lines, Function(line) line.Split(":"c)(1))
ListBox1.Items.AddRange(items)
You need this loop.
Using sr As New StreamReader(OpenFileDialog1.FileName)
While Not sr.EndOfStream
ListBox1.Items.Add(sr.ReadLine().Split(":")(1))
End While
End Using

Openfile on event

I am getting my feet wet with VB .Net programming (total novice). I have a DataGridView with amongst other information, a file path to where a particular document is stored. I have added a DataGridViewButtonColumn to the DataGridView, but I cannot figure out how to get the button to open the file.
Sorry, I have no code to provide as starting point for where I get stuck.
Thanks in advance,
Sorry I didn't read the post clear enough the first time, and didn't explain my code enough so it got deleted. This uses the contentclick event.
Dim Filetext As String = "" 'At start of the class to make it available to the whole class
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim FilePathColumn As Integer = 0 'File path is in Column 0
Dim ButtonColumn As Integer = 1 'Column buttons are in
Dim RowClicked As Integer = e.RowIndex 'This gets the row that you clicked the button in
If e.ColumnIndex = ButtonColumn Then 'Make sure you clicked a button
Dim FILE_PATH As String = DataGridView1.Rows(RowClicked).Cells(FilePathColumn).ToString 'Get the path to the file
If System.IO.File.Exists(FILE_PATH) Then 'Make sure file exists
Filetext = System.IO.File.ReadAllText(FILE_PATH) 'Save file to a variable
'OR
Process.Start(FILE_PATH) 'To open it
End If
End If
End Sub
You can get rid of most of those lines but I wrote it like that to explain how it worked

renaming files in a folder by other file names in another folder with vb.net

i'm writing a programme and i guess the title explain it all .
and it did work but with one file only , problem is the program ignores the next claarving the first file - the success - is still his priority and he can't find it so it crashes
this is my code
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
IO.Directory.SetCurrentDirectory(TextBox2.Text)
Dim info As New IO.DirectoryInfo(TextBox1.Text)
Dim imfo2 As New IO.DirectoryInfo(TextBox2.Text)
For Each fi In imfo2.GetFiles
For Each foo In info.GetFiles
Dim namewithoutavi As String = foo.Name.Substring(0, foo.Name.Length - 4)
Dim fixedname As String = namewithoutavi & ".srt"
My.Computer.FileSystem.RenameFile(fi.FullName, fixedname)
Next
Next
End Sub
can someone please tell me how to make it right ?
as you can see i'm a beginner and i'm really desperate for help :)

HI, New to programming with textfiles loops and much more

i have a textfile which is in this format
and i am trying to use a stream reader to help me loop each word into a text box, i am new to programming and really need help because all other examples are too complicated for me to understand,
this is what i am trying to do :
Dim objectreader As New StreamReader("filepath")
Dim linereader(1) As String
linereader = Split(objectreader.ReadLine, ",")
For i As Integer = 0 To UBound(linereader)
Spelling_Test.txtSpelling1.Text = linereader(0)
Spelling_Test.txtSpelling2.Text = linereader(0)
Next
but only get the first line of the text file in to a textbox, i need it to loop to the next line so i can write the next line in!
your help would be much appreciated, and if possible then can you show it practically , if you dont understand what i am trying to do then please ask
It is a little confusing on what you are trying to do, it looks like your text file consists of a word and a hint, you only have one set of textbox's and 3 lines of information in your file.
This example show you how to incrementally read your Stream.
Public Class Form1
Dim objectreader As StreamReader
Dim linereader() As String
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If IsNothing(objectreader) Then
objectreader = New StreamReader("C:\Temp\data.txt")
End If
linereader = Split(objectreader.ReadLine, ",")
If String.IsNullOrEmpty(linereader(0)) Then
objectreader.Close()
objectreader = Nothing
Else
txtSpelling1.Text = linereader(0) 'Word
txtSpelling2.Text = linereader(1) 'Hint
End If
End Sub
End Class
But in your case, I would probably just use the File.ReadAllLines Method
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim result As String() = File.ReadAllLines("C:\Temp\data.txt")
For x = 0 To UBound(result)
Dim c As Control = Controls.Find("txtSpelling" + (x + 1).ToString, True)(0)
If Not IsNothing(c) Then
c.Text = Split(result(x), ",")(0)
End If
Next
End Sub