unicode folderitem.Shellpath

HI,

I’m having a problem running a shell command where the path of the shell command is on a Chinese system using Chinese encoding.

I have a folderitem which points to ‘C:\Documents and Settings\Eric\??\folder’. Folderitem.exists is True.

dim fpath as string = folderitem.shellpath

On my (British English) system:
fpath is “C:\DOCUME~1\Eric\263E~1\folder”

When I run: Shell.execute("cd "+fpath). It succeeds - Shell.ErrorCode=0

So far, so good.

When I run it on a Chinese system, however fpath retains it’s Chinese path and now always fails when passed as a path to shell.execute
“C:\DOCUME~1\Eric\??\folder”

When I run Shell.execute("cd "+fpath). It fails - Shell.ErrorCode=1. I can’t read Shell.Result as I can’t work out what encoding the result comes back as.

If I run cd "C:\\DOCUME~1\\Eric\\??\\folder" from a command prompt(cmd.exe) it works.

So, I either have an environmental variable problem (why does it work from cmd.exe?) or a path encoding issue.

Anyone have an idea?

I tried the ‘Interactive Shell’ example on the system and all the paths are a mess. Just typing dir does not display any of the correct paths. Typing cd "C:\\DOCUME~1\\Eric\\??\\folder" does not work either.

Lee

[quote=186556:@Lee Badham]HI,

I’m having a problem running a shell command where the path of the shell command is on a Chinese system using Chinese encoding.

I have a folderitem which points to ‘C:\Documents and Settings\Eric\??\folder’. Folderitem.exists is True.

dim fpath as string = folderitem.shellpath

On my (British English) system:
fpath is “C:\DOCUME~1\Eric\263E~1\folder”

When I run: Shell.execute("cd "+fpath). It succeeds - Shell.ErrorCode=0

So far, so good.

When I run it on a Chinese system, however fpath retains it’s Chinese path and now always fails when passed as a path to shell.execute
“C:\DOCUME~1\Eric\??\folder”

When I run Shell.execute("cd "+fpath). It fails - Shell.ErrorCode=1. I can’t read Shell.Result as I can’t work out what encoding the result comes back as.

If I run cd "C:\\DOCUME~1\\Eric\\??\\folder" from a command prompt(cmd.exe) it works.

So, I either have an environmental variable problem (why does it work from cmd.exe?) or a path encoding issue.

Anyone have an idea?

I tried the ‘Interactive Shell’ example on the system and all the paths are a mess. Just typing dir does not display any of the correct paths. Typing cd "C:\\DOCUME~1\\Eric\\??\\folder" does not work either.

Lee[/quote]

I would look at nativepath to see if I can get the Dos name for ??. With some luck it will give you 263E~1.

A possible workaround since apparently shell does not stomach UTF-16 but Cmd does, would be to use a batch file which takes the path as argument and executes it through cmd.

My original implementation used folderitem.Nativepath, but that did not work either.

When I show the shortpath using directions from http://superuser.com/questions/348079/how-can-i-find-the-short-path-of-a-windows-directory-file

I get “C:\DOCUME~1\Eric\??\folder” on their folder

and “C:\DOCUME~1\Eric\263E~1\folder” on mine.

NativePath gives:
“C:\Documents and Settings\Eric\??\folder”

Using dir /x on seems to show that the desktop folder does not have a shortname on either system, unlike (for example) ‘My Documents’ has a shortname of ‘MYDOC~1’

Lee

Also, when I tried

cd "C:\\Documents and Settings\\Eric\\??\\folder\"

From the ‘Interactive Shell’ example I get an error.

This works from cmd.exe /u

Lee

[quote=186569:@Lee Badham]Also, when I tried

cd "C:\\Documents and Settings\\Eric\\??\\folder\"

From the ‘Interactive Shell’ example I get an error.

This works from cmd.exe /u

Lee[/quote]

What about shell.execute(“CMD /C mycommand”) ? That should pass the command to Cmd and with some luck do the same thing as the command prompt.

Also, you may want to have a look here : http://stackoverflow.com/questions/1259084/what-encoding-code-page-is-cmd-exe-using

Hi Michel,

Thanks for your replies so far.

I’d had looked at the chcp page earlier and tried setting the chcp as one of the commands in the string but to no affect.

Their system returns chcp 936

I’ve just tried adding "cmd /u /c " to the beginning of the command (also cmd /c) but that did not work either when called from a Xojo shell. Exactly the same string, including the ‘cmd /u /c’ pasted into an open ‘cmd.exe /u’ window works fine.

I’ve tried various convert encoding values too on the string before running .execute as well.

Lee

[quote=186577:@Lee Badham]Hi Michel,

Thanks for your replies so far.

I’d had looked at the chcp page earlier and tried setting the chcp as one of the commands in the string but to no affect.

Their system returns chcp 936

I’ve just tried adding "cmd /u /c " to the beginning of the command (also cmd /c) but that did not work either when called from a Xojo shell. Exactly the same string, including the ‘cmd /u /c’ pasted into an open ‘cmd.exe /u’ window works fine.

I’ve tried various convert encoding values too on the string before running .execute as well.

Lee[/quote]

Difficult to help since obviously I do not have a Chinese Windows :wink:

Last idea : try launching Cmd with the same parameters with FolderItem Launch. Since that will not be in the shell, it should execute just as it does manually.

It’s an interactive shell, so I need to wait for the command to finish and see whether it succeeded. I’m not sure how I could use folderitem.launch

Lee

Using shell.Result.DefineEncoding(Encodings.SystemDefault).ConvertEncoding(encodings.UTF8) I get a ‘Cannot find the file specified’ error in Chinese.

What you really need is the Dos name for the system to use. The issue is that Xojo does not make the difference between Dos name and long name.

Here is how you can proceed :

dim f as folderitem = specialfolder.desktop.child("tagada").child("??") dim s as new shell s.execute "dir /x "+f.shellpath msgbox s.result

You will need to parse the result, but you do get your short name 263E~1 (or whatever) from ??.

I tested this on an English system, but with some luck, the Chinese Dos command will work the same.

Stupid question, I suppose, but have you tried putting quotes around the path in your shell command?

This is the problem I have. the shortname is still “C:\DOCUME~1\Eric\??\folder” on the chinese system, not "C:\DOCUME~1\Eric\263E~1\folder, like I have on mine when I create a folder with that name. It’s weird but it looks like the Desktop folder is a special case and has no shortname either on my English XP, or on their Chinese one.

Tim - All the paths are definitely quoted.

Michel - this is exactly what I’m doing: Converting a folderitem into a shellpath to send to a shell.

Here’s a screenshot of an application I’ve written to test the paths.

This is what I have in the action event of the button.

dim f as folderitem = SelectFolder if f<>nil then self.Listbox1.AddRow(array("ShellPath",f.ShellPath)) self.Listbox1.AddRow(array("nativePath",f.NativePath)) dim s as new shell dim st as string st="dir /x """+f.ShellPath+"""" s.Execute(st) self.Listbox1.AddRow(array("dir /x ShellPath",st,str(s.ErrorCode)+" "+s.Result.DefineEncoding(Encodings.SystemDefault).ConvertEncoding(encodings.UTF8))) st=st.ConvertEncoding(encodings.SystemDefault) s.Execute(st) self.Listbox1.AddRow(array("dir /x ShellPath encoded",st,str(s.ErrorCode)+" "+s.Result.DefineEncoding(Encodings.SystemDefault).ConvertEncoding(encodings.UTF8))) st="dir /x """+f.NativePath+"""" s.Execute(st) self.Listbox1.AddRow(array("dir /x NativePath",st,str(s.ErrorCode)+" "+s.Result.DefineEncoding(Encodings.SystemDefault).ConvertEncoding(encodings.UTF8))) st=st.ConvertEncoding(encodings.SystemDefault) s.Execute(st) self.Listbox1.AddRow(array("dir /x NativePath encoded",st,str(s.ErrorCode)+" "+s.Result.DefineEncoding(Encodings.SystemDefault).ConvertEncoding(encodings.UTF8))) end if

The test code is at Here

The three columns show how the path string is created from the folderitem, the command sent to the shell and the result (shell.errorcode shell.Result).

Shell.errorcode = 1 when it fails and the Chinese shell.Result is ‘System cannot find the file specified’
Shell.errorcode = 0 when it succeeds and the Chinese shell.Result is the directory structure.

This shows that an ASCII shortname works ok, but a chinese one doesn’t.

Exactly the same command pasted into a cmd.exe window works.

Lee

[quote=186811:@Lee Badham]This is the problem I have. the shortname is still “C:\DOCUME~1\Eric\??\folder” on the chinese system, not "C:\DOCUME~1\Eric\263E~1\folder, like I have on mine when I create a folder with that name. It’s weird but it looks like the Desktop folder is a special case and has no shortname either on my English XP, or on their Chinese one.

Tim - All the paths are definitely quoted.

Michel - this is exactly what I’m doing: Converting a folderitem into a shellpath to send to a shell.[/quote]

You seem to have missed the most important part. I convert to a shellpath, then I DIR that shell path with the option /x that displays the Dos name.

Have you tried Dir /x, even just in the Command Prompt ? On an English system, at least, it displays the Dos name for a folder I created with name ??.

Hi Michel,

Yes I have:

dim st as string st="dir /x """+f.ShellPath+"""" s.Execute(st)

dir /x always works from the command prompt.

Chinese dir /x

English dir /x

You can see form the screenshot there is no shortname for the chinese desktop folder.

Lee

I am afraid the only option you have now is a declare into Win32 GetShortPathName
https://msdn.microsoft.com/en-us/library/windows/desktop/aa364989(v=vs.85).aspx

Hoping it, too, is not localized.

dir /x shows the desktop folder does not have a shortname

I also tried folderitem.shortnameMBS (using MBS plugins) with the same result. The desktop folder is still localised.

I don’t get why typing the localised name into a cmd prompt works, but the same string in a Xojo shell does not.

In my first post, I thought it was either to do with environmental variables or encoding.

Lee

The difference is that the Xojo Shell does not inherit the environment variables that you get with cmd.exe.

So what is missing?

why would cd "C:\\DOCUME~1\\Eric\\??\\folder\" from a cmd.exe work

but

dim pathcommand as string = "cd """C:\\DOCUME~1\\Eric\\??\\folder\""""
//pathcommand checked to contain the correct quoted string
shell.execute(pathcommand)

not work?

Remember the only problem is with folders with no shortname but are still localised (such as C:\Documents and Settings\Eric\Desktop, which is actually C:\Documents and Settings\Eric\??)
Lee

Wait. This is very important. You mean ?? is the desktop folder ?

Have you tried to point to it with SpecialFolder.Desktop ?

Yep. ?? is the desktop folder. It’s ‘Desktop’ in Chinese.

specialfolder.desktop.shellpath returns ‘C:\DOCUME~1\Eric\??\’

specialfolder.desktop.nativepath returns ‘C:\Documents and Settings\Eric\??’

Lee