Using paths with accented characters in Windows shell commands

I’m probably missing something really obvious here, so I’m hoping that someone will be able to point out precisely how I’m being an idiot :wink:

I’m using an instance of Shell to run commands on Windows, and struggling to work out how to work with file paths that include accented characters. Here’s a simple test:

  1. Create a file that contains an accented character (e.g. “”) in its path.

  2. Copy the following into a sample project:

[code]dim theShell as Shell = New Shell
dim theCommand as String
dim theDialog as New OpenDialog
dim theFile as FolderItem

theFile = theDialog.ShowModal
if theFile = Nil then
Return
end if

theCommand = “”"" + theFile.NativePath + “”""
theShell.Execute( theCommand )

if theShell.ErrorCode <> 0 then
Break
end if[/code]

  1. Run that, and select the file that you created in step 1.

If you select a file that doesn’t include an accented character, the shell command will open the file. If the path includes an accented character, the shell returns error code 1, “The system cannot find the path specified.”

I must be doing something pathetically obviously wrong here. How do I make this work with accented characters?

Tom, take a look at this article which may point you in the right direction (unless someone comes up with a simpler way).

https://stackoverflow.com/questions/16180652/manage-paths-with-accented-characters

Thanks, Paul. That looks helpful if I were working with batch files and know which code page to specify, but I don’t know how I would go about applying that to a shell command that needs to work with an arbitrary file path.

This is probably really obvious to people who know more about Windows than I do, especially those who are more likely to have an accented character in their username. The actual example that I’ve seen from a customer is ““C:\Users\Cline\AppData\Local\Temp\257612791824.html””

WIll be better (to look and understand) to use:

theCommand = Chr(34) + theFile.NativePath + Chr(34)

Thanks, Emile: that does indeed look nicer, but doesn’t fix the problem :wink: Any idea how to use accented characters in the file path?

It looks like Xojo is using the ASCII call instead of the Unicode call, try this (works here)

Declare Function ShellExecute Lib "Shell32" Alias "ShellExecuteW" (hwnd As Ptr, lpOperation As WString, lpFile As WString, lpParameters As WString, lpDirectory As WString, nShowCmd As Int32) As Integer Dim ok As Integer ok = ShellExecute(Nil, "open", theFile.NativePath, "", "", 5)

Thanks, Julian. Yes, it does rather look like Xojo might be using ASCII rather than Unicode.

Your example works well in my test case, so thanks for that. However, the real use case is rather more complicated in that I’m using a shell command to tell WKHTMLTOPDF to convert an HTML file to a PDF, like so:

"C:\\Program Files (x86)\\Light Blue\\Light Blue Resources\\wkhtmltopdf-pc\\bin\\wkhtmltopdf.exe" --zoom 1, --encoding utf-8 --margin-top 10mm --margin-left 10mm --margin-bottom 10mm --margin-right 10mm --page-width 214mm --page-height 300mm "C:\\Users\\Cline\\AppData\\Local\\Temp\\257612791824.html" "C:\\Users\\Cline\\AppData\\Local\\Temp\\257612791824.pdf"

Try:

ok = ShellExecute(Nil, "open", "C:\\Program Files (x86)\\Light Blue\\Light Blue Resources\\wkhtmltopdf-pc\\bin\\wkhtmltopdf.exe", "--zoom 1, --encoding utf-8 --margin-top 10mm --margin-left 10mm --margin-bottom 10mm --margin-right 10mm --page-width 214mm --page-height 300mm ""C:\\Users\\Cline\\AppData\\Local\\Temp\\257612791824.html"" ""C:\\Users\\Cline\\AppData\\Local\\Temp\\257612791824.pdf""", "", 5)

Thanks, Julian. I’ve just tried what you suggested with a non-accented file path and, although the returned value is >32 and therefore according to this page not an error, something’s wrong with the call to WKHTMLTOPDF but I’m not sure where to find the error output.

Here you go, this works on mine:

[code]Dim f As String = “”“C:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe”""
Dim p As String = “–zoom 1 --encoding utf-8 --margin-top 10mm --margin-left 10mm --margin-bottom 10mm --margin-right 10mm --page-width 214mm --page-height 300mm ““C:\Users\Julian\Desktop\Using paths with accented characters in Windows shell commands - Xojo Forum.html”” ““C:\Users\Julian\Desktop\test.pdf”””

system.DebugLog(f)
system.DebugLog§

ok = ShellExecute(Nil, “open”, f, p, “”, 5)[/code]

For future reference, the Feedback case for this problem has been marked as fixed & verified: <https://xojo.com/issue/51246>

can’t you use this?

dim s as string=EncodeURLComponent("C:\\Users\\Cline\\AppData\\Local\\Temp\\257612791824.htm")

Not related but of interest for those using accented characters: <https://xojo.com/issue/40884>

I submitted this feedback case with regard to building Windows 64-bit (2015 R3B9) applications when some of the files use accented characters. The status is “verified”.

Therefore, as of current releases, one still cannot build 64 bit with containers, classes etc. using accented characters.

Use Shellpath instead of nativepath.