New to xojo! Execute a bat file correctly from Xojo

Hi first of all I am happy to be a proud new user of Xojo. I have bought the windows license and I am so happy with Xojo. I am greatfull for all the Xojo developers for developing such an outstanding product!

I come from a background in .net and Vb6 and migration is really easy to this language.

I have a following question.

Problem
I have a .bat file which I need to run from my xojo application. However when I execute it via code it does nothing but when I execute it by double clicking it the batch file does run.

I think therefor that there is a difference between executing a .bat file and double clicking it.

Can anyone tell me what the best way is to have the same result as double click executing?

Below is the bat file

-When ? I double click it from windows it does something.
-When I try to get the same result by executing the bat from xojocode it does nothing?

Can anyone help? What is the best approach to run this bat from a xojo application?

Many thanks!

[code]@echo off

echo ################################# START XAMPP TEST SECTION #################################
echo [XAMPP]: Test php.exe with php\php.exe -n -d output_buffering=0 --version …
php\php.exe -n -d output_buffering=0 --version
if %ERRORLEVEL% GTR 0 (
echo:
echo [ERROR]: Test php.exe failed !!!
echo [ERROR]: Perhaps the Microsoft C++ 2008 runtime package is not installed.
echo [ERROR]: Please try to install the MS VC++ 2008 Redistributable Package from the Mircrosoft page first
echo [ERROR]: http://www.microsoft.com/en-us/download/details.aspx?id=5582
echo:
echo ################################# END XAMPP TEST SECTION ###################################
echo:
pause
exit 1
)
echo [XAMPP]: Test for the php.exe successfully passed. Good!
echo ################################# END XAMPP TEST SECTION ###################################
echo:

if “%1” == “sfx” (
cd xampp
)
if exist php\php.exe GOTO Normal
if not exist php\php.exe GOTO Abort

:Abort
echo Sorry … cannot find php cli!
echo Must abort these process!
pause
GOTO END

:Normal
set PHP_BIN=php\php.exe
set CONFIG_PHP=install\install.php
%PHP_BIN% -n -d output_buffering=0 %CONFIG_PHP%
GOTO END

:END
pause
[/code]

What code are you using to start the BAT file?

Welcome also!

This works with a windows exe
f=New FolderItem("C:\\xampp\\xampp-control.exe",FolderItem.PathTypeShell)

If f.Exists then
  ''f= SpecialFolder.Extensions.child("xampp-control.exe")
  f.Launch

I tried the same to now run the bat file but it didn’t work. It must be the difference in bat vs exe

Hi Mike and Kem!

[quote=164643:@Pieter van den Bosch]This works with a windows exe
f=New FolderItem(“C:\xampp\xampp-control.exe”,FolderItem.PathTypeShell)

If f.Exists then
  ''f= SpecialFolder.Extensions.child("xampp-control.exe")
  f.Launch

I tried the same to now run the bat file but it didn’t work. It must be the difference in bat vs exe

Hi Mike and Kem![/quote]

I am a mac guy vs. Windows, but have you tried to use a Xojo Shell to execute the BAT file vs. the folderitem.launch?

I used the example from the LR: http://documentation.xojo.com/index.php/Shell

Dim s As Shell
s = New Shell
  s.Execute("YOURBATCHFILE.BAT")

If s.ErrorCode = 0 Then
  MsgBox  s.Result
Else
  MsgBox("Error code: " + Str(s.ErrorCode))
End If

Hi Mike many thanks!

I think it is indeed the way of executing I see that the shell execute can run dos commands, I will try your example.

How can I link a path in this example?

s.Execute(“YOURBATCHFILE.BAT”)

s.Execute(“C:\myfolder\YOURBATCHFILE.BAT”)

Just like this?

dim s as new Shell s.execute("start c:\\Users\\Mitch\\Desktop\\myprog.bat")

Start is the important part if you want to see the terminal window. Otherwise f.launch executes the bat file fine, but you won’t get the terminal window.

ok great I will try Michel thanks all

[quote=164649:@Michel Bujardet] dim s as new Shell s.execute("start c:\\Users\\Mitch\\Desktop\\myprog.bat")

Start is the important part if you want to see the terminal window. Otherwise f.launch executes the bat file fine, but you won’t get the terminal window.[/quote]
Thanks Michel! Forgot about that! :slight_smile:

The problem is with your relative paths in the batch file. Your current working directory will be different if you Launch vs. if you double click. If you Execute the file directly, there are even more differences in the environment.

Many thanks all! Love Xojo!

Hi I need to now run this command in cmd.exe (i am in windows, this works)

c:\xampp\mysql\bin>mysqldump -uroot mystore > d:\tmp\database.sql

This will dump a mysql database for me in the d drive folder. Now I want to do this programmatically in Xojo.

where mysqldump.exe is an application and -uroot mystore > d:\tmp\database.sql are the parameters.
In the shell (cmd) this works…

How do I execute this now in xojo with parameters?
I tried this:

[code] Dim sh As New Shell

sh.Execute(“start C:\xampp\mysql\bin\mysqldump -uroot mystore > d:\tmp\database.sql”)[/code]

But this is giving different results… Am i not parsing the parameters correctly in this code?

[quote=178354:@Pieter van den Bosch]Hi I need to now run this command in cmd.exe (i am in windows, this works)

c:\xampp\mysql\bin>mysqldump -uroot mystore > d:\tmp\database.sql

This will dump a mysql database for me in the d drive folder. Now I want to do this programmatically in Xojo.

where mysqldump.exe is an application and -uroot mystore > d:\tmp\database.sql are the parameters.
In the shell (cmd) this works…

How do I execute this now in xojo with parameters?
I tried this:

[code] Dim sh As New Shell

sh.Execute(“start C:\xampp\mysql\bin\mysqldump -uroot mystore > d:\tmp\database.sql”)[/code]

But this is giving different results… Am i not parsing the parameters correctly in this code?[/quote]

What do you mean by “different results” ?

Is this the correct way to add parameters in xojo?

It shows some output that it is creating a dump but the file is 0 kb so the process seems different with the arguments from Xojo,
I have no idea on this one.

How do you normally add from xojo parameters to a bat or exe file? (shell programm, not native windows exe)

[quote=178404:@Pieter van den Bosch]Is this the correct way to add parameters in xojo?

It shows some output that it is creating a dump but the file is 0 kb so the process seems different with the arguments from Xojo,
I have no idea on this one.

How do you normally add from xojo parameters to a bat or exe file? (shell programm, not native windows exe)[/quote]

If database.sql is empty it means the program probably simply does not execute.

If you use exactly the same line as in the terminal, I do not see why the result should be different. In you post, though, I notice you use mysqldump without the extension. The shell may not be able to find the exe that way. You may want to use this instead :

sh.Execute("start C:\\xampp\\mysql\\bin\\mysqldump.exe -uroot mystore > d:\\tmp\\database.sql")

Xojo executes from the root directory. So you may want to enter

CD \\

Before you make sure it does work in the terminal (including start) before running it in Xojo, and you should be fine.

Hi Michel it does not work with the extension sh.Execute(“start C:\xampp\mysql\bin\mysqldump.exe -uroot mystore > d:\tmp\database.sql”)

It does start mysqldump.exe and I see also sql being created in the prompt (normally you dont see that) but the file is still 0kb and typing this manually does work but then I dont see output but will create a correct dump.

So what to do with the CD \ exactly? Thanks for your help btw!

I solved it…This was really strange!

Used the below code

Dim sh As New Shell sh.TimeOut = 99999 sh.Execute("C:\\xampp\\mysql\\bin\\mysqldump.exe -uroot mystore > d:\\tmp\\pieter7.sql") TextArea1.Text = sh.Result

So i did not use start command and had set the timeout to a big number… So I think the start was the problem here but don’t know why…

Start simply makes it run in a visible window. The key here was the TimeOut. Your shell was being killed prematurely. The default timeout is too short for your process.

Or just use -1 as the timeout and it wont have any timeout

Windows is the only one that Timeout affects
http://documentation.xojo.com/index.php/Shell.TimeOut