Click here to Skip to main content
16,018,802 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
have a Text file with several different C:\ locations i need, the idea is to use the streamreader to read the text file, load each line into an array and then be able to use that specific array per line in the Sub routines to code.

the idea, is that if in the future the location of the C:\ changes, there is no need to go into the code, just modify the information on that txt file and that would be it...

What I have tried:

have tried several ways but no luck...

text file info looks like

;C:\receiveddata\d1.html
;C:\receiveddata\d2.html
;C:\displaydata\d1.html
;C:\displaydata\d2.html

I also tried putting the ; at the en andd nothing and putting the text in "" and nothing

the idea is to modify the setup.txt file and that updates the new location to be used in the program...

here is the code .... i tried the following and no luck

Public Class Form1

Dim urls(10) As String

Public Sub Setup()
Dim sr As New StreamReader("C:\Setup\Setup.txt")
Dim line As String
Dim token() As String
Dim i As Integer = 0

Do Until sr.Peek = -1
'grab one line at a time from the txt
line = sr.ReadLine()
'place into an array
token = Split(line, ";")
urls(i) = token(0)
'increment loop for i
i = i + 1
Loop
sr.Close()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Call Setup() 'load setup for each url in project
Call displayfolderlog() 'check if folders exist
Call MoveFiles()
End Sub

Public Sub MoveFiles()
Dim D2Source = "C:\receiveddata\d2.html";
Dim D2file = "C:\displaydata\d2.html";

'FOR D1 FILE
If My.Computer.FileSystem.FileExists(urls(1)) = True Then
My.Computer.FileSystem.MoveFile(urls(1), urls(3))
End If

'FOR D2 FILE
If My.Computer.FileSystem.FileExists(D2Source) = True Then
My.Computer.FileSystem.MoveFile(D2Source, D2file)
End If

End Sub

End Class

it only moves the D2 file but the D1 no....
Posted
Updated 5-May-16 16:05pm
Comments
Sergey Alexandrovich Kryukov 5-May-16 13:19pm    
Programming is not done by "luck"...
Are your files HTML files?
—SA
Sergey Alexandrovich Kryukov 5-May-16 13:21pm    
The fragment
If My.Computer.FileSystem.FileExists(D2Source) = True Then
shows that you have no clue what you are doing. My.Computer.FileSystem.FileExists(D2Source) is already Boolean, so why comparing it with true?
—SA
George Jonsson 5-May-16 22:08pm    
You can blame Microsoft for this.
Snippet straight from example for File.ReadAllLines on MSDN.
"If File.Exists(path) = False Then"
Sergey Alexandrovich Kryukov 5-May-16 23:05pm    
Sometimes Microsoft should be blamed.
—SA

1 solution

From my understanding of your problem, I gather that you want to read an arbitrary number of lines from a text file and each line is a file path.

I think you are confusing your self a bit by first reading the file line-by-line and then try to split it further.

Input File
C:\receiveddata\d1.html<CR><LF>
C:\receiveddata\d2.html<CR><LF>
C:\displaydata\d1.html<CR><LF>
C:\displaydata\d2.html<CR><LF>

You don't need a specific separator character, because you already have a Carriage Return and a Line Feed at the end of each line, although invisible in the regular text editor.

The next step is to read the lines.
System.IO.File.ReadAllLines[^] was kind of made for this task.

So basically all you have to do is this:
VB
Dim filePath As String
For Each filePath In File.ReadAllLines("C:\Setup\Setup.txt")
    ' Do some cool stuff with your files.
Next
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-May-16 23:07pm    
Sure, a 5.
—SA
George Jonsson 6-May-16 1:06am    
Thanks, Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900