Question parsing a text file and writing to new text fileD:\Folder\

tommyt11

Member
Joined
Dec 21, 2015
Messages
23
Location
Columbus, Ohio
Programming Experience
5-10
I have a dilemma. I am quite a novice at parsing text files in vb net. I have tried split to no avail.
This is what I need to do. I have a text file with multiple lines (varies with each run of the function)
The text file is a list of files with paths.
Each path is different i.e.

D:\Folder\subfolder1\subfolder1a\subfolder1b\filename1.ext
D:\Folder\subfolder1\subfolder1a\subfolder1b\subfolder1c\filename2.ext
so forth and so on.
What I am attempting to do is parse just the filenames with their extension and writing to a new text file.i.e.
filename1.ext
filename2.ext
filename3.ext
etcetera
I have seen many different examples of split and regex but none of them seem to go where I am trying to go.
I have tried LastIndexOf can't seem to get it right.
Just need a clue as to what to do code wise I am at a loss.
 
There is a System.IO.Path class for manipulating file and folder paths. You can use its GetFileName method to get the file name from a file path. In your case, I would probably suggest calling File.ReadAllLines to get the lines of your file into a String array. You can then use a loop to get the file name from each file path. Finally, call File.WriteAllLines to write out the file names to a new file.
 
Thank you for your direction and
Ok so I came up with this and I got it to read the file but it writes only the first line over and over. I tried objstreamwriter and thats what happened. So I went with writealllines and that didn't write anything. I'm at a loss which line is failling. I'm sure its in reading and writing each line.
Here is my code with some commented out as it didn't work properly.

Function getfilenames()
On Error Resume Next

Dim line As String
Dim filename As String
Dim filenames As Array
Dim fileswopath As String
fileswopath = (appData & "/TNV/FileMover/filenamesonly.txt")
'Dim objStreamWriter As IO.StreamWriter
filenames = File.ReadAllLines(appData & "/TNV/FileMover/movedfiles.txt")

For Each line In filenames
filename = Path.GetFileName(line)
File.WriteAllLines(fileswopath, filename)
'objStreamWriter = New IO.StreamWriter(fileswopath, False, System.Text.Encoding.Unicode)
'objStreamWriter.WriteLine(filename)
'objStreamWriter.WriteLine(filename + vbNewLine)
'objStreamWriter.Close()
Next
 
Thank you for your direction and
Ok so I came up with this and I got it to read the file but it writes only the first line over and over. I tried objstreamwriter and thats what happened. So I went with writealllines and that didn't write anything. I'm at a loss which line is failling. I'm sure its in reading and writing each line.
Here is my code with some commented out as it didn't work properly.

Function getfilenames()
On Error Resume Next

Dim line As String
Dim filename As String
Dim filenames As Array
Dim fileswopath As String
fileswopath = (appData & "/TNV/FileMover/filenamesonly.txt")
'Dim objStreamWriter As IO.StreamWriter
filenames = File.ReadAllLines(appData & "/TNV/FileMover/movedfiles.txt")

For Each line In filenames
filename = Path.GetFileName(line)
File.WriteAllLines(fileswopath, filename)
'objStreamWriter = New IO.StreamWriter(fileswopath, False, System.Text.Encoding.Unicode)
'objStreamWriter.WriteLine(filename)
'objStreamWriter.WriteLine(filename + vbNewLine)
'objStreamWriter.Close()
Next
Are you using vb6 or vb.net?

The first part of the sub is vb6 code, but then you've got vb.net code (the namespaces) in the bottom section.
 
Thank you for your direction and
Ok so I came up with this and I got it to read the file but it writes only the first line over and over. I tried objstreamwriter and thats what happened. So I went with writealllines and that didn't write anything. I'm at a loss which line is failling. I'm sure its in reading and writing each line.
Here is my code with some commented out as it didn't work properly.
VB.NET:
 Function getfilenames()
        On Error Resume Next

        Dim line As String
        Dim filename As String
        Dim filenames As Array
        Dim fileswopath As String
        fileswopath = (appData & "/TNV/FileMover/filenamesonly.txt")
        'Dim objStreamWriter As IO.StreamWriter
        filenames = File.ReadAllLines(appData & "/TNV/FileMover/movedfiles.txt")

        For Each line In filenames
            filename = Path.GetFileName(line)
            File.WriteAllLines(fileswopath, filename)
            'objStreamWriter = New IO.StreamWriter(fileswopath, False, System.Text.Encoding.Unicode)
            'objStreamWriter.WriteLine(filename)
            'objStreamWriter.WriteLine(filename + vbNewLine)
            'objStreamWriter.Close()
        Next

You haven't thought that through. You're writing one file, right? In that case, why would you call WriteAllLines in the loop? I specifically said in my previous reply that you do that as the final step. If you're going to use a For Each loop then you need to get the file name for the current file path and add it to a list. After the loop completes, you make one call to WriteAllLines to write the entire list. Alternatively, you can use a For loop and modify the element at each index in the existing array instead of creating a new list.
 
I am using vs2015. I am trying to parse the one file movedfiles.txt and writing it to a second file filenamesonly.txt keeping the original in tact.
I tried this but like I said in previous post I am very new to parsing and all this stuff so I'm not sure of what to use to accomplish task
This is where I went next but this fails as well. I get nothing in the new file. So I'm missing something or I'm not creating the new content to write properly
Function getfilenames()
On Error Resume Next
Dim filename As String
Dim line As String
Dim lines As Array = File.ReadAllLines(appData & "/TNV/FileMover/movedfiles.txt")
Dim fileswopath As String = (appData & "/TNV/FileMover/filenamesonly.txt")


For Each line In lines
filename = Path.GetFileName(line)
Next
File.WriteAllLines(fileswopath, filename)


End Function
I thank you in advance for any help as I am totally confused now. Just to newbie I guess.
 
I know what you're trying to do so you don't need to explain again. I've already told you what to do so now you just have to do it. What you are showing that you tried is NOT what I told you to do. Read what I told you to do and do that.
 
Okay jmcilhinney I went back to your original post and I do apologize for not getting back sooner, other projects have distracted me until now. I understand the concept of what you were saying. I get the ReadAllLines and I create the array from the file. I understand the WriteAllLines aspect as well. It's the middle part whereby I assume you want me to loop through each line to getfilename using the Path.GetFileName which I undestand as well. The problem with my coding now appears to be I don't understand the loop process well enough. I've tried numerous loops only to get either a file that has only one line which it does correctly. OR a file with the same line written x number of times OR the same filename written a zillion times(actually hangs it's such a process.) During the attempts I get numerous errors stating string cannot be converted to type string() or even more odd string cannot be converted to IEnumerable of string(). What am I doing wrong?
 
It's like you're not actually reading the words I wrote but just getting a fuzzy idea of what the post is about based on your preconceptions. Read the words. I gave you two choices and I haven't seen code for either of them. Either:

1. Use a For Each loop over the array returned by ReadAllLines, get the file name from the path and add that file name to a list. I'll repeat that part because you seem to have ignored it previously: add the file name to a list.

or:

2. Use a For loop over the array, get the file name from the path at the current index and then replace the path in the array with the file name. At the end of that loop you will still just have the original array but now it will be full of file names rather than file paths.

Wihcever option you choose, you'll have a list of file names at the end that you can then write out.
 
As much as I don't like doing this, I know this is going to drag on so I'm just going to write the code.
Dim myList As New List(Of String)

For Each filePath In myArray
    myList.Add(Path.GetFileName(filePath)
Next
For i = myArray.GetUpperBound(0)
    myArray(i) = Path.GetFileName(myArray(i))
Next
 
Thanks jmcilhinney but I never wanted you to write my code. I was just stuck in the middle part of getting the value I wanted and getting it to another array. Once I read the List part it started to click and I eventually figured it out as I have never done that part of a coding procedure before. It took me awhile to get all the syntax correct and logic correct, but I did and I appreciate you pushing me in the right direction and not giving in til the last. Anyways Thanks for your help.
 
Back
Top