Newbie here. Using StreamReader in VB, I am trying to read any csv file. Filename changes daily as it contains current date. Trying different Regular expressions to represent filename but with no luck.
My original line -
objReader = New IO.StreamReader("\\FolderA\FolderB\SEBCTS20220831.csv")
I have tried -
objReader = New IO.StreamReader("\\FolderA\FolderB\^SEBCTS\w\w\w\w\w\w\w\w\.csv$")
objReader = New IO.StreamReader("\\FolderA\FolderB\^\w\w\w\w\w\w\w\w\w\w\w\w\w\w\.csv$")
But I got error - Error: Could not find a part of the path '\FolderA\FolderB^SEBCTS\w\w\w\w\w\w\w\w.csv$'.
I also tried -
objReader = New IO.StreamReader("\\FolderA\FolderB\^SEBCTS[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].csv$")
I get the error - Error: Could not find file '\FolderA\FolderB^SEBCTS[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].csv$'.
Can somebody point me in the right direction? Any help will be appreciated.
Related
In VB, I want to dynamically create a new text file in a hard-coded file share, based on the current user logged in.
I have tested the below code, which does indeed create the test file in the specified path, but i want the test.txt file name to be dynamic, im thinking based on the environment.username class?
Dim objwriter As New System.IO.StreamWriter("\\server\path\test.txt")
objwriter.WriteLine("first line")
objwriter.WriteLine("testing")
objwriter.WriteLine("")
objwriter.Close()
Based on what I had obtained rthus far, I may have to define a variable as a string then append it to my StreamWriter write command?
Dim user_name As String = Environment.UserName
Just trying to now put the two together.. any help would be appreciative..
Thanks hackerman - this did the trick..
Dim objwriter As New System.IO.StreamWriter("\\server\path\" + user_name + ".txt")
I'm sure this is really simple, but has had me stumped for a while!
I need to show the user a text file, with lines being written to it as my program executes. Stuff like "Working on this file - Successful!". Another forum thread has helped me to allow the text file to be accessed by multiple processes, but now when I open the text file using Process.Start, it doesn't show the lines that are being written using the StreamWriter. Any help very much appreciated.
Code:
Dim ExportLog As String = "C:\ExportLog.txt"
If System.IO.File.Exists(ExportLog) Then
System.IO.File.Delete(ExportLog)
End If
Using writeStream = System.IO.File.Open(ExportLog,
FileMode.OpenOrCreate, FileAccess.ReadWrite,
FileShare.ReadWrite), Write As New StreamWriter(writeStream)
Write.WriteLine("Starting")
End Using
Dim MyLog As Process = Process.Start(ExportLog)
So I'm a bit stuck with this. Basically I've got a text file in a directory and in that text file, I've added different text in each of its line. I'm trying to use this to count how many lines there are in that text file. I'm using IsolatedStorageFile to do this and this is how I'm accessing it.
So far I've got this but It's not working:
Using isoComments As IsolatedStorageFileStream = New IsolatedStorageFileStream("textfile1.txt", FileMode.Open, isoStore)
Using readerComments As StreamReader = New StreamReader(isoComments)
For Each line In readerComments.ReadLine
MessageBox.Show(line.ToString.Count)
Next
End Using
End Using
The result is not the amount of lines instead, the amount of characters in each line. This is not what I want I want the number of lines in that particular textfile. Is anyone able to help me with this.
Something like this:
Dim iLineCount As Integer
iLineCount = 0
Using isoComments As IsolatedStorageFileStream =
New IsolatedStorageFileStream("textfile1.txt", FileMode.Open, isoStore)
Using readerComments As StreamReader = New StreamReader(isoComments)
For Each line In readerComments.ReadLine
iLineCount=iLineCount+1
Next
MessageBox.Show(iLineCount)
End Using
End Using
So I managed to solve this problem. My original idea was to get the amount of times a textfile was edited and so I figured adding something in each line of that textfile and counting each line would help but i couldn't find a way to it.
What I did was I added a counter which increases by 1 everytime it saves and then when it's read +1 is added to it. That way I have an exact number of times it was edited because everytime the textfile is edited, like mentioned before, it increases like a counter by 1. so if the user edited the textfile about 12 times then there will be the number 12 in the file and that will be read when the ser decides to edit the textfile again, once he saves +1 to the 12 will be added maing it 13.
I used this method to do the increment by one:
Using isoStream As IsolatedStorageFileStream = New IsolatedStorageFileStream("textfile.text", FileMode.CreateNew, isoStore) Using writer As StreamWriter = New StreamWriter(isoStream)
Dim i As Integer
i += 1
writer.WriteLine(i)
End Using
End Using
The below given code is returning corrupted data to the variable 'mystr'
like
PK ! ��fѲ � [Content_Types].xml �(� �UMk�#���^��N%�9�ɱ
It was reading the word files correctly. Suddenly it started happening without any change in code or software versions or source files!! Any word file I try to run through the below code gives the same corrupted output. I'm able to open the file in MsWord without any issue.
Dim myStreamReader As System.IO.StreamReader
Dim myStr As String
myStreamReader = System.IO.File.OpenText("c:\test.docx")
myStr = myStreamReader.ReadToEnd()
myStreamReader.Close()
Any suggestions why it is happening ?
You can't read a Word document using the StreamReader class as the file itself isn't plain text (it contains the data you find 'corrupted').
This link will help you reading text from a Word document.
I am trying to make a patching system or an updater if you will for my program. What I want it to do is download the patch list which is in a specific form in the text file.
Like this:
1 http://127.0.0.1:8080/patches/test1.rar
2 http://127.0.0.1:8080/patches/test2.rar
3 http://127.0.0.1:8080/patches/test3.rar
It's a tab in between 1 and the link.
And then I want it to check if the version number of the launcher or program at the moment matches the LAST patch in the PatchList.txt file. The last patch is version 3 and the launcher version at the moment is 0.
So if it doesn't match then it should start downloading one by one the patches. From patch 1 to patch 3.
I thought a loop statement would be best for it so I came up with this idea:
Make it download the PatchList.txt and then read each line individually. After it reads a line it will split where tab is so there will be two variables Version and PatchURL.
It will then check Version to see if it matches the launcher version and if it doesn't then it will download files from the PatchURL and apply it and then set the launcher version to the version of the current patch and then check again, if the version number and if it does match then there will be no updates so it will finish.
I don't know exactly how to put it into code. I have been trying for ages to be able to Download the txt file and read it line by line but I haven't been able to. I have tried many different things from StreamReader to StringReader and a bunch of other stuff.
I'd appreciate any help. This is very frustrating for me.
If you just want to download the file and read it in again, it's as easy as
My.Computer.Network.DownloadFile("http://your_url.txt", "local_tmp_path")
If you don't want to save the file on disk but read it in memory, you could use something like this:
Dim httpRequest = DirectCast(WebRequest.Create("http://your_url.txt"), HttpWebRequest)
Dim httpResponse = DirectCast(httpRequest.GetResponse(), HttpWebResponse)
Dim line As String
Using responseStream = httpResponse.GetResponseStream(), sr = New StreamReader(responseStream)
Do
line = sr.ReadLine()
If line IsNot Nothing Then
Dim s = line.Split(new String() {" "}, StringSplitOptions.RemoveEmptyEntries)
Dim version = s(0)
Dim path = s(1)
'Do something'
End If
Loop Until line Is Nothing
End Using
If you try to implement an update system for your software, better have a look at ClickOnce first.
What do you mean by download?
I would try the solution mentioned here.
How to read a specific line from a text file in VB
Otherwise, I would reccommend using XML for the version file and just deserializing it.