Exec & Shell

Post

Posted
Rating:
#1 (In Topic #717)
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
Greetings all.
I'm trying to run an Open office command line instruction using Exec or Shell.
It's not working, but if I run exactly the same command from the terminal, it works as required.
The process uses 'soffice' to convert a writer document (.odt) file to a txt file.

Terminal command:
soffice –headless –convert-to txt "{path}/myfile.odt"

Code (gambas)

  1. Exec["soffice","--headless","--convert-to","txt","\{path}/myfile.odt"] Wait
  2.  
  3. Shell("soffice --headless --convert-to txt " & chr(34) & "\{path}/myfile.odt" & chr(34)) Wait
  4.  

The quote characters are required as the path has a comma in it - not ideal, but it is what it is.
Again, this works from the command line but not from Gambas.

Any thoughts would be appreciated.

I've also use to "To" parameter to see if there is a resultant error message, but in both cases it returns nothing.

Useful fact: You can use the same command with html instead of txt to convert a odt document to html.

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
I find glitches where my usual shell is bash but gambas uses sh by default.

I start my progs with …

Code (gambas)

  1. System.Shell = System.Find("bash")
  2.  

I find this has fixed many a glitch.


Also what's with Chr(34) ?
Will this not work ?

Code (gambas)

  1.   Shell("soffice --headless --convert-to txt '\{path}/myfile.odt'" Wait
  2.  
Or…

Code (gambas)

  1.   Shell("soffice --headless --convert-to txt \"\{path}/myfile.odt\"" Wait
  2.  


hmm what's that {path} ?  should that not be some gambas code providing an actual path?

did you try ..

Code (gambas)

  1.   Debug Quote("soffice --headless --convert-to txt " & chr(34) & "\{path}/myfile.odt" & chr(34))
  2.  

just to check the command line looks right, valid paths n all?
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
I have used this tool using LibreOffice to convert .ods spreadsheet to a CSV file using this code:-

Code (gambas)

  1. Shell "cd " & sFolder & " && " & "libreoffice --headless --convert-to csv temp.ods" Wait

The following works to change an .odt file with a silly name into a text file: -

Code (gambas)

  1.  Public Sub Form_Open()
  2.  
  3.   Dim sFolder As String = User.Home &/ "Documents"
  4.  
  5.   Shell "cd " & sFolder & " && " & "libreoffice --headless --convert-to txt " & Chr(34) & sFolder &/ "A very, very silly file name.odt" & Chr(34) Wait
  6.  

The changing of the folder at the beginning of the Shell command will create the new file in the same folder, you can remove it if you are happy for the file to be created in your Home folder.

I suggest your code checks that you have created the new file and if not pops up a warning to close LibreOffice (OpenOffice) as you can't do this conversion if LibreOffice (OpenOffice) is open

<IMG src="https://www.cogier.com/gambas/LOConvert.png"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
Another possible solution (without seeing your code)…

If {path} is an Env variable the shell is looking for you can supply it with "With"
This could be one way to do it..

Code (gambas)

  1.  
  2. Shell("soffice --headless --convert-to txt \"$path/myfile.odt\"" With ["path=" & sMyPathString] Wait
  3.  
  4.  

Or something like that :)
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
Also consider this code……
shell must have $ char to access variables

Code (gambas)

  1.  
  2. Shell "echo \"this is \{path}\"" With ["path=hello there"] Wait
  3.  
  4. Shell "echo \"this is $\{path}\"" With ["path=hello there"] Wait
  5.  
  6.  

Prints…
this is {path}
this is hello there

Doing functions on strings in bash is the only time i use {}  for just the path text you'd use $path  using ${path} makes sense when perfoming action like mid string functions like ${path##*/}

Like this ..(gets path part and filename part)…

Code (gambas)

  1. Shell "echo \"this is $path\"\necho \"this is $\{path%/*}\"\necho \"this is $\{path##*/}\"" With ["path=/my/path/to/file.txt"] Wait
  2.  
Prints..
this is /my/path/to/file.txt
this is /my/path/to
this is file.txt


thought it worth a mention
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
Thanks guys.
@ Bruce - The Gambas Quote function has failed on every SQL statement I've used it on so far, so my confidence is somewhat lacking.
I'll have another look to cover all bases, but I'm not confident.

cogier . I'm using LibreOffice but have always used the soffice application for command line stuff.
I found this: libreoffice is a shell script that sets up the environment and passes the command line arguments to the soffice.bin binary.  Here

So I might play around with using the command libreoffice instead of soffice.

Also, you can do multiple files at the same time by using  libreoffice –headless –convert-to txt {path}/*.odt
This will run the conversion process against all .odt files in the target folder.

Gut feel is that the folder and file name both having a apostrophe in them is the causing the failure. Lesson learnt about doing that again !.

Cheers - Quin.
I code therefore I am
Online now: No Back to the top
1 guest and 0 members have just viewed this.