[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Open a folder with partial name

Posted on 2016-11-03
14
Medium Priority
?
74 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 45

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 45

Expert Comment

by:AndyAinscow
ID: 41872260
for the wildcard search:
Folder_Path & Me.txtRefAuth.Text  & "*"
0
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!

 

Author Comment

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

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 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 400 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 35

Accepted Solution

by:
it_saige earned 1600 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 45

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month17 days, 15 hours left to enroll

831 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