System.util.execute() does not work when executing .bat

Hello everyone.
I'm trying to execute a .bat file on my gateway, but it's not working. I've tried many different ways of writing it and different paths, I've done a lot of research on the forum, and I've also tried creating a shortcut with the "Run as administrator" option checked, but without success.
Could you help me?
Here are some of the commands I've tried:

system.util.execute(['cmd.exe', '/C', 'C:/Program Files/Inductive Automation/Ignition/batfile/Shortcut'])

system.util.execute(["cmd.exe", "/C", "C:\Program Files\Inductive Automation\Ignition\batfile\Shortcut"])

system.util.execute(["C:\\windows\\system32\\cmd.exe","/C","start","C:\\Program Files\\Inductive Automation\\Ignition\\batfile\\Shortcut"])

system.util.execute(["C:\\windows\\system32\\cmd.exe","/C","start","C:\\Program Files\\Inductive Automation\\Ignition\\batfile\\Clean.bat"])

system.util.execute(['C:\\windows\\system32\\cmd.exe','/C','start','C:\\Program Files\\Inductive Automation\\Ignition\\batfile\\Clean.bat'])

system.util.execute(['C:\\windows\\system32\\cmd.exe','/C','start','C:\\Program Files\\Inductive Automation\\Ignition\\batfile\\Shortcut'])

I also tried it in the C:/ directory and on the desktop

Thanks in advance, Best regards.

Where are you executing/testing this script from?

1 Like

In Gateway Events Scripts, tag Change and have it linked to a button.

What are you expecting to happen when the batch script executes? How have you determined it is or isn't running?

The .bat delete files from a folder. I have tried running it on the gateway machine with double click and it works perfectly.

@echo off
set carpeta_base=C:\ProgramData\MySQL\MySQL Server 8.0\Uploads
set carpetas=Folder1 Folder2 Folder3

cd /d %carpeta_base%

for %%f in (%carpetas%) do (
    if not exist "%%f" (
        mkdir "%%f"
    ) else (
        del /q "%%f\*.*"
    )
)

I run a batch file like so with system.util.execute - system.util.execute(['C:\\BackupFiles\\DeleteBackup.bat']) I just call the batch directly, no preceding arguments required. This one runs on my GW as well and does similar to yours, deletes old files. What if you try it this way?

Consider using Java's ProcessBuilder instead of system.util.execute so that you can capture and log the output of the script. Also consider that the gateway doesn't run as a normal user by default and may not have privileges in the target folders. That you can run the batch file yourself on the gateway means nothing.

1 Like

I tried it again and it didn't work.

Ok, try what Phil said regarding using JavaProcessBuilder so you can get some output from the attempt as well as checking the permissions. What user is your Ignition service running as - do they have the permissions to run this file?

2 Likes

As you can see, it appears to have executed successfully, but it still doesn't work. The .bat file is executable by SYSTEM, just like the folders that are being deleted

system.util.execute is "fire-and-forget". It doesn't wait for or check on what it launches. Its own success means nothing. That's why you should be using ProcessBuilder.

What showed up in your log?

Also, you'll need to capture the error stream, too.