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
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
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.
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