Solved

How can I Use Server.MapPath() when the filename has a comma in it?

Posted on 2001-08-30
24
397 Views
Last Modified: 2008-01-09
I'm trying to use the Server.MapPath() method to check for the existance of a certain file.  Unfortunately the file that I'm searching for may have a comma in the filename ex <c:\test, test.txt>.  I have no control over the filename so I need to find a work around that will allow me to use extract the actual full path of the file.

Currently I get the following error:

Server.MapPath() error 'ASP 0173 : 80004005'
Invalid Path Character
0
Comment
Question by:maytawn
  • 10
  • 6
  • 2
  • +3
24 Comments
 
LVL 2

Expert Comment

by:Ixeus
ID: 6439680
Server.MapPath(Server.htmlencode("c:\test, test.txt"))

~Ixeus

(think this will work?)
0
 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6439697
Why are you using Server.Mappath when you have the full path anyway? If you pass "C:\test, test.txt" to server.mappath, it probably will cause an error. What you put inside mappath is the virtual file path: Server.Mappath("/directory1/directory1.1/test, test.txt")

And this will be converted into something like: "C:\InetPub\WWWroot\Server1\directory1\directory1.1\test, test.txt".
0
 
LVL 2

Expert Comment

by:Ixeus
ID: 6439722
I don't think that throws an error Alpha, it just gives what you give it... I believe he just gave an example, not what he actually did... but if he did. I don't know
0
 
LVL 2

Expert Comment

by:Ixeus
ID: 6439796
I don't think that throws an error Alpha, it just gives what you give it... I believe he just gave an example, not what he actually did... but if he did. I don't know
0
 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6439820
These are the errors I got with:

Server.MapPath("C:\test, test.txt")
"The Path parameter for the MapPath method must be a virtual path. A physical path was used."

Server.MapPath("test, test.txt")
"An invalid character was specified in the Path parameter for the MapPath method."

Server.MapPath(Server.URLEncode("test, test.txt"))
D:\SERVER-CLIENTS\Bis-Web\2-Sites-Not-Hosted-By-Bis-Web\A&L_Intranet\_management\test%2C+test%2Etxt

So I guess it works with your example then lxeus?

0
 
LVL 19

Expert Comment

by:daveamour
ID: 6439839
Can we use Server.MapPath("test" & chr(44) & " test.txt")

?

Dave
0
 
LVL 2

Expert Comment

by:Ixeus
ID: 6439856
I believe Server.Mappath(Server.HTMLEncode("test, text.txt")) would work fine...

did it?

0
 
LVL 19

Expert Comment

by:daveamour
ID: 6439863
Server.HTMLEncode is used to preserver characters displatyed in an HTML page from not being interpreted as HTML  - ie displaying a less than sign

All the work here is done on the server so I don see how this solves anything

Dave
0
 
LVL 2

Expert Comment

by:Ixeus
ID: 6439865
Believe so Alpha, I use Server.HTMLencode for any field manipulation or parameter passing, simply cos of illegal chars throwing errors...

~Ixeus
0
 
LVL 2

Expert Comment

by:Ixeus
ID: 6439879
p.s. I think he went to bed...

0
 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6440082
I think Server.URLencode is the one to use. Surely Server.HTMLEncode would do nothing to "test, test.txt", as there's no illegal characters in there?

Server.URLPathEncode might be the real one to try?
0
 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6440089
And maytawn... WAKE UP!!!!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:Ixeus
ID: 6440106
blehhhhhhhhhhhhh what everrrrrr :p
same difference, idea was there
oh well =p

guess yours then
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 6440129
If you have a file at "/work/test, test.txt" you can user Server.MapPath("/") to get the path to the applications root, then add the virtual path. If you have a file at "work/test, test.txt", you can user Server.MapPath(".") to get the path of the current folder, then add the virtual path.

Function GetPath(strFile)
   If Left(strFile,1)="/" Then
      GetPath=MapPath("/") & strFile
   Else
      GetPath=MapPath(".") & "\" & strFile
   End If
End Function
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 6440137
...and maybe replace / with \ in the name, if the file system is picky...

Function GetPath(ByVal strFile)
  strFile=Replace(strFile,"/","\")
  If Left(strFile,1)="/" Then
     GetPath=MapPath("/") & strFile
  Else
     GetPath=MapPath(".") & "\" & strFile
  End If
End Function
0
 
LVL 1

Author Comment

by:maytawn
ID: 6443349
AlfaNoMore & Ixeus thanks for the suggestions, but neither Server.HTMLEncode nor Server.URLPathEncode work in this case.  I am checking for the existance of a file and if the file name gets converted to have percent signs in it then it will not propperly find the file even if it exits.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 6716633
Experts, thoughts on closing this out?

Split, delete?

Thank you
Computer101
Community Support Moderator
0
 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6717251
Points split between me and lxeus?

We don't know what the reasons are for this not working on maytawn's machine, as they never reported the error!
0
 
LVL 2

Expert Comment

by:Ixeus
ID: 6717267
Wow, long time ago, I totally forgot about this question lol

Yeah that would be nice, hopefully he gets this mail and does something.

-Ix
0
 
LVL 2

Expert Comment

by:Ixeus
ID: 6717273
Member Name maytawn
Date Account Created 02/25/00
Expert Points 0

Questions Asked 2
Last 10 Grades Given  
Question Grading Record 0 Answers Graded / 0 Answers Received
Answers  
Questions Answered 0
Last 10 Grades Received  

Do you think we'll get the points?

-Ixeus
0
 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6717295
salright, Computer101 will give us the points :-)
0
 
LVL 2

Expert Comment

by:Ixeus
ID: 6717302
That's great :)

 Hate it when people do this.  They abuse a good system... augh.

Anyway, thanks ahead of time.
0
 
LVL 1

Author Comment

by:maytawn
ID: 6722144
Please award 50 pts to AlfaNoMore and 50 pts to Ixeus.  Neither of their answers actually solved my problem... I had to solve it a different way.  But they were helpful so I would like to award the points to them.

Thanks
0
 
LVL 1

Expert Comment

by:Computer101
ID: 6730119
Questions for the 2 experts posted in this topic area.

Thank you
Computer101
Community Support Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Migrated SQL connection issues 2012 6 39
Need help to rewrite script 3 68
INNER JOIN ? 8 42
Questions about INCLUDE FILES 2 21
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now