Run a batch file from Processing

I am trying to run a batch file from my processing sketch, but I cannot get it done. I keep getting an exception.

I tried open(), launch() and exec() and with several paths. I believe open() is no longer used in Processing.

./flashArduino.bat
.\flashArduino.bat
flashArduino.bat

But nothing what I attempt gets the job done. Searching the web came up blank.

void flashProgram()
{
    print("trying to run batch thingy") ;
    try {
        //launch("./flashArduino.bat");
        String myscript = "./flashArduino.bat";
        Runtime rt = Runtime.getRuntime();
        Process pr = rt.exec(myscript); 
    }
    catch( IOException  c )
    {
        print("IOException , it fails") ;
    }
}

The batch file is in the same directory as my Processing files.

How do I call/run the batch file from my Processing application?

1 Like

I found that running this line from the sketch:

launch("rundll32 SHELL32.DLL,ShellExec_RunDLL " + "C:\\Users\\sknippels\\Documents\\hobbyProjects\\functionBloX\\flashArduino.bat") ;

actually launches cmd.exe and runs the script. But now there is something wrong with the paths. + i would like to use relative paths.

Inside the batch files I have this content. I just invoke arduino-cli.exe 2x

echo off

Arduino-cli\arduino-cli compile -b arduino:avr:uno .\arduinoProgram
Arduino-cli\arduino-cli upload ./arduinoProgram -b arduino:avr:uno -p COM4

pause

When double clicking it from the explorer, all is well. But when running the line with launch

This happens

C:\Users\sknippels\Documents\processing-4.0.1>echo off
The system cannot find the path specified.
The system cannot find the path specified.
Press any key to continue . . .

How do I use a relative path here? The absolute path cannot be used anyways.

Kind regards,

Bas

Hello @bask185,

There may be something here that can be useful:

:)

It was not the solution that I needed, but it is surely helpfull. I needed this as well for the next step.

In the meantime I found out in that horrible batch language how to set relative paths.

SET mypath=%~dp0

echo "BUILDING"
%mypath:~0,-1%\Arduino-cli\arduino-cli compile -b arduino:avr:uno %mypath:~0,-1%\arduinoProgram

This %~dp0 and mypath:~0,-1 make total sense :expressionless:

Kind regards :tumbler_glass:

Bas

Hi @bask185. Although Windows uses the \ as directory separator, it accepts / . If you give it a path using / everything works fine. (With / it does not prompt you as you are entering paths in e.g. cmd window.) Using / in your Processing sketch avoids Java needing \ to be escaped.

1 Like