• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 520
  • Last Modified:

Using FSO to read and list links to folder of PDFS. PDF names with apostrophes are broken. Is there a way around it?

Using FSO to read and list links to folder of PDFS. PDF names with apostrophes are broken. Is there a way around it?

I would just ask the co-workers to rename, but when it requires them to use company's names with apostrophes it's not an option.

In essence. I am hoping that the users do not have to change the way they name their PDFs but the magic works behind the scenes with code so that the listed URL, looks "normal" and the link is where the alteration and revert to match the original file name so that the link still works.

Calling all geniuses!

Thank you in advance.
0
colonelblue
Asked:
colonelblue
  • 3
  • 2
1 Solution
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
It's probably not a good idea to have an apostrophe in the file name.  Outside of numbers and letters, I would just use an underscore or hypen.  

Otherwise, instead of using ' use chr(39) or '  The full list is http://www.w3schools.com/tags/ref_entities.asp  

But get rid of the apostrophe would be the best choice.
0
 
RobSampsonCommented:
If you use the FSO to read a file name, like
For Each objFile In objFSO.GetFolder("C:\Temp").Files
   WScript.Echo objFile.Path
Next

Open in new window


if you want them to be URL encoded, try this function:
Function URLEncode(strURLString)
	' TITLE: URLEncode
	' DESCRIPTION: This function will modify a string into a URL friendly string
	'	by replacing URL invalid characters with the equivalent hex codes.
	' INPUT:
	'	strURLString contains a string that is to be encoded into a URL friendly string
	' OUTPUT:
	'	A string value that is the string encoded with the hex codes for the invalid characters
	strTemp = ""
	strChar = ""
	For intPos = 1 To Len(strURLString)
		intASCII = Asc(Mid(strURLString, intPos, 1))
		If intASCII = 32 Then
			strTemp = strTemp & "%20"
		ElseIf ((intASCII < 123) And (intASCII > 96)) Then
			strTemp = strTemp & Chr(intASCII)
		ElseIf ((intASCII < 91) And (intASCII > 64)) Then
			strTemp = strTemp & Chr(intASCII)
		ElseIf ((intASCII < 58) And (intASCII > 47)) Then
			strTemp = strTemp & Chr(intASCII)
		Else
			strChar = Trim(Hex(intASCII))
			If intASCII < 16 Then
				strTemp = strTemp & "%0" & strChar
			Else
				strTemp = strTemp & "%" & strChar
			End If
		End If
	Next
	URLEncode = strTemp
End Function

Open in new window


and that way, you can use
For Each objFile In objFSO.GetFolder("C:\Temp").Files
   WScript.Echo URLEncode(objFile.Path)
Next

Open in new window


and see if comes out correctly.

Regards,

Rob.
0
 
colonelblueAuthor Commented:
Hello again Rob,

And thank you.
However I don't use "echo".
I'm not sure how to incorporate that with my code.
Would you know how I may do it please?

This is how my code looks.
 <%
				
Dim objFSO, objFile, objFolder

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(Server.MapPath("/Project1/PDFS/" & (RSProject1.Fields.Item("Short_Name").Value))) Then
Set objFolder = objFSO.GetFolder(Server.MapPath("/Project1/PDFS/" & (RSProject1.Fields.Item("Short_Name").Value)))

For Each objFile in objFolder.Files
Response.Write  "<a class='Good'  target='_blank' rel='PDF' border='0' href='/Project1/PDFS/" & (RSProject1.Fields.Item("Short_Name").Value) & "/" & objFile.Name & "'><img src='images/pdf_icon.gif' width='20' height='20' alt='PDF File' align='absmiddle' border='0'/>&nbsp;"
Response.Write  Left(objFile.Name,Len(objFile.Name) - 4)  & "</a><br> "


Next
Else
Response.Write "<p><br><span class='Knead'>There are no files for" & "&nbsp;" & (RSProject1.Fields.Item("Short_Name").Value) & "</span></p>"
End If

Set objFolder = Nothing
Set objFSO = Nothing

%>

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
RobSampsonCommented:
OK, since it's ASP, just replace wscript.echo with Response.Write, but I have done that for you, and included the function.  See how this goes.

Regards,

Rob.

 <%

Function URLEncode(strURLString)
	' TITLE: URLEncode
	' DESCRIPTION: This function will modify a string into a URL friendly string
	'	by replacing URL invalid characters with the equivalent hex codes.
	' INPUT:
	'	strURLString contains a string that is to be encoded into a URL friendly string
	' OUTPUT:
	'	A string value that is the string encoded with the hex codes for the invalid characters
	strTemp = ""
	strChar = ""
	For intPos = 1 To Len(strURLString)
		intASCII = Asc(Mid(strURLString, intPos, 1))
		If intASCII = 32 Then
			strTemp = strTemp & "%20"
		ElseIf ((intASCII < 123) And (intASCII > 96)) Then
			strTemp = strTemp & Chr(intASCII)
		ElseIf ((intASCII < 91) And (intASCII > 64)) Then
			strTemp = strTemp & Chr(intASCII)
		ElseIf ((intASCII < 58) And (intASCII > 47)) Then
			strTemp = strTemp & Chr(intASCII)
		Else
			strChar = Trim(Hex(intASCII))
			If intASCII < 16 Then
				strTemp = strTemp & "%0" & strChar
			Else
				strTemp = strTemp & "%" & strChar
			End If
		End If
	Next
	URLEncode = strTemp
End Function
			
Dim objFSO, objFile, objFolder

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(Server.MapPath("/Project1/PDFS/" & (RSProject1.Fields.Item("Short_Name").Value))) Then
Set objFolder = objFSO.GetFolder(Server.MapPath("/Project1/PDFS/" & (RSProject1.Fields.Item("Short_Name").Value)))

For Each objFile in objFolder.Files
Response.Write  "<a class='Good'  target='_blank' rel='PDF' border='0' href='/Project1/PDFS/" & (RSProject1.Fields.Item("Short_Name").Value) & "/" & URLEncode(objFile.Name) & "'><img src='images/pdf_icon.gif' width='20' height='20' alt='PDF File' align='absmiddle' border='0'/>&nbsp;"
Response.Write  URLEncode(Left(objFile.Name,Len(objFile.Name) - 4))  & "</a><br> "


Next
Else
Response.Write "<p><br><span class='Knead'>There are no files for" & "&nbsp;" & (RSProject1.Fields.Item("Short_Name").Value) & "</span></p>"
End If

Set objFolder = Nothing
Set objFSO = Nothing

%>

Open in new window

0
 
colonelblueAuthor Commented:
Rob you are MOST awesome!
THAT was the solution!!

Thank you so much! :)
0
 
RobSampsonCommented:
No problem.  Thanks for the grade.  Now you can use URLEncode() in any of your code to generate proper hyperlinks. ;-)

Rob.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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