# Run a batch file from Processing

**URL:** <https://discourse.processing.org/t/run-a-batch-file-from-processing/43474>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [December 14, 2023, 2:03pm UTC](https://discourse.processing.org/t/run-a-batch-file-from-processing/43474 "2023-12-14T14:03:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bask185](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@bask185](https://discourse.processing.org/u/bask185)\
**Post date:** [December 14, 2023, 2:03pm UTC](https://discourse.processing.org/t/run-a-batch-file-from-processing/43474/1 "2023-12-14T14:03:53Z")

</div>

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.

```auto
./flashArduino.bat
.\flashArduino.bat
flashArduino.bat

```

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![bask185](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@bask185](https://discourse.processing.org/u/bask185)\
**Post date:** [December 14, 2023, 3:07pm UTC](https://discourse.processing.org/t/run-a-batch-file-from-processing/43474/2 "2023-12-14T15:07:27Z")

</div>

I found that running this line from the sketch:

```auto
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

```auto
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

```auto
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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 15, 2023, 12:27pm UTC](https://discourse.processing.org/t/run-a-batch-file-from-processing/43474/3 "2023-12-15T12:27:23Z")

</div>

Hello @bask185,

There may be something here that can be useful:

> [@Can't open .bat file!](https://discourse.processing.org/t/cant-open-bat-file/29299/2):
>
> Hello, This worked for me: /\* \* Launch Example 1 \* v1.0.0 \* GLV 2021-04-12 \* Launches Google Chrome \*/ void setup() { //Works: //println(sketchPath("")+"data/go.bat"); //launch(sketchPath("")+"data/go.bat"); //Works: println(sketchPath("")+"data\\go.bat"); launch(sketchPath("")+"data\\go.bat"); } go.bat in \data folder: REM notepad.exe start "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "www.howtogeek.com" pause How to format: [FAQ - Processing F…](https://discourse.processing.org/faq#format-your-code)

`:)`

---

<div class="post-metadata">

**Author:** ![bask185](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@bask185](https://discourse.processing.org/u/bask185)\
**Post date:** [December 18, 2023, 2:08pm UTC](https://discourse.processing.org/t/run-a-batch-file-from-processing/43474/4 "2023-12-18T14:08:07Z")

</div>

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.

```auto
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 😑

Kind regards 🥃

Bas

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [December 18, 2023, 3:11pm UTC](https://discourse.processing.org/t/run-a-batch-file-from-processing/43474/5 "2023-12-18T15:11:47Z")

</div>

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.
