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....
"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....
P.S.
You need to import the System.IO namespace :)
You need to import the System.IO namespace :)
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.GetDir ectories(B aseDir)
'loop through the folders, looking for one with our customer's name
For Each myDir As String In myDirs
If myDir.ToLower.Contains(Cus tName.ToLo wer) Then
'We found our folder!
Return myDir
End If
Next
'Uh oh, didn't find anything...
Return Nothing
End Function
Hope that helps,
-Ray
'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.GetDir
'loop through the folders, looking for one with our customer's name
For Each myDir As String In myDirs
If myDir.ToLower.Contains(Cus
'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" : )
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
-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
ASKER
Thanks for all the suggestions, I am going to test each one and see which works best. Ill report back shortly.
Thanks again!!
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.
"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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@LCARSx32
Touche! I guess I'm still stuck in the VB6/VBA days... I didn't realize .NET allowed you to do that now :)
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:
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