Solved

Open a folder with partial name

Posted on 2016-11-03
14
27 Views
Last Modified: 2016-11-04
Hello,

Is there a way to open a folder with partial name know?

The code i am using is
 Dim clsFrm As New ClsFrmMainLoad
        Try
            Dim Folder_Path As String
            Folder_Path = MedicalFolderPath
            Folder_Path = Folder_Path & Me.txtRefAuth.Text --------- folder name 
            If (Directory.Exists(Folder_Path)) Then
                Me.BtnOpenFolder.Enabled = False
                Me.Cursor = Cursors.WaitCursor
                Process.Start("explorer.exe", Folder_Path)
                Me.BtnOpenFolder.Enabled = True

            Else
                clsFrm.FormatMessage(24, " Folder Update : ", Folder_Path, "Folder Location")
            End If

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        Finally
            Me.BtnOpenFolder.Enabled = True
            Me.Cursor = Cursors.Default
        End Try

Open in new window



Cheers
0
Comment
Question by:RIAS
  • 8
  • 5
14 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Yes (-ish).
You would need to perform a wildcard search in code.  Then if only one match you can complete the name otherwise there is no match or you need user input to decide which one is the correct folder.
0
 

Author Comment

by:RIAS
Comment Utility
Andy what should be here

  Folder_Path = Folder_Path & Me.txtRefAuth.Text --------- folder name 

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
for the wildcard search:
Folder_Path & Me.txtRefAuth.Text  & "*"
0
 

Author Comment

by:RIAS
Comment Utility
Andy it didnt work , it came up saying it does  not exist
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Please post your code.

ps.  Did you read my first comment carefully?
0
 

Author Comment

by:RIAS
Comment Utility
Dim clsFrm As New ClsFrmMainLoad
        Try
            Dim Folder_Path As String
            Folder_Path = MedicalFolderPath
            Folder_Path = Folder_Path & Me.txtRefAuth.Text --------- folder name 
            If (Directory.Exists(Folder_Path)) Then
                Me.BtnOpenFolder.Enabled = False
                Me.Cursor = Cursors.WaitCursor
                Process.Start("explorer.exe", Folder_Path)
                Me.BtnOpenFolder.Enabled = True

            Else
                clsFrm.FormatMessage(24, " Folder Update : ", Folder_Path, "Folder Location")
            End If

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        Finally
            Me.BtnOpenFolder.Enabled = True
            Me.Cursor = Cursors.Default
        End Try

Open in new window

0
 

Author Comment

by:RIAS
Comment Utility
Did read it and replaced with wildchar as you suggested
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 100 total points
Comment Utility
>>Did read it and replaced with wildchar as you suggested
You haven't done as I suggested at all.  

I said perform a search and then based on the results of that search do one of a number of things.  None of that appears in your code.
0
 
LVL 32

Accepted Solution

by:
it_saige earned 400 total points
Comment Utility
*NO POINTS*

What Andy is saying is this:
Imports System.IO

Module Module1
	Sub Main()
		Dim medicalFolderPath As String = "C:\_admin"
		Dim searchFolder As String = "tes"

		If Directory.Exists(medicalFolderPath) Then
			Dim matches = Directory.GetDirectories(medicalFolderPath, String.Format("{0}*", searchFolder), SearchOption.TopDirectoryOnly)
			If matches.Count <> 0 Then
				Console.WriteLine("Found {0} match{1}", matches.Count, If(matches.Count <> 1, "es", ""))
				For Each folder In matches
					Console.WriteLine(folder)
				Next
			Else
				Console.WriteLine("There were no folders found that match - {0}", searchFolder)
			End If
		Else
			Console.WriteLine("Medical Folder Path - {0}; is invalid.", medicalFolderPath)

		End If
		Console.ReadLine()
	End Sub
End Module

Open in new window

Which on my system with the following directory structure:Capture.JPGProduces the following output:Capture.JPG
If you are using .NET 4 or higher, you can use Directory.EnumerateDirectories instead of Directory.GetDirectories.

Regardless the changes to your code would look something like:
Dim clsFrm As New ClsFrmMainLoad
Try
	Dim Folder_Path As String
	Folder_Path = MedicalFolderPath
	Folder_Path = Folder_Path & Me.txtRefAuth.Text
	If (Directory.GetDirectories(MedicalFolderPath, String.Format("{0}*", Me.txtRefAuth.Text), SearchOption.TopDirectoryOnly).Count > 0) Then
		Me.BtnOpenFolder.Enabled = False
		Me.Cursor = Cursors.WaitCursor
		Process.Start("explorer.exe", Folder_Path)
		Me.BtnOpenFolder.Enabled = True
	Else
		clsFrm.FormatMessage(24, " Folder Update : ", Folder_Path, "Folder Location")
	End If
Catch ex As Exception
	MessageBox.Show(ex.ToString)
Finally
	Me.BtnOpenFolder.Enabled = True
	Me.Cursor = Cursors.Default
End Try

Open in new window


Also when combining paths, it is generally a good to use Path.Combine.

-saige-
1
 

Author Comment

by:RIAS
Comment Utility
Thanks Sir, Did not understand No points?
0
 

Author Closing Comment

by:RIAS
Comment Utility
Sir,

Process.Start("explorer.exe", Folder_Path)  does not have wild char to open the folder.
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
>>Process.Start("explorer.exe", Folder_Path)  does not have wild char to open the folder.
@RIAS
If that previous comment by you was directed to me then all I can say is:

I know that, which is why I told you what you needed to do (which is partially what it_saige showed you, the search part) to achieve what you asked for.  I expect people asking questions to think and try to do it themselves.  One learns much more that way than just copy and paste.

Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
0
 

Author Comment

by:RIAS
Comment Utility
lol! Andy thanks for your help !
0
 

Author Comment

by:RIAS
Comment Utility
Saige Sir,
Here is the solution I worked it out with your guidline :
 Dim clsFrm As New ClsFrmMainLoad
        Try
            Dim i As Integer = 0
            Dim Folder_Path As String
            Folder_Path = MedicalFolderPath
            Folder_Path = Folder_Path & Me.txtRefAuth.Text

            If Directory.Exists(MedicalFolderPath) Then
                Dim matches = Directory.GetDirectories(MedicalFolderPath, String.Format("{0}*", Me.txtRefAuth.Text), SearchOption.TopDirectoryOnly)

                If matches.Count <> 0 Then
                    For Each folder In matches
                        Dim matchFolder = Replace(matches(i), MedicalFolderPath, "")
                        Dim match1 = Regex.Replace(matchFolder, "\D", "")
                        If match1 = Me.txtRefAuth.Text.Trim Then
                            Process.Start("explorer.exe", matches(i))
                        End If
                        i += 1
                    Next
                Else
                    MessageBox.Show("There were no folders found that match - {0}", Me.txtRefAuth.Text)
                End If
            Else
                MessageBox.Show("Medical Folder Path - {0}; is invalid.", MedicalFolderPath)

            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        Finally
            Me.BtnOpenFolder.Enabled = True
            Me.Cursor = Cursors.Default
        End Try

Open in new window


Can't thank you enough !
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now