Link to home
Start Free TrialLog in
Avatar of bidgadget
bidgadgetFlag for United States of America

asked on

ASP.NET VB.NET to have DirectoryInfo and retrieve files with spaces.

I have a ASP.NET website that we use for our client portal.  It is written in VB.  I created a DirectoryInfo link that works great and gives the users the ability to see a directory listing and download the file.  I have to make sure that the files in the directory do not have spaces or the link gives an error.  Is there any way to change the code to retrieve the file that has a space in it  Below is an example of my code.  Any help would be appreciated.

  Protected Sub Label11_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label9.Load

        Dim strFileSize As String = ""
        Dim di As New IO.DirectoryInfo("e:\printed statements\" & Label7.Text & "familyoffice\" & "2019\" & "february\" & "monthly_reports\")
        Dim aryFi As IO.FileInfo() = di.GetFiles
        Dim fi As IO.FileInfo

        For Each fi In aryFi
            strFileSize = (Math.Round(fi.Length / 1024)).ToString()
            Label11.Text += "<a href=https://www.brandytrust.net\Clientfiles\" & Label7.Text & "familyoffice\" & "2019\" & "february\" & "monthly_reports\" + fi.Name + ">" + fi.Name + "</a><br /> "

        Next

Open in new window

Avatar of Paul MacDonald
Paul MacDonald
Flag of United States of America image

Replace the spaces in the file name with %20.

So if the file name is "My Text File.txt", instead of

"www.mysite.tld/My Text File.txt"

have it read

"www.mysite.tld/My%20Text%20File.txt"

ASP.Net as the URLEncode method that will do this for you, but a simple string substitution will work for you just as well.

and you should be okay.
Avatar of bidgadget

ASKER

there are hundreds of files in hundreds of directories.  Is there any way to retrieve without having to change all the actual file names manually.
sorry I am confused.   Are you saying to manually change each file and take out the spaces or do I need to but %20 in my line of code?  Thank you
I'm not suggesting you change file names.

You asked how to filter out file names with spaces, because when you send people a link to a file  name with spaces, the link doesn't work.  My response was to show you how to make those links work.

The file names can stay the same.  You can show the user the file names as they actually appear.  When the user wants to download a file with a space in the file name, change the URL so any spaces are replaced with "%20", and the link should work just fine.
OK, thank you.  I appreciate your suggestion.  Unfortunately we cannot ask all of our clients to change url links.  I was hoping there was a way to have the link add in the %20 automatically if it sees a space in the file name.  I guess there is not way to do that?
I don't know what you mean by "automatically".  I'm suggesting a small change to the code you're using, which is "automatic" from a human perspective.  No intervention would be required on the file.  You'd simply be fixing the link to the file so it would work.
ok, thank you.  I guess I don't know where in my code I would replace the code
+ fi.Name + ">" + fi.Name +

So if the above is the part in my code that retrieves the file name, where would I change it?  Thank you and so sorry for the confusion.
ASKER CERTIFIED SOLUTION
Avatar of Paul MacDonald
Paul MacDonald
Flag of United States of America 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
Thank you so much!
Very happy to help.
And it worked perfectly. Cheers!
Ha ha ha!  I took that for granted as you'd closed the question.  From now on, make sure you have a working solution before you award points!