How to remove file path?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • starlight849
    New Member
    • Jun 2009
    • 82

    How to remove file path?

    hello,
    I am reading all files from a directory and writing them to a text file. This is working just fine.
    Code:
      IO.File.WriteAllLines(strFile, IO.Directory.GetFiles(strPath, "*.txt"))
    How would i go about removing the file path from the list of file names in the text file.
    This is information I do not need.
    THanks,
    Starlight849
  • Joseph Martell
    Recognized Expert New Member
    • Jan 2010
    • 198

    #2
    You can do this using LINQ:

    Code:
            Dim allFiles = From str1File As String In IO.Directory.GetFiles(strPath, "*.txt") _
                                        Select str1File.Remove(0, strPath.Length + 1)
    Then use allFiles.ToArra y() to get your string array to use with IO.File.WriteAl lLines.

    The LINQ expression will cycle through all of the files and remove the path. I had to add + 1 because my path did not end with "\". You could do this with a for each loop as well if you are using VB before .Net version 3.5. Either way you basically have to add at least 1 more variable.

    Comment

    • PsychoCoder
      Recognized Expert Contributor
      • Jul 2010
      • 465

      #3
      Instead of doing any string parsing why not just use the Path.GetFileNam e Method, which returns just the file name from the path

      Code:
      Dim allFiles = From str1File As String In IO.Directory.GetFiles(strPath, "*.txt")
                                  Select System.IO.Path.GetFileName(str1File)

      Comment

      • Joseph Martell
        Recognized Expert New Member
        • Jan 2010
        • 198

        #4
        @Richard McCutchen
        Good call. I didn't remember that method was available. It would avoid the problem I had of padding the strPath.length attribute to handle the '\' character.

        Comment

        • starlight849
          New Member
          • Jun 2009
          • 82

          #5
          Thanks for all your help so far, guys. I am trying this out and getting an error.
          unable to cast object of type '<Select Iterator>d_d'2[system.string,s ystem.string]' to type 'system.string[]'

          Comment

          • Joseph Martell
            Recognized Expert New Member
            • Jan 2010
            • 198

            #6
            The only way that I have been able to reproduce your error has been to declare allFiles as a string array before the LINQ statement that Richard and I gave you. Notice that in both of our postings our statements start off with the "Dim" keyword.

            Make sure that allFiles is NOT declared before this statement, or, if you really want it declared prior to this statement, declare it as IEnumerable(Of String).

            Comment

            • starlight849
              New Member
              • Jun 2009
              • 82

              #7
              Figured it out! Thanks for all the help, guys!

              Comment

              Working...