Solved

Open a folder with partial name

Posted on 2016-11-03
14
56 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
ID: 41872214
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
ID: 41872244
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
ID: 41872260
for the wildcard search:
Folder_Path & Me.txtRefAuth.Text  & "*"
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

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

Expert Comment

by:AndyAinscow
ID: 41872351
Please post your code.

ps.  Did you read my first comment carefully?
0
 

Author Comment

by:RIAS
ID: 41872356
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
ID: 41872357
Did read it and replaced with wildchar as you suggested
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 100 total points
ID: 41872369
>>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 33

Accepted Solution

by:
it_saige earned 400 total points
ID: 41872488
*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
ID: 41872622
Thanks Sir, Did not understand No points?
0
 

Author Closing Comment

by:RIAS
ID: 41873609
Sir,

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

Expert Comment

by:AndyAinscow
ID: 41873692
>>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
ID: 41873694
lol! Andy thanks for your help !
0
 

Author Comment

by:RIAS
ID: 41873831
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

679 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