Results 1 to 3 of 3

Thread: [RESOLVED] Problem with Directory.GetFiles

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2013
    Posts
    48

    Resolved [RESOLVED] Problem with Directory.GetFiles

    Visual Studio Express 2012
    VB.NET

    Everything I've read about Directory.GetFiles says that it returns the full path of the files it finds in a folder. My code is only returning the filename.ext.

    Code:
    Imports System.Threading
    Imports System.IO
    Imports Scripting
    Imports System.Collections
    Module Module1
       Public Function GetFiles(ByVal vFolder As String,
                                 Optional vMask As String = "*.*",
                                 Optional vSearchOption As System.IO.SearchOption = SearchOption.TopDirectoryOnly)
            Const C_PROC_NAME = "GetFiles"
            Dim di As New DirectoryInfo(vFolder)
            Dim files As IO.FileInfo() = di.GetFiles()
            Dim arrayList As New System.Collections.ArrayList()
            Dim sFiles
    
            Try
                sFiles = Directory.GetFiles(vFolder, vMask, vSearchOption).[Select](Function(nm) Path.GetFileName(nm))
                For Each filenm As String In sFiles
                    arrayList.Add(filenm) 
                Next
            Catch ex As Exception
                Stop
            Finally
                GetFiles = arrayList
            End Try
        End Function
    End Module
    
    Dim fls As New System.Collections.ArrayList()
    fls = modFunctions.GetFiles(aFolders(i), "*.txt", SearchOption.AllDirectories)
    It certainly finds the one file in the folder.

    I have "Scripting" code that does the job perfectly, I'm trying to move more towards .NET code rather than hanging on to VB6 code, so if the worst comes to the worst and .NET functionality doesn't do what I want it to do, I stay with Scripting and the FileSystemObject...

    What am I doing wrong?

    Thanks!
    Last edited by JA12; Apr 2nd, 2014 at 03:51 AM.

  2. #2
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Problem with Directory.GetFiles

    I'm not too sure, but I also looked at Directory.GetFiles and it said it retrieved the full path.
    I believe, but not too sure, that your error is here
    Code:
    sFiles = Directory.GetFiles(vFolder, vMask, vSearchOption).[Select](Function(nm) Path.GetFileName(nm))
    because you seem to be then selecting only the filenames from the list. Maybe try it without that part.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2013
    Posts
    48

    Re: Problem with Directory.GetFiles

    Brilliant!

    I copied the code from an example and didn't pay full attention to what the code was doing...

    Code:
    .[Select](Function(nm) Path.GetFileName(nm))
    This bit strips the path information and returns just the filename and extension.

    I'm feeling a little stupid right now!

    Thanks so much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width