[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

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....
0
ITHelper80
Asked:
ITHelper80
  • 4
  • 3
  • 2
  • +3
1 Solution
 
käµfm³d 👽Commented:
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

0
 
käµfm³d 👽Commented:
P.S.

You need to import the System.IO namespace  :)
0
 
LCARSx32Commented:
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

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
käµfm³d 👽Commented:
@ LCARSx32

Your function will always return nothing unless you put an "Exit Sub" after "Return myDir"   : )
0
 
LCARSx32Commented:
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
0
 
Shahan AyyubSenior Software Engineer - iOSCommented:
I couldn't get that, Is it possible that you have two customers of same name but different dates as folder name???
0
 
Shahan AyyubSenior Software Engineer - iOSCommented:
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

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

Thanks again!!
0
 
lenordisteCommented:
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.
0
 
Shahan AyyubSenior Software Engineer - iOSCommented:
Try this one, I hope this will not create problem:
Public Function RetrieveFolder(ByVal Cust_Name As String) As String
        Dim re As Regex = New Regex("\d{2}-\d{2}-\d{2} " & Cust_Name & "\\")
        Dim getdirs As String() = Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\" & "Widgets")
        For Each dir As String In getdirs
            Dim temp As String() = dir.Split("\\")
            Dim s As String = String.Concat(temp(temp.Length - 1), "\\")
            If re.IsMatch(s) Then
                Return dir
            End If
        Next
        Return Nothing
    End Function

Open in new window

0
 
käµfm³d 👽Commented:
@LCARSx32

Touche!  I guess I'm still stuck in the VB6/VBA days...  I didn't realize .NET allowed you to do that now  :)
0
 
kapilkhalasCommented:
@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

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now