Problem with shell execute

Hi group, I have a problem with shell execute.
I write like this:

Dim s as new shell 
s.Execute("START C:\Program Files (x86)\OpenOffice 4\program\scalc.exe")

and I get an error when opening. Depends on the path format which has spaces ?
error

Try:

s.Execute("START " + Chr(34) + "C:\Program Files (x86)\OpenOffice 4\program\scalc.exe" + Chr(34))

Chr(34) = "
The spaces in the path prevent execution. :wink:

1 Like

s.Execute("START " + """C:\Program Files (x86)\OpenOffice 4\program\scalc.exe""")

or

s.Execute( "START " + chr(34) + " C:\Program Files (x86)\OpenOffice 4\program\scalc.exe" + chr(34))

1 Like

Or:

There are three different ways you can escape file paths on Windows:

  • By enclosing the path (or parts of it) in double quotation marks ( ” ).
  • *By adding a caret character ( ^ ) before each space. (This only works in Command Prompt/CMD, and it doesn’t seem to work with every command.)
  • *By adding a grave accent character ( ` ) before each space. (This only works in PowerShell, but it always works.)

So, a

s.Execute("START C:\Program Files (x86)\OpenOffice 4\program\scalc.exe").ReplaceAll(" ","^ ")

could also work. :smiley:

*Source: https://www.howtogeek.com/694949/how-to-escape-spaces-in-file-paths-on-the-windows-command-line/

2 Likes

Windows command line syntax rules - the very definition of Technical Debt :smiley:

1 Like

The format would have been perfect, but the ReplaceAll doesn’t work

I tested the other examples … but they don’t work. I state that the OpenOffice Path from which I want to run the Calc is that, and it’s right. If I run the commands, the Command Prompt opens, with the right Path of the file in the window title, but the location it points to is Windows/system32


's.Execute("START C:\Program` Files (x86)\OpenOffice` 4\program\scalc.exe")   NO WORK
's.Execute("START " + Chr(34) + "C:\Program Files (x86)\OpenOffice 4\program\scalc.exe" + Chr(34))   OPEN A DOS PROMPT WINDOW
's.Execute("START " + """C:\Program Files (x86)\OpenOffice 4\program\scalc.exe""")    OPEN A DOS PROMPT WINDOW
's.Execute( "START " + chr(34) + " C:\Program Files (x86)\OpenOffice 4\program\scalc.exe" + chr(34))  OPEN A DOS PROMPT WINDOW
's.Execute("START C:\Program Files (x86)\OpenOffice 4\program\scalc.exe").ReplaceAll(" ","^ ")   NOT WORK, error in .ReplaceAll

I was used this command with PAINT and it work fine:

Dim s as new shell 
s.Execute("START /MAX %windir%\system32\mspaint.exe "+Chr(34) + PathImmagine + chr(34))

but now, I am not able to open openoffice calc.

Have you considered just launching the app?


Dim f as folderitem
f = getfolderitem("C:\Program Files (x86)\OpenOffice 4\program\scalc.exe")
if f = nil then 
msgbox "the path is invalid or the file does not exist"
else

if f.exists then
   f.launch
else
  msgbox "the file does not exist"
end if

end if

Actually NO, I didn’t know you could use f.launch.
So launch the application. Now the question is: can I also attach the file to be opened together with the app? In the meantime, I’m going to read the documentation.

The question always changes after the answer. :slight_smile:

if you have a document, and SCALC.exe is the default app which opens it in Windows (association), then just launch the actual document file instead.
or use

start somedoc.someextension

If SCALC is NOT the default app, then you are back to a command line solution.

You’re right… the problem is that I don’t know all the commands and I try to do what I need with what little I know. Then, asking for advice, I understand that instead there is so much more than what I think … I thought (as I did for the PAINT) to launch the calc app and attach the file I needed … but there is no I managed. Now you’ve directed me to another path … I understand what you told me but I don’t understand how it works … I certainly have to study. In my case the .xls files I open with calc, so it’s the default app … for now I would like to leave the path of “start somedoc.someextension” and go back to f.launch. But is it possible to open the file I need together with the APP? (Basically I have an xls file that I have to open when I press a button)

I have an xls file that I have to open when I press a button

Dim f as folderitem
f = specialfolder.documents.child("myfile.xls")
if f = nil then 
msgbox "the path is invalid or the file does not exist"
else

if f.exists then
   f.launch
else
  msgbox "the file does not exist"
end if

end if

[/quote]

Thanks Jeff, I thought you should open the application + file… I discovered a new thing.

Dim f as folderitem
f = specialfolder.documents.child(LeggiRigoFileDiTesto(4))
if f = nil then 
  msgbox "the path is invalid or the file does not exist"
else
  
  if f.exists then
    f.launch
  else
    msgbox "the file " + LeggiRigoFileDiTesto(4) +" does not exist"
  end if
 
end if

The path read by LeggiRigoFileDiTesto(4) is “C:\Users\Federico\Desktop\ListinoHF.xls”

I’ve been careful to remove all the special characters, but it says it can’t find the file. But the file is right there. Where am I wrong?

So: read the documentation (they used RTFM in the old times, but there are no more manuals nowadays…)

When dealing with files, read FolderItem

Or your [LOCAL] documentation if you use a pre Xojo 2019 version. Set that in the preferences.

PS: yes, I know, it is a boring task.

The look vary depending on the Xojo IDE version, but:

If you set the preferences to the local (or built-in as in the screen shot) documentation, you will get the documentation that is for your running Xojo version.

What special characters?
If you take any characters out, you can easily break it.
If there are accented characters, (eg ç , å ) they may be unicode characters, and you may not be retaining them in your text file.

Please screenshot the value of LeggiRigoFileDiTesto(4) in the debugger before you do anything with the characters.
Also screenshot the folder contents as they appear in Windows, so that we can see the actual filename and location.
Show us the contents of the file which is parsed by LeggiRigoFileDiTesto

And finally, is that folder synched by OneDrive or Dropbox?
The file may not actually be available immediately.
But it’s almost certain that the filename is not correct.

I was saying that there are no special characters, so the error cannot be attributed to them, the problem will be somewhere else. I am attaching the various screenshots.
File desktop position:


My text file … i read row n°4.

Verifing path read
2 Read Path File
Error. File non find.
3 Error file read

The problem occurs even i write : f = specialfolder.documents.child(“C:\Users\Federico\Desktop\ListinoHF.xls”)

Of course !

Read the documentation first, then write the commands !

You use a path to the DOcuments folder and then pyou add a full path to elsewhere ! Even a human can have troubles reading that.

If the file is in the Desktop folder, use that (if exist, check the doc): SpecialFolder.Desktop?Child(“ListinoHF.xls”)

capice ?

The windows screenshot says the path is c:\utenti.…

Maybe that’s the problem?

You’re right Emile, it’s that I’m not a programmer, I wrote something in VB6 and for some things it’s difficult for me to understand the logic of Xojo.
Ok I solved it, at least it seems, by setting f = specialfolder.UserHome.Child(“\Desktop\ListinoHF.xls”).
(P.S: I referred to the guide https://documentation.xojo.com/api/files/specialfolder.html)

But my question is this: Can I set a Path to be read? In the sense that the.xls file that I have to open can also be found on a network PC.

Jeff
i solved with:
f = specialfolder.UserHome.Child(“\Desktop\ListinoHF.xls”).

I seem to have understood from what I’ve read, that the FolderItem has preset Paths, which can be recalled… Ex: Templates, System, UserHome…etc…etc… as per the documentation. But can’t I set a path without having to refer to the preset ones?
…what if the file is located on a network folder or c:\folder1\folder2 ?

Blockquote

You can even make easier it by using desktop directly.
f = SpecialFolder.Desktop.Child(“ListinoHF.xls”)