Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Open a folder with partial name

Posted on 2016-11-03
14
53 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

861 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