-2

So here is my Directory

Dim strFldCustom As String
strFldCustom = "W:\CUSTOM\130000"

I need to search for an excel file with in the sub folders of this directory and then open it. the excel file is called rptWESSSummary 132788-03-r1

thank you

Eric J.
  • 147,927
  • 63
  • 340
  • 553
user3183324
  • 24
  • 2
  • 7
  • possible duplicate of [Searching By File Extensions VB.NET](http://stackoverflow.com/questions/8676516/searching-by-file-extensions-vb-net) – DeanOC Apr 29 '14 at 23:54
  • I downvoted your question because it's a duplicate to many other questions on here and you showed no initiative to solve the problem on your own. SO shouldn't be a place where you ask other people to do the work for you. – Maurice Reeves Apr 30 '14 at 03:32

2 Answers2

1

You can use

System.Directory.EnumerateDirectories(string path)

http://msdn.microsoft.com/en-us/library/dd383304(v=vs.110).aspx

to get an enumeration of all subdirectories, and then

System.IO.File.Exists()

http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx

to check if that file exists.

You can open the file using

System.Diagnostics.Process.Start()

http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.start

There are code samples in VB.Net at each of those links.

Note: If the file isn't in a folder directly under strFldCustom, but rather there could be folders with folders, you can use recursion to search the folders-in-folders for the file.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
1

Here working basic code:

F parameter is the top directory where you want to scan system.GetfilesSystemEntries will take care to make the recursive job for you :)

In example you have to scan in windows folder you pass c:/windows and it will check into all nested, not nested, directory and return an array of string () which include the fullpath :) that's all

  Private Sub GetFile(ByVal f As String)
    Try
        Console.WriteLine("Scanning")
        Dim fList = Directory.GetFileSystemEntries(f, "*.xls", SearchOption.AllDirectories).Where(Function(X) X.Contains("rptWESSSummary 132788-03-r1.xls"))
        Dim Z As String = String.Empty
        For Each el In fList
            Z = el.ToString
            Console.WriteLine(Z)
        Next
        Console.WriteLine("Load excel")
        Dim xlApp As New Excel.Application
        xlApp.Workbooks.Open(Z)
        xlApp.Visible = True
        Console.WriteLine("File aperto in excel")
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try

    Console.ReadLine()
End Sub

Get all file with directory.GetFileSystemEntries create a new excel application accesso to the default workbooks and open your file.That's all :) hope it help

makemoney2010
  • 1,222
  • 10
  • 11