Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 FileSystemObject and Apostrophes
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 10 July 2013 :  12:11:35  Show Profile  Reply with Quote
I want to check for the existence of files using FSO, but if the file path and/or name include an apostrophe, the program breaks. Anyone have a suggestion?

Example code:

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If not objFSO.FileExists(strPath&strFile) Then
	strErr=strErr & "<li>"&strFile&" does not exist.</li>"
Else
	objFSO.Close
End If
Set objFSO = Nothing

HuwR
Forum Admin

United Kingdom
20584 Posts

Posted - 10 July 2013 :  12:19:25  Show Profile  Visit HuwR's Homepage  Reply with Quote
try escaping any apostrophes with a slash, so try a replace on strFile to replace ' with \'

MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 11 July 2013 :  09:18:36  Show Profile  Reply with Quote
Replacing with an \' or with an '' doesn't work. Any other suggestions?
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20584 Posts

Posted - 11 July 2013 :  10:41:09  Show Profile  Visit HuwR's Homepage  Reply with Quote
well, from what I can find out, the FSO should not have any problem with filenames containing an apostrophe, are you sure that is definitely what is causing the problem ?

MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20584 Posts

Posted - 11 July 2013 :  10:45:19  Show Profile  Visit HuwR's Homepage  Reply with Quote
quote:
From the Script Guy's Blog
Incidentally, if you’re using the FileSystemObject (another way to work with files and folders), you don’t have to worry about this; the apostrophe doesn’t seem to bother the FileSystemObject. For example, if you want to know the size of Ken Myer’s Folder, use this code:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Ken Myer’s Folder")
Wscript.Echo "Size: " & objFolder.Size
No escaping or other coding tricks required.



MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 11 July 2013 :  11:50:13  Show Profile  Reply with Quote
The code works perfectly on all folders/files except for those containing an apostrophe and/or an ampersand.
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20584 Posts

Posted - 11 July 2013 :  13:30:59  Show Profile  Visit HuwR's Homepage  Reply with Quote
what are you actually getting when you run your code, I just tried this myself and it works quite happily with ampersand and apostrophes in file names

MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20584 Posts

Posted - 11 July 2013 :  13:34:15  Show Profile  Visit HuwR's Homepage  Reply with Quote
this is the code I ran, and it quite happily displays that the file exists

<%
strPath = "c:\temp\"
strFile = "apostroph'e&ampersand.txt"
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If not objFSO.FileExists(strPath&strFile) Then
	strErr=strErr & "<li>"&strFile&" does not exist.</li>"
Else
	strErr=strErr & "<li>"&strFile&" exists.</li>"
End If
Set objFSO = Nothing
Response.Write strErr
%>

results in
  • apostroph'e&ampersand.txt exists.


FYI, your code errors on the line objFSO.Close

MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 11 July 2013 :  20:04:38  Show Profile  Reply with Quote
Found it - wasn't in this routine but in the chkstring function; was resulting in a double apostrophe before checking the file name. Thanks. Now if only I could figure out that javascript mouse-over routine, I'd be through with this and I could rest.

Edited by - Carefree on 11 July 2013 20:05:52
Go to Top of Page

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 11 August 2015 :  16:22:13  Show Profile  Reply with Quote
This is very old, but here's an update. I solved the mouseover Javascript routine by replacing apostrophes with the U+2032 symbol (#8242;). They still look like apostrophes (and actually sort properly now whereas the apostrophe didn't), so now the Javascript is happy.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.14 seconds. Powered By: Snitz Forums 2000 Version 3.4.07