# How to alter macro to allow special characters in filename

Am using LibreOffice 6.3.01 x64 on Windows 10 64bit

The macro below doesn't seem to allow for special characters in my filename (e.g. comma or single quote).

How do I need to change the macro so that it allows all valid filename characters please?

REM  *****  BASIC  *****
Option explicit

Function MYFILEEXISTS(sFullPath as string) as boolean

dim sFileUrl as string

sFileURL = ConvertToUrl(sFullPath)

If FileExists(sFileUrl) then
MYFILEEXISTS = True
else
MYFILEEXISTS = False
end if
End function

edit retag close merge delete

In order to hardcode, you need to encode them as in URLs (such as %2C for a comma, see this). I don’t know how to do it in StarBasic, though, meaning, if there is a dedicated function.

( 2019-07-20 22:04:42 +0100 )edit

Hi

I have just tested this procedure on windows and it works as expected with file names like a,b.html or a'b.html

Besides, I'm probably from an old world but I'm not completely sure it's a good practice to name files like that :)

Regards

( 2019-07-21 07:48:17 +0100 )edit

Hmm - I ask myself whether this is a question to be addressed to users of LibreOffice. The macro above relies on the Basic function ConvertToUrl and if this function cannot deal with filenames specific to an operating system, then this may be considered a bug of LibreOffice Basic (or at least an enhancement request may be required)

( 2019-07-21 13:11:46 +0100 )edit

What does the "sFileURL = ConvertToUrl(sFullPath)" actually do? Is it needed at all given all my filenames are already full pathnames?

( 2019-07-21 16:01:14 +0100 )edit

As far as i understand function FileExists allows two modes to pass a filename

• system notification -- and --

(see here). And function ConvertToUrl - nomen est omen - converts the text string to URL type. According to FileExists your macro should also work, if you

-- dim sFileUrl as string
-- sFileURL = ConvertToUrl(sFullPath)
-- If FileExists(sFileUrl) then
++ If FileExists(sFullPath) then


(Did not yet test)

( 2019-07-21 17:16:18 +0100 )edit

@Opaque - I have changed macro and tested as suggested but it makes no difference. Perhaps the problem is with my formulae rather than the macro itself?

It got nothing to do with your formula but the interpretation of apostroph and single quote and the replacements performed by Calc, when you input a ' into Calc text and thus the filename in your sheet changes. See Tools -> AutoCorrect Options -> Tab: Localized Options. The option Replace in category Single Quotes is ticked by default, and this causes a change in the string of the filename. Try to untick and check again - during my test this made it working.