VB Getting The SubFolder name and saving it to a Text file

asomethings

I want to get The Subfolder Name Listed in my Textfile. I don't want to see the path for the SubFolder. I finally got a way to show only to my VS Console. But If i try to save it to my txt file it keeps on writing only the first line even though I used For. Please Help Me! Here's the code that writes to the console

Dim di As New IO.DirectoryInfo(startPath)
        Dim Drs() As IO.DirectoryInfo = di.GetDirectories()
        For Each dr As IO.DirectoryInfo In Drs
            Console.WriteLine(dr.Name)
        Next

This is the code that I tried to Write It on a txt file. It only writes 1 Line

For Each Dir As String In Directory.GetDirectories(startPath)
            My.Computer.FileSystem.WriteAllText("C:\Test.txt", Dir, False)
        Next

The Expected Output is

SubFolder1
SubFolder2
SubFolder3
SubFolder4
SubFolder5

Like this in txt file

Tim Schmelter

You are using the wrong method, WriteAllText always overwrites the complete file, you want to append a new line. You could use File.AppendAllText:

For Each Dir As String In Directory.GetDirectories(startPath)
    System.IO.File.AppendAllText("C:\Test.txt", Dir)
Next

Another option, use a StreamWriter, it has a constructor that takes a Boolean to append text:

Using writer As New System.IO.StreamWriter(startPath, True)
    For Each Dir As String In Directory.GetDirectories(startPath)
        writer.WriteLine(Dir)
    Next
End Using

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

VB Getting The SubFolder name and saving it to a Text file

From Dev

VB Saving img file with path adds folder name to file name

From Dev

from DataGridView saving to text file issue when saving date values on vb.net

From Dev

Getting single Subfolder name but not its contents

From Dev

Batch file to loop through subfolder and use subfolder name as variable

From Dev

Saving ArrayList to Text File

From Dev

Saving PairedRDD as a text file

From Dev

Saving ArrayList to Text File

From Dev

Create a file with exact name as an existing subfolder?

From Dev

Getting only specific data based on name in text file

From Dev

R: plot saving and file name

From Dev

Getting "UnicodeEncodeError: 'charmap' codec can't encode character" when saving to a text file in Windows

From Dev

getting input from click event in javascript and sending it to django or saving it as text file

From Dev

Write folder name and subfolder name being deleted to output file

From Dev

Saving and formatting text file with Matlab

From Dev

Saving a data structure to a text file

From Dev

Saving output of a function to a text file

From Dev

trouble with saving ndarray to text file

From Dev

Saving textbox text into XML file

From Dev

Saving a batch variable in a text file

From Dev

Refreshing and Saving as a text file in Excel

From Dev

Saving text file from code

From Dev

Saving user feedback in a text file

From Dev

trouble with saving ndarray to text file

From Dev

Saving a text file from flash

From Dev

Saving text file in it's entirety

From Dev

Saving a copy of text file by matlab

From Dev

Saving a spinner position to a text file

From Dev

Saving output of a program in text file

Related Related

HotTag

Archive