Solved

Open a folder with partial name

Posted on 2016-11-03
14
47 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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