Link to home
Start Free TrialLog in
Avatar of ITHelper80
ITHelper80

asked on

Save file to folder using partial folder name for path

Im working on a project where the client has pre-setup folders in a sub folder("Widgets") of My Documents, these folders are date coded with the customer name for e.g.
 "04-10-10 John Smith".

The client requires that I take the value of the Customer Name field from my form and use it to save a series of files generated from the application into the matching sub folder.

How can I save my output into the "04-10-10 John Smith" folder with only using John Smith in folder path? I cannot use the entire folder name because I have no way of knowing the date value of the folder name....

I know this isnt an ideal solution but its what my customer wants....
Avatar of kaufmed
kaufmed
Flag of United States of America image

Something like this, perhaps?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myDocs As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
    Dim widets As String = Path.Combine(myDocs, "Widgets")
    Dim searchPattern As String = String.Concat("*", custNameBox)
    Dim subFolders As String() = Directory.GetDirectories(widets, searchPattern)
    Dim saveLocation As String

    If subFolders.Length <> 1 Then
        MessageBox.Show("Could not isolate folder")
        Exit Sub
    End If

    saveLocation = subFolders(0)

    'For Each file In filesToSave
        'save file using "saveLocation" as folder path
    'Next
End Sub

Open in new window

P.S.

You need to import the System.IO namespace  :)
Avatar of LCARSx32
LCARSx32

Something along the following lines should work as long as there isn't more than one folder with the customer's name (it would still work, but would always choose the first instance if their name):

'Begin code:

Imports System.IO 'At the very top, before any class declarations, of course


'This function takes the base path that contains the folders and the customer name, and returns the
'path to the correct folder.

    Private Function getPathByCustName(ByVal BaseDir As String, ByVal CustName As String) As String
        'Get a list of all of the folders in our base directory
        Dim myDirs() As String = System.IO.Directory.GetDirectories(BaseDir)

        'loop through the folders, looking for one with our customer's name
        For Each myDir As String In myDirs
            If myDir.ToLower.Contains(CustName.ToLower) Then
                'We found our folder!
                Return myDir
            End If
        Next

        'Uh oh, didn't find anything...
        Return Nothing

    End Function

Hope that helps,
-Ray

@ LCARSx32

Your function will always return nothing unless you put an "Exit Sub" after "Return myDir"   : )
It will work fine.  Try it.  But it would be the proper way to do it, yes.  Thanks for the correction.  In VB.NET, when a return is met in a function, the function automatically exits.

-Ray
I couldn't get that, Is it possible that you have two customers of same name but different dates as folder name???
if your all the customers have different names then it will work.
Public Function RetrieveFolder(ByVal Cust_Name As String) As String
        Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\" & "Widgets"
        Dim folders As String() = Directory.GetDirectories(path, "*" & Cust_Name)

        If folders.Length = 0 Then
            Return Nothing
        End If
        Return folders(0)
    End Function

Open in new window

Avatar of ITHelper80

ASKER

Thanks for all the suggestions, I am going to test each one and see which works best. Ill report back shortly.

Thanks again!!
all these solutions could give you a mismatch though... for example, let's assume your are looking for "John Smith" and you have two folders:
"04-10-10 Alan John Smith"
"04-10-10 John Smith"

The one you truly want is the second folder but the first one will match because it also ends with "John Smith". Since the search pattern of the method GetDirectories() doesn't give you much freedom I would advise you to add a regular expression and check the result against it so you can choose the best one.
ASKER CERTIFIED SOLUTION
Avatar of Shahan Ayyub
Shahan Ayyub
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@LCARSx32

Touche!  I guess I'm still stuck in the VB6/VBA days...  I didn't realize .NET allowed you to do that now  :)
@ITHelper80:

If the format of the folder will always remain same, then the code below should work for you:


Private Function FindFolder(ByVal foldername As String) As Boolean
Dim found As Boolean = False
Dim rootdir As DirectoryInfo

rootdir = New DirectoryInfo(Server.MapPath("Files"))

For Each subdir As DirectoryInfo In rootdir.GetDirectories()
            Dim dirfoldername = subdir.Name.Remove(0, 8).Trim

            If (dirfoldername = foldername) Then
                found = True
                Return found
            End If
Next

Return found
End Function

Open in new window