Click here to Skip to main content
15,071,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
everything in this code works, however it lists the full pathname and i would like to remove that. All i would like for this to do, is produce only a list of files in the selected directory without the fullpath name.

here is the code i need help with.

VB
Private Sub btnDirectory_Click(sender As Object, e As EventArgs) Handles btnDirectory.Click
    'This open up a dialog for the user to choose a folder.
    Dim objFolderDlg As System.Windows.Forms.FolderBrowserDialog
    objFolderDlg = New System.Windows.Forms.FolderBrowserDialog
    objFolderDlg.SelectedPath = "C:\Test"
    If objFolderDlg.ShowDialog() = DialogResult.OK Then
        'This is the location where the text file will be saved.
        Dim path As String = "C:\Users\" & Environment.UserName & "\list\Directory_List.txt"
        'This gets the list of files in the selected directory.
        Dim dirs As String() = Directory.GetFiles(objFolderDlg.SelectedPath)
        'Dim dirs As String() = System.IO.Directory.GetFileSystemEntries(objFolderDlg.SelectedPath)
        Dim fileName As String
        For Each fileName In dirs

        Next
        'Code to write the information to a text file.
        File.WriteAllLines(path, dirs)
        'Code to show the user the folder that they selected.
        'MessageBox.Show(objFolderDlg.SelectedPath)

    End If
End Sub

Posted
Updated 26-Mar-14 14:19pm
v2
Comments
[no name] 26-Mar-14 20:14pm
   
Okay.... remove the full path and then what? Replace with an empty string? The path? The file name? Next week's lottery numbers?
abroehl 26-Mar-14 20:19pm
   
All i would like for this to do, is produce only a list of files in the selected directory without the fullpath name.
Bernhard Hiller 27-Mar-14 3:39am
   
In addition, I'd suggest to use
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
instead of
"C:\Users\" & Environment.UserName
It works on XP also, and in cases where that folder was moved to a different location.

Easily done with the methods in the System.Io.Path[^] class.
   
This should give you the filename without the full path.
VB
For Each fileName As String In dirs
Dim nopath As String = FileIO.FileSystem.GetName(filename)
'code to write each  nopath  to your textfile
Next
   
VB
Public Sub btnFileList_Click(sender As Object, e As EventArgs) Handles btnFileList.Click
    ' This opens up a Dialog for the user to choose a folder to list the contents in.
    Dim objFolderDlg As System.Windows.Forms.FolderBrowserDialog
    objFolderDlg = New System.Windows.Forms.FolderBrowserDialog
    objFolderDlg.SelectedPath = "U:\"
    If objFolderDlg.ShowDialog() = DialogResult.OK Then
        ' This will get the User Profile information.
        Dim user As String = Environment.GetEnvironmentVariable("userprofile")
        '
        ' This is the location where the text file will be saved.
        Dim chkpath As String = user & "\list"
        Dim path As String
        If Directory.Exists(chkpath) Then
            path = user & "\list\File_List.txt"
        Else
            path = "u:\" & Environment.UserName & "\list\File_list.txt"
            'Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
        End If
        '
        'Dim path As String = user & "\list\File_List.txt"
        Dim di As New DirectoryInfo(objFolderDlg.SelectedPath)
        ' Get a reference to each file in that directory.
        Dim fiArr As FileInfo() = di.GetFiles()
        ' Display the names of the files.

        Dim file As FileInfo
        For Each file In fiArr
            ' Code to write the information to a text file.
            My.Computer.FileSystem.WriteAllText(path, file.Name & Environment.NewLine, True)

        Next

        ' This adds a new line at the end of file.
        My.Computer.FileSystem.WriteAllText(path, vbCrLf & "-----------------------------------------------------------" & vbCrLf & vbCrLf, True)

        ' Code to show the user the folder that they selected.
        MessageBox.Show(objFolderDlg.SelectedPath)

        ' This code opens the file that was created.
        Dim fyle As String = path

        If System.IO.File.Exists(fyle) Then
            IO.File.Open(fyle, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
            'IO.File.Open(fyle, FileMode.Open)
        End If

        Process.Start(fyle)
        'System.Diagnostics.Process.Start(fyle)
    End If
End Sub
   

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